Docker Containers Basics

basic operations docker containers
basic operations docker containers

Basic operations on Docker containers

On the previous post we started getting familiar with Docker containers. How to run them, name them and check their logs. On this article we will continue with some basic operations on containers that can be handy for troubleshooting.

Inspecting Docker container processes

We can inspect the processes running inside our container using the docker equivalent to the linux top command:

# docker top australtech_container
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2343                2315                0                   14:11               pts/0               00:00:00            /bin/bash

Docker Statistics

In addition to the docker top command you can also use the docker stats command. This shows statistics for one or more running Docker containers. Let’s see what these look like. We’re going to look at the statistics for our container:

# docker stats australtech_container
CONTAINER ID        NAME                    CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
45ad6b72f148        australtech_container   0.00%               1.133MiB / 983.9MiB   0.12%               1.59kB / 0B         8.63MB / 0B         1

We see the containers and their CPU, memory and network and storage I/O performance and metrics.

Running a process inside an already running container

We can also run additional processes inside our containers using the docker exec command. There are two types of commands we can run inside a container: background and interactive. Background tasks run inside the container without interaction and interactive tasks remain in the foreground. Interactive tasks are useful for tasks like opening a shell inside a container. Let’s look at an example of a background task.

docker exec -d australtech_container touch /etc/new_file

Here the -d flag indicates we’re running a background process. We then specify the name of the container to run the command inside and the command to be executed. In this case our command will create a new empty file called /etc/new_file inside our container. We can use a docker exec background command to run maintenance, monitoring or management tasks inside a running container.

We can also run interactive tasks like opening a shell inside our container.

# docker exec -t -i australtech_container /bin/bash
root@45ad6b72f148:/# ls -la /etc/new_file
-rw-r--r-- 1 root root 0 Apr 22 14:17 /etc/new_file
root@45ad6b72f148:/# 

The -t and -i flags, like the flags used when running an interactive container, create a TTY and capture STDIN for our executed process. We then specify the name of the container to run the command inside and the command to be executed. In this case our command will create a new bash session inside the container. We could then use this session to issue other commands inside our container, like checking with ls that the file created previously exists.

Stopping a container

If we wish to stop our container, we can do it with the docker stop command, like so:

docker stop australtech_container

Automatic container restarts

If your container has stopped because of a failure you can configure Docker to restart it using the –restart flag. The –restart flag checks for the container’s exit code and makes a decision whether or not to restart it. The default behaviour is to not restart containers at all.
You specify the –restart flag with the docker run command.

sudo docker run --restart=always --name australtech -d ubuntu
/bin/sh -c "while true; do echo hello world; sleep 1; done"

In this example the –restart flag has been set to always . Docker will try to restart the container no matter what exit code is returned. Alternatively, we can specify a value of on-failure which restarts the container if it exits with a non- zero exit code. The on-failure flag also accepts an optional restart count.

Finding out more about our container

In addition to the information we retrieved about our container using the docker ps command, we can get a whole lot more information using the docker inspect command.

# docker inspect australtech_container
[
    {
        "Id": "45ad6b72f148eeb433663dd781167b1290b7a55a3b071349722718a07697e014",
        "Created": "2019-04-19T19:02:04.028311906Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2677,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2019-04-22T14:26:23.692126608Z",
            "FinishedAt": "2019-04-22T14:21:09.905121575Z"
        },
        "Image": "sha256:94e814e2efa8845d95b2112d54497fbad173e45121ce9255b93401392f538499",
        "ResolvConfPath": "/var/lib/docker/containers/45ad6b72f148eeb433663dd781167b1290b7a55a3b071349722718a07697e014/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/45ad6b72f148eeb433663dd781167b1290b7a55a3b071349722718a07697e014/hostname",
        "HostsPath": "/var/lib/docker/containers/45ad6b72f148eeb433663dd781167b1290b7a55a3b071349722718a07697e014/hosts",
        "LogPath": "/var/lib/docker/containers/45ad6b72f148eeb433663dd781167b1290b7a55a3b071349722718a07697e014/45ad6b72f148eeb433663dd781167b1290b7a55a3b071349722718a07697e014-json.log",
        "Name": "/australtech_container",
        "RestartCount": 0,
.............

The docker inspect command will interrogate our container and return its configuration information, including names, commands, networking configuration, and a wide variety of other useful data.
We can also selectively query the inspect results hash using the -f or –format flag.

# docker inspect --format='{{ .State.Running }}' australtech_container
true

This will return the running state of the container, which in our case is true We can also get useful information like the container’s IP address.

# docker inspect --format '{{ .NetworkSettings.IPAddress }}' australtech_container
172.17.0.2

Deleting a container

If you are finished with a container, you can delete it using the docker rm command.

docker rm australtech_container

If the docker container is running you will get an error message like the following one:

Error response from daemon: You cannot remove a running container 45ad6b72f148eeb433663dd781167b1290b7a55a3b071349722718a07697e014. Stop the container before attempting removal or force remove

Therefore, in order to delete a running container, you can do it adding the -f flag:

# docker rm -f australtech_container

We are done for now with docker containers, on the next post we will get into docker images and repositories, stay tuned!