In this section, we will guide you through the process of creating your first Docker container. By the end of this lesson, you will have a basic understanding of how to run a container, interact with it, and manage its lifecycle.

Prerequisites

Before you start, ensure that:

  • Docker is installed on your machine.
  • You have a basic understanding of Docker concepts from the previous lessons.

Steps to Create Your First Docker Container

  1. Pulling a Docker Image

Docker containers are created from Docker images. To start, we need to pull an image from Docker Hub.

docker pull hello-world

Explanation:

  • docker pull is the command used to download images from Docker Hub.
  • hello-world is a simple image that Docker provides to verify that Docker is installed correctly.

  1. Running a Docker Container

Once the image is pulled, you can create and run a container from it.

docker run hello-world

Explanation:

  • docker run is the command used to create and start a container.
  • hello-world is the name of the image from which the container will be created.

When you run this command, Docker will:

  1. Check if the hello-world image is available locally. If not, it will pull it from Docker Hub.
  2. Create a new container from the hello-world image.
  3. Start the container and execute the default command specified in the image.
  4. Print a message to the console and then stop the container.

  1. Listing Running Containers

To see the list of currently running containers, use the following command:

docker ps

Explanation:

  • docker ps lists all running containers. Since the hello-world container stops immediately after printing the message, it won't appear in this list.

To see all containers (running and stopped), use:

docker ps -a

Explanation:

  • -a flag shows all containers, including those that are stopped.

  1. Inspecting a Container

You can inspect the details of a container using the docker inspect command.

docker inspect <container_id>

Explanation:

  • Replace <container_id> with the actual ID of the container you want to inspect. You can get the container ID from the docker ps -a command.

  1. Removing a Container

To remove a container, use the docker rm command.

docker rm <container_id>

Explanation:

  • Replace <container_id> with the actual ID of the container you want to remove.

Practical Example

Let's go through a practical example to reinforce these concepts.

  1. Pull the nginx image:

    docker pull nginx
    
  2. Run a container from the nginx image:

    docker run --name mynginx -d -p 8080:80 nginx
    

    Explanation:

    • --name mynginx assigns a name to the container.
    • -d runs the container in detached mode (in the background).
    • -p 8080:80 maps port 8080 on your host to port 80 in the container.
  3. Verify the container is running:

    docker ps
    
  4. Access the Nginx server: Open a web browser and navigate to http://localhost:8080. You should see the Nginx welcome page.

  5. Stop the container:

    docker stop mynginx
    
  6. Remove the container:

    docker rm mynginx
    

Exercises

Exercise 1: Pull and Run a Different Image

  1. Pull the alpine image.
  2. Run a container from the alpine image and execute the echo "Hello from Alpine!" command.
  3. Verify the output.

Solution:

docker pull alpine
docker run alpine echo "Hello from Alpine!"

Exercise 2: List and Remove Containers

  1. List all containers (running and stopped).
  2. Remove any stopped containers.

Solution:

docker ps -a
docker rm $(docker ps -a -q)

Explanation:

  • $(docker ps -a -q) returns the IDs of all stopped containers.

Common Mistakes and Tips

  • Forgetting to map ports: When running a web server container, ensure you map the container's port to a host port using the -p flag.
  • Not using detached mode: If you want to run a container in the background, use the -d flag.
  • Cleaning up: Regularly remove unused containers and images to free up disk space.

Conclusion

In this lesson, you learned how to create and manage Docker containers. You pulled images, ran containers, inspected them, and cleaned up afterward. These foundational skills are crucial as you progress to more advanced Docker topics. In the next module, we will dive deeper into working with Docker images.

© Copyright 2024. All rights reserved