Docker - Commands

Docker

In this tutorial we will learn about some of the important Docker commands.

Table of Content

docker run To create/run a container

We use the docker run command to run a docker container from a docker image.

In the following example we are going to run a docker container using the hello-world official docker image.

docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:6e8b6f026e0b9c419ea0fd02d3905dd0952ad1feea67543f525c73a0a790fefb
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Note! Docker first checks if the image is present locally. If the image is not found on our machine then Docker pulls the image from the DockerHub.

If we try to run the docker run command again and use the hello-world image then this time the container will start quicker as the image is already downloaded on our machine.

docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To exit out of a running docker container, use Ctrl+C.

docker run -d To run a container in detach mode

When we run a container using docker run command then the container runs in attach mode or foreground and takes up the terminal and we cannot run any other command till we stop the container by pressing Ctrl+C.

In order to run the container in detach mode or background we use -d flag like docker run -d DOCKER_IMAGE_NAME command.

In the following example we are running docker image in detach mode.

docker run -d nginx

Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
3f9582a2cbe7: Pull complete
9a8c6f286718: Pull complete
e81b85700bc2: Pull complete
73ae4d451120: Pull complete
6058e3569a68: Pull complete
3a1b8f201356: Pull complete
Digest: sha256:aa0afebbb3cfa473099a62c4b32e9b3fb73ed23f2a75a65ce1d4b4f55a5c2ef2
Status: Downloaded newer image for nginx:latest
04cfbdbd3ab064a99a37a512419e39ab6d773a14c67b33674f44e0d2c25e0d17

docker ps To list all running containers

We use the docker ps command to list all the running containers.

In the following example we do not have any running containers on our local machine.

docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

In the following example we are running a container in detach mode and then listing all the running processing.

➜ docker run -d nginx
d386e76309a56a630910ad29276fda63e87874ca694100bb3541e127b04de951


➜ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
d386e76309a5   nginx     "/docker-entrypoint.…"   6 seconds ago   Up 5 seconds   80/tcp    naughty_poitras

docker ps -a To list all containers

We use the docker ps -a command to list all the containers either running or stopped.

docker ps -a

CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
bbba20f4005b   hello-world   "/hello"   6 minutes ago    Exited (0) 6 minutes ago              optimistic_williamson
fb107142026e   hello-world   "/hello"   10 minutes ago   Exited (0) 10 minutes ago             hardcore_cori

In the above example we can see we have two containers that ran and then exited few minutes ago.

docker stop To stop a container

We use the docker stop CONTAINER_ID or docker stop CONTAINER_NAME command to stop a container.

In the following example we have stopped a container by its container Id.

docker stop bbba20f4005b
bbba20f4005b

We can stop multiple containers by providing container Ids separated by space.

docker stop bbba20f4005b fb107142026e
bbba20f4005b
fb107142026e

In the following example we are stopping containers by their name.

docker stop optimistic_williamson hardcore_cori
optimistic_williamson
hardcore_cori

docker start To start a container

To start a stopped container we can run the docker start CONTAINER_ID or docker start CONTAINER_NAME command.

In the following example the nginx container is stopped and we are going to start it.

➜  docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                     PORTS     NAMES
fbf69e8a1f2f   nginx     "/docker-entrypoint.…"   2 minutes ago   Exited (0) 7 seconds ago             jolly_elgamal

Restarting nginx container.

➜  docker start fbf69e8a1f2f
fbf69e8a1f2f

➜  docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS          PORTS     NAMES
fbf69e8a1f2f   nginx     "/docker-entrypoint.…"   3 minutes ago   Up 3 seconds   80/tcp    jolly_elgamal

docker rm To remove a container

We use the docker rm CONTAINER_ID or docker rm CONTAINER_NAME command to remove a container.

In the following example we are first listing all the containers and then removing them by container id and then by container name.

➜ docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
bbba20f4005b   hello-world   "/hello"   17 minutes ago   Exited (0) 17 minutes ago             optimistic_williamson
fb107142026e   hello-world   "/hello"   20 minutes ago   Exited (0) 20 minutes ago             hardcore_cori

➜ docker rm bbba20f4005b
bbba20f4005b

➜ docker rm hardcore_cori
hardcore_cori

➜ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

docker image ls To list all images

We use the docker image ls command to list all the images downloaded on our machine.

In the following example we are listing all the downloaded images on our machine.

docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   17 months ago   13.3kB

docker rmi To remove an image

We use the docker rmi IMAGE_ID and docker rmi IMAGE_NAME command to remove an image from our machine.

In the following example we are removing a docker image by its name.

docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:6e8b6f026e0b9c419ea0fd02d3905dd0952ad1feea67543f525c73a0a790fefb
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359

Note! We must first stop the containers using the image we want to remove/delete.

docker inspect To inspect a docker container or docker image

We run the docker inspect DOCKER_CONTAINER_ID or docker inspect DOCKER_CONTAINER_NAME to inspect a docker container.

Similarly, we run the docker inspect DOCKER_IMAGE_NAME to inspect a docker image.

The default output of this command is in JSON format like the following.

➜  docker inspect hello-world
[
    {
        "Id": "sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412",
        "RepoTags": [
            "hello-world:latest"
        ],
        "RepoDigests": [
            "hello-world@sha256:6e8b6f026e0b9c419ea0fd02d3905dd0952ad1feea67543f525c73a0a790fefb"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-09-23T23:47:57.442225064Z",
        "Container": "8746661ca3c2f215da94e6d3f7dfdcafaff5ec0b21c9aff6af3dc379a82fbc72",
        "ContainerConfig": {
            "Hostname": "8746661ca3c2",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/hello\"]"
            ],
            "Image": "sha256:b9935d4e8431fb1a7f0989304ec86b3329a99a25f5efdc7f09f3f8c41434ca6d",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "20.10.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/hello"
            ],
            "Image": "sha256:b9935d4e8431fb1a7f0989304ec86b3329a99a25f5efdc7f09f3f8c41434ca6d",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 13256,
        "VirtualSize": 13256,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/88ad13d8f49a90be11a3d28b4af2f8b02d3a4ff7cd02791fba7a0dcd6bc8d9be/merged",
                "UpperDir": "/var/lib/docker/overlay2/88ad13d8f49a90be11a3d28b4af2f8b02d3a4ff7cd02791fba7a0dcd6bc8d9be/diff",
                "WorkDir": "/var/lib/docker/overlay2/88ad13d8f49a90be11a3d28b4af2f8b02d3a4ff7cd02791fba7a0dcd6bc8d9be/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

docker logs To fetch the logs of a container

We run the docker logs CONTAINER_NAME or docker logs CONTAINER_ID to check the logs of the container.

In the following example we have container logs of hello-world image.

➜  docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
ac4f8cca0533   hello-world   "/hello"   16 minutes ago   Exited (0) 16 minutes ago             confident_pare

➜  docker logs ac4f8cca0533

Hello from Docker!
This message shows that your installation appears to be working correctly.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT