Common Docker image and container management commands

Introduction

The docker command now consists of multiple management commands having related sub-commands embedded within them. Some of the examples of management commands are builder, config, container, image, network, etc.
You could view the full list of management commands available with docker by typing docker –help. In this article, we’ll discuss the sub-commands associated with two of the management commands namely image and container as we are likely to use them the most while working with docker.

We’ll start with docker images. To view a list of available commands with the docker image command type docker image –help.

List images:
To list images we use docker image ls.

[sahil@linuxnix ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
[sahil@linuxnix ~]$

Download an image:
From the docker image ls command output, we determined that we don’t have any image on the system. So, let’s download an image using the docker image pull command.

[sahil@linuxnix ~]$ docker image pull nginx
Using default tag: latest
latest: Pulling from library/nginx
f5d23c7fed46: Pull complete
918b255d86e5: Pull complete
8c0120a6f561: Pull complete
Digest: sha256:eb3320e2f9ca409b7c0aa71aea3cf7ce7d018f03a372564dbdb023646958770b
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

Here we pulled an image comprising the nginx web server installed from the Docker Hub. We’ll talk in detail about what happens during an image pull in a separate post docker images.

Fetch image details:
To view details about an image type docker image inspect followed by the image id.

sahil@linuxnix ~]$ docker image inspect nginx
[
{
"Id": "sha256:e445ab08b2be8b178655b714f89e5db9504f67defd5c7408a00bade679a50d44",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"nginx@sha256:eb3320e2f9ca409b7c0aa71aea3cf7ce7d018f03a372564dbdb023646958770b"
],
"Parent": "",
"Comment": "",
"Created": "2019-07-23T19:59:08.879181657Z",
"Container": "c863d9d5564c77538fa9ae5d46d088aeff1fe21f21da3af224b2ebf9e5dafe73",
"ContainerConfig": {
"Hostname": "c863d9d5564c",
---------------------------------------------output truncated for brevity

This command returns JSON formatted output consisting of of various details about the image.  A noteworthy piece of information provided by this output is the cmd section

Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"nginx\" \"-g\" \"daemon off;\"]"
],

This denotes the command that will be executed when we run a container from this image. In this image, the daemon mode is set to off because when we run nginx in a container we generally do not want it to run as a service.

Linuxnix-free-e-book

We now move on to docker container commands. To view a list of available docker container commands type docker container –help.

List running containers:
To list running container type docker container ls.

[sahil@linuxnix ~]$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[sahil@linuxnix ~]$

We could add the -a flag to the above command which would list out stopped containers as well.

Run a container:
To start a container we use the docker container run command followed by the container image we would like to use.
Let’s run a container from the nginx image we downloaded earlier.

[sahil@linuxnix ~]$ docker container run -P -d nginx
0ae71e907e0f3ba70b4e3b8789a52154750a6a31b8a79a1bebbe051bbd2450c7

In the above command, we’ve added the additional flags -P and -d.

  • The -P flag maps a random port on the local host to an exposed port on the container.
  • The -d flag starts the container in detached mode.

If we do not use the -d flag then the container would run in the foreground and we would not be able to execute any further commands. We can verify from the docker container ls command that our container is indeed running.

[sahil@linuxnix ~]$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ae71e907e0f nginx "nginx -g 'daemon of…" 4 minutes ago Up 4 minutes 0.0.0.0:32768->80/tcp dreamy_haibt
[sahil@linuxnix ~]$

Get container details:
Like we did for an image we could use the docker container inspect command to fetch details about a running container. This too will return JSON formatted output a snippet of the same is shown below.

[sahil@linuxnix ~]$ docker container inspect 0ae71e907e0f
[
{
"Id": "0ae71e907e0f3ba70b4e3b8789a52154750a6a31b8a79a1bebbe051bbd2450c7",
"Created": "2019-08-10T04:16:19.759848622Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3833,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-08-10T04:16:24.675982237Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},

View processes associated with a container:
To view processes associated with a running container type docker top followed by the container id.

[sahil@linuxnix ~]$ docker container top 0ae71e907e0f
UID PID PPID C STIME TTY TIME CMD
root 3833 3816 0 04:16 ? 00:00:00 nginx: master process nginx -g daemon off;
101 3871 3833 0 04:16 ? 00:00:00 nginx: worker process
[sahil@linuxnix ~]$

Stopping and starting containers:
We can use the docker container stop/start commands to stop and start containers.  We’ll demonstrate this with the nginx container that we’d started.

[sahil@linuxnix ~]$ docker container stop 0ae71e907e0f 0ae71e907e0f
[sahil@linuxnix ~]$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ae71e907e0f nginx "nginx -g 'daemon of…" 14 minutes ago Exited (0) 11 seconds ago dreamy_haibt
[sahil@linuxnix ~]$
[sahil@linuxnix ~]$ docker container start 0ae71e907e0f
0ae71e907e0f
[sahil@linuxnix ~]$
[sahil@linuxnix ~]$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ae71e907e0f nginx "nginx -g 'daemon of…" 15 minutes ago Up 3 seconds 0.0.0.0:32769->80/tcp dreamy_haibt
[sahil@linuxnix ~]$

View logs from a running container:
The docker container logs command can be used to show logs related to a running container. In access to our nginx container, this will be the access log as shown below.

[sahil@linuxnix ~]$ docker container logs 0ae71e907e0f
172.17.0.1 - - [10/Aug/2019:04:25:55 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
172.17.0.1 - - [10/Aug/2019:04:34:43 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"

The above traffic was generated by using the curl command on the URL http://localhost:32769 where 32769 is the port on the localhost mapped to port 80 on the container.

View a container’s resource usage:
To view system resources being used by a container, we use the docker stats command as shown below.

[sahil@linuxnix ~]$ docker container stats 0ae71e907e0f
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
0ae71e907e0f dreamy_haibt 0.00% 1.414MiB / 963.1MiB 0.15% 1.08kB / 1.27kB 11.3MB / 0B 2

Running commands on a container:
We may frequently come across situations where we need to execute some commands on a running container. For this purpose, we use the docker exec command.  To demonstrate this let’s run the uname -a command on our nginx container.

[sahil@linuxnix ~]$ docker container exec 0ae71e907e0f uname -a
Linux 0ae71e907e0f 3.10.0-957.12.1.el7.x86_64 #1 SMP Mon Apr 29 14:59:59 UTC 2019 x86_64 GNU/Linux
[sahil@linuxnix ~]$

Get shell access to a container:
Generally, we use the docker attach command to gain access to a container. The docker attach command basically attaches the user to the STDOUT of the container. But in case of our nginx container STDOUT is being sent to access logs, therefore, we wouldn’t be able to get a shell if we run docker attach. The workaround for this is using the docker exec command with a couple of additional options.

sahil@linuxnix ~]$ docker container exec -it 0ae71e907e0f /bin/bash
root@0ae71e907e0f:/# id
uid=0(root) gid=0(root) groups=0(root)
root@0ae71e907e0f:/# uname -a
Linux 0ae71e907e0f 3.10.0-957.12.1.el7.x86_64 #1 SMP Mon Apr 29 14:59:59 UTC 2019 x86_64 GNU/Linux

The -i flag indicates an interactive session and the -t flag denotes tty.
Using the above combination of commands, the /bin/bash shell is executed inside the container and we get logged in via an interactive terminal. This allows us to execute commands within the container. If we want to logout from the container without stopping it we need to use the CTRL+P+Q sequence.

Pausing/Unpausing containers:
Pausing a container pauses the processes running within that container and unpausing it resumes the processes. To pause a container we use docker container pause followed by the container id and to unpause we use the docker container unpause.

[sahil@linuxnix ~]$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ae71e907e0f nginx "nginx -g 'daemon of…" About an hour ago Up 58 minutes (Paused) 0.0.0.0:32769->80/tcp dreamy_haibt
[sahil@linuxnix ~]$
[sahil@linuxnix ~]$ docker container unpause 0ae71e907e0f
0ae71e907e0f
[sahil@linuxnix ~]$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ae71e907e0f nginx "nginx -g 'daemon of…" About an hour ago Up 58 minutes 0.0.0.0:32769->80/tcp dreamy_haibt
[sahil@linuxnix ~]$

Deleting containers:
To delete a container we use the docker container rm command. In case the container is running we need to add -f flag to force the deletion. For demonstration let’s delete the nginx container that we’ve been using thus far in the post.

[sahil@linuxnix ~]$ docker rm -f 0ae71e907e0f
0ae71e907e0f
[sahil@linuxnix ~]$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[sahil@linuxnix ~]$

We could use also use a command called docker container prune to delete all stopped containers without having to specify container ids.

Conclusion

This cocnludes our discussion on commonly used docker image and docker container commands.
We hope that you found these examples to be useful and we look forward to your suggestions and feedback.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he’s gained over the years with the rest of the community.

Latest posts by Sahil Suri (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *