Docker Command Examples (with examples)

Docker Command Examples (with examples)

List all docker containers (running and stopped)

Code:

docker ps --all

Motivation: The docker ps command is used to list all the currently running containers. However, if you also want to display the stopped containers, you can use the --all option along with docker ps. This can be useful when you need to manage multiple containers and want to have an overview of their status.

Explanation:

  • docker ps is used to list the currently running containers.
  • --all option is used to display all the containers, whether they are running or stopped.

Example Output:

CONTAINER ID   IMAGE             COMMAND       CREATED         STATUS                     PORTS    NAMES
a24b9463ef4a   nginx:latest      "nginx -g de…"  5 minutes ago   Up 5 minutes               80/tcp   amazing_mestorf
4e8w83d5rwkd   mysql:latest      "docker-entry…" 2 hours ago     Restarting (0) 4 seconds ago           awesome_mclean

Start a container from an image, with a custom name

Code:

docker run --name mycontainer nginx:latest

Motivation: When starting a container, Docker usually assigns a random name to it. However, in some cases, you may want to customize the name to something more meaningful. By using the --name option, you can specify a custom name for the container.

Explanation:

  • docker run is used to create and start a new container from an image.
  • --name option is used to specify a custom name for the container.
  • mycontainer is the custom name specified in this example.
  • nginx:latest is the image used to create the container.

Example Output:

<no output> (Container "mycontainer" is started)

Start or stop an existing container

Code:

docker start mycontainer
docker stop mycontainer

Motivation: Once you have created a container, you may need to start or stop it based on your requirements. The docker start and docker stop commands allow you to respectively start and stop an existing container.

Explanation:

  • docker start is used to start an existing container.
  • docker stop is used to stop a running container.
  • mycontainer is the name of the container to start or stop.

Example Output (Start):

mycontainer

Example Output (Stop):

mycontainer

Pull an image from a docker registry

Code:

docker pull nginx:latest

Motivation: To create a container from an image, you first need to have that image available on your system. The docker pull command is used to fetch an image from a docker registry and store it locally on your machine.

Explanation:

  • docker pull is used to fetch an image from a docker registry.
  • nginx:latest is the image to be pulled.
  • latest refers to the tag/version of the image to be pulled. If not specified, the latest tag is used by default.

Example Output:

latest: Pulling from library/nginx
Digest: sha256:b36dfc8932bf8e253efb2c7d712415513f59ade880bcad6c25d16ea2cbb8e2b3
Status: Image is up to date for nginx:latest

Display the list of already downloaded images

Code:

docker images

Motivation: When working with Docker, you may have multiple images stored on your machine. The docker images command allows you to display a list of all the images that are already downloaded.

Explanation:

  • docker images is used to list the downloaded images.

Example Output:

REPOSITORY    TAG        IMAGE ID        CREATED         SIZE
nginx         latest     2e2f321b621d    3 days ago      133MB
mysql         latest     1862e8a92778    4 weeks ago     556MB

Open a shell inside a running container

Code:

docker exec -it mycontainer sh

Motivation: In certain scenarios, you may need to access the shell or run commands inside a running container. The docker exec command allows you to execute a command within the specified container.

Explanation:

  • docker exec is used to run a command inside a container.
  • -it option is used to open an interactive terminal session.
  • mycontainer is the name of the container to access.
  • sh is the command to run inside the container, which opens a shell.

Example Output:

# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

Remove a stopped container

Code:

docker rm mycontainer

Motivation: After a container is stopped, it is not automatically deleted. The docker rm command allows you to remove a stopped container from your system.

Explanation:

  • docker rm is used to remove a container.
  • mycontainer is the name of the container to remove.

Example Output:

mycontainer

Fetch and follow the logs of a container

Code:

docker logs -f mycontainer

Motivation: Container logs provide important information about the operation and behavior of the container. The docker logs command allows you to fetch and view the logs of a specific container.

Explanation:

  • docker logs is used to display the logs of a container.
  • -f option is used to follow the log output in real-time.
  • mycontainer is the name of the container to fetch the logs from.

Example Output:

[2022-01-01 16:34:01] [INFO] Application started
[2022-01-01 16:34:05] [ERROR] Something went wrong
[2022-01-01 16:35:12] [INFO] Request received

By utilizing these different Docker commands, you can effectively manage and work with containers and images. Whether it’s listing containers, starting and stopping them, pulling images, managing logs, or accessing shells, Docker provides a versatile command-line interface for containerization tasks.

Related Posts

Using `virsh-domblklist` Command (with examples)

Using `virsh-domblklist` Command (with examples)

Introduction The virsh-domblklist command is a powerful tool used to list information about block devices associated with a virtual machine.

Read More
How to use the command csvstat (with examples)

How to use the command csvstat (with examples)

The csvstat command is a tool included in csvkit that allows users to print descriptive statistics for all columns in a CSV file.

Read More
How to use the command 'reg delete' (with examples)

How to use the command 'reg delete' (with examples)

The ‘reg delete’ command is a useful Windows command that allows you to delete keys or their values from the Windows registry.

Read More