Docker has revolutionized the way developers package, distribute, and deploy applications. It allows you to create lightweight, portable containers that can run applications and their dependencies consistently across different environments. In this beginner’s guide, we will walk you through the essential Docker container operations: creating, listing, and deleting Docker containers. We’ll also provide practical examples using Dockerfiles to illustrate these concepts.

Advertisement

Prerequisites

Before we get started, make sure you have Docker installed on your system. You can download and install Docker from the official website (https://www.docker.com/get-started).

Once Docker is installed, open your terminal or command prompt, and let’s begin.

1. Creating Docker Containers

Running an Interactive Container

To create a Docker container, you can use the docker run command. Let’s start with a simple example:

docker run -it --name my_container ubuntu:lts
  • `docker run` is the command to create and start a container.
  • `-it` stands for “interactive” and “tty,” which allows you to interact with the container’s shell.
  • `–name` specifies the name of the container (in this case, “my_container”).
  • `ubuntu:lts` is the Docker image used to create the container.

You will be dropped into an interactive shell within the Ubuntu container. You can run commands, install software, and experiment inside the container.

Running a Detached Container

Sometimes, you may want to run containers in detached mode, where they run in the background. To do this, you can use the -d flag:

docker run -d --name my_detached_container nginx:latest

This command starts an Nginx web server container in detached mode. You won’t see any output in your terminal, but the container is running in the background.

2. Listing Docker Containers

To list all running containers on your system, use the following command:

docker ps

This command provides information such as the container ID, image used, status, and more.

To list all containers, including stopped ones, use:

docker ps -a

3. Deleting Docker Containers

To delete a container, use the docker rm command followed by the container’s name or ID:

docker rm my_container

If the container is running, you may need to stop it first using docker stop:

docker stop my_container
docker rm my_container

4. Practical Example with Dockerfile

Now, let’s explore how to create a Docker container using a Dockerfile, which is a script that defines the configuration of a Docker image.

  1. Here’s a simple Dockerfile to create a custom Nginx container:
    
    # Use the official Nginx image as the base image
    FROM nginx:latest
    
    # Copy a custom HTML file to the Nginx web root directory
    COPY index.html /usr/share/nginx/html/
    
    
  2. Create a file named Dockerfile with the content above.
  3. Create an index.html file in the same directory as your Dockerfile with your desired HTML content.
    Build the Docker image using the following command:
    docker build -t custom-nginx .
    
  4. Now, you can create and run a container from the custom image:
    docker run -d --name my_nginx_container custom-nginx
    

You’ve successfully created a Docker container using a Dockerfile!

Conclusion

In this beginner’s guide, we’ve covered the fundamental Docker container operations of creating, listing, and deleting containers. We also provided a practical example of creating a custom Nginx container using a Dockerfile. Docker containers offer incredible flexibility and efficiency in application deployment, and mastering these basic operations is the first step towards harnessing their power in your development workflow. Start experimenting with Docker today and unleash the full potential of containerization for your projects.

Share.

3 Comments

  1. hi do you have information where i can try docker for free and online? since my laptop still 32 bit and docker doesnt support old architecture 🙁

    i really want to learn about docker things ..

    • Sunil Raskar on

      Hi FERRI,
      YES, you can learn docker for free. Here are the simple steps that you can follow to begin your journey.

      1. Open an AWS account. You will need a credit card. There will be no charges for 12 months if you really take efforts to understand and use the free service.
      2. Launch Ubuntu EC2 16.04 instance.
      3. Use an SSH terminal using a client software like MobaXterm.
      4. Install docker user docker . You will find tons of docker beginner tutorial on line. Happy learning…

Exit mobile version