Docker 02 - Image and Container Overview

A Docker image is the application we want to run and a container is an instance of that image running as a process. Docker image can be thought of as a blueprint for containers and containers are the actual running instances of the images. Therefore, a single image can be used to create multiple containers.

Docker Commands for Images and Containers

Viewing Running and Stopped Containers

  • docker container ls or docker ps: Displays a list of running containers.
  • docker container ls -a: Shows all containers, including the stopped ones.

Running a Container

  • docker container run <image>: Runs a container from a specified image. If the image is not available locally, Docker will download it from the Docker Hub.

Executing Commands in a Running Container

  • docker exec -it <container> <command>: Executes a command in a running container.

Naming, Running in Background and Setting Environment Variables

  • Options such as --name, --detach, and --env can be used to name a container, run it in the background, and set environment variables, respectively.

Monitoring Containers

  • docker container logs <container>: Displays the logs of a container.
  • docker container top <container>: Shows the processes running in a container.
  • docker container stats <container>: Provides real-time information about the resource usage of a container.

Removing Containers

  • docker container rm <container>: Removes one or more containers. It’s recommended to stop a container before removing it. However, using the -f option, you can forcefully remove a running container.

Detailed Example: Running an Nginx Container

Let’s breakdown the command as an example:

docker container run --detach --name test1 --publish 8000:80 nginx
  1. --detach: This option runs the container in the background (detached mode), allowing you to continue using your terminal while the container runs.
  2. --name test1: This assigns the name test1 to the container, making it easier to manage (e.g., start, stop, remove) instead of using the container ID.
  3. --publish 8000:80: This option maps port 80 inside the container to port 8000 on the host machine. In this case, accessing http://localhost:8000 on your host machine will direct traffic to the Nginx server running inside the container on port 80.
  4. nginx: This is the name of the Docker image to use. If the Nginx image is not available locally, Docker will pull it from Docker Hub.

Inspecting the Container

To get more detailed information about the running container, use:

  • docker container inspect test1: This command provides detailed information about the container, including its IP address, configuration, and state.

Accessing the Container’s Shell

Sometimes, you might need to access the shell inside the container for troubleshooting or configuration purposes. Use:

  • docker exec -it test1 /bin/bash: This command opens an interactive terminal session inside the running Nginx container, allowing you to execute commands directly inside the container.

Stopping and Removing the Container

When you are done with the container, you can stop and remove it with the following commands:

  • docker container stop test1: This stops the running container named test1.
  • docker container rm test1: This removes the stopped container. If the container is still running, use -f to forcefully remove it.



    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • Dependency Injection
  • CPU Cache
  • Understanding Linear Blended Skinning in 3D Animation
  • Starvation in Operating Systems
  • Virtual Memory