You are on page 1of 2

Lesson 1 (of 9)

Objectives

After completing this section, students should be able to manage the life cycle of a container from creation to deletion.

Docker Client Verbs


The Docker client, implemented by the docker command, provides a set of verbs to create and manage containers. The following figure shows a summary of the most commonly used
verbs that change container state.

Docker client action verbs


The Docker client also provides a set of verbs to obtain information about running and stopped containers. The following figure shows a summary of the most commonly used verbs
that query information related to Docker containers.

Docker client query verbs


Use these two figures as a reference while you learn about the docker command verbs along this course.

Creating Containers
The docker run command creates a new container from an image and starts a process inside the new container. If the container image is not available, this command also tries to
download it:

$ docker run rhscl/httpd-24-rhel7


Unable to find image 'rhscl/httpd-24-rhel7:latest' locally
Trying to pull repository registry.lab.example.com/rhscl/httpd-24-rhel7 ...
latest: Pulling from registry.lab.example.com/rhscl/httpd-24-rhel7
b12636467c49: Pull complete
b538cc6febe6: Pull complete
fd87b2ca7715: Pull complete
6861c26d5818: Pull complete
Digest: sha256:35f2b43891a7ebfa5330ef4c736e171c42380aec95329d863dcde0e608ffff1e
...
[Wed Jun 13 09:40:34.098087 2018] [core:notice] [pid 1] AH00094: Command line: 'httpd -D FOREGROUND'
$ ^C

In the previous output sample, the container was started with a noninteractive process, and stopping that process with Ctrl+C (SIGINT) also stops the container.

The management docker commands require an ID or a name. The docker run command generates a random ID and a random name that are unique. The docker ps command is
responsible for displaying these attributes:

This ID is generated automatically and must be unique.

This name can be generated automatically or manually specified.

If desired, the container name can be explicitly defined. The --name option is responsible for defining the container name:

$ docker run --name my-httpd-container do180/httpd


The name must be unique. An error is thrown if another container has the same name, including containers that are stopped.

Another important option is to run the container as a daemon, running the containerized process in the background. The -d option is responsible for running in detached mode. Using
this option, the container ID is displayed on the screen:

$ docker run --name my-httpd-container -d do180/httpd


77d4b7b8ed1fd57449163bcb0b78d205e70d2314273263ab941c0c371ad56412

The container image itself specifies the command to run to start the containerized process, but a different one can be specified after the container image name in docker run:

$ docker run do180/httpd ls /tmp


anaconda-post.log
ks-script-1j4CXN
yum.log

The specified command must exist inside the container image.


Since a specified command was provided in the previous example, the HTTPD service does not start.

Sometimes it is desired to run a container executing a Bash shell. This can be achieved with:

$ docker run --name my-httpd-container -it do180/httpd /bin/bash


bash-4.2#

Options -t and -i are usually needed for interactive text-based programs, so they get a proper terminal, but not for background daemons.

Running Commands in a Container


When a container is created, a default command is executed according to what is specified by the container image. However, it may be necessary to execute other commands to
manage the running container.

The docker exec command starts an additional process inside a running container:

$ docker exec 7ed6e671a600 cat /etc/hostname


7ed6e671a600

The previous example used the container ID to execute the command. It is also possible to use the container name:

$ docker exec my-httpd-container cat /etc/hostname


7ed6e671a600

Demonstration: Creating Containers


Watch this video as the instructor shows how to create and manipulate containers.

1. Run the following command:

[student@workstation ~]$ docker run --name demo-container rhel7 \


dd if=/dev/zero of=/dev/null

This command downloads the official Red Hat Enterprise Linux 7 container and starts it using the dd command. The container exits when the dd command returns the result. For educational purposes, the
provided dd never stops.

2. Open a new terminal window from the workstation VM and check if the container is running:

[student@workstation ~]$ docker ps

Some information about the container, including the container name demo-container specified in the last step, is displayed.

3. Open a new terminal window and stop the container using the provided name:

[student@workstation ~]$ docker stop demo-container

4. Return to the original terminal window and verify that the container was stopped:

[student@workstation ~]$ docker ps

5. Start a new container without specifying a name:

[student@workstation ~]$ docker run rhel7 dd if=/dev/zero of=/dev/null

If a container name is not provided, docker generates a name for the container automatically.

6. Open a terminal window and verify the name that was generated:

[student@workstation ~]$ docker ps

An output similar to the following will be listed:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES


05b725c0fd5a rhel7 "dd if=/dev/zero of=/" 13 seconds ago Up 11 seconds reverent_blackwell

The reverent_blackwell is the generated name. Students probably will have a different name for this step.

7. Stop the container with the generated name:

[student@workstation ~]$ docker stop reverent_blackwell

8. Containers can have a default long-running command. For these cases, it is possible to run a container as a daemon using the -d option. For example, when a MySQL container is started it creates the
databases and keeps the server actively listening on its port. Another example using dd as the long-running command is as follows:

[student@workstation ~]$ docker run --name demo-container-2 -d rhel7 \


dd if=/dev/zero of=/dev/null

9. Stop the container:

[student@workstation ~]$ docker stop demo-container-2

10. Another possibility is to run a container to just execute a specific command:

[student@workstation ~]$ docker run --name demo-container-3 rhel7 ls /etc

This command starts a new container, lists all files available in the /etc directory in the container, and exits.

11. Verify that the container is not running:

[student@workstation ~]$ docker ps

12. It is possible to run a container in interactive mode. This mode allows for staying in the container when the container runs:

[student@workstation ~]$ docker run --name demo-container-4 -it rhel7 \


/bin/bash

The -i option specifies that this container should run in interactive mode, and the -t allocates a pseudo-TTY.

13. Exit the Bash shell from the container:

[root@8b1580851134 /]# exit

14. Remove all stopped containers from the environment by running the following from a terminal window:

[student@workstation ~]$ docker rm demo-container demo-container-2 \


demo-container-3 demo-container-4

15. Remove the container started without a name. Replace the <container_name> with the container name from the step 7:

[student@workstation ~]$ docker rm <container_name>

16. Remove the rhel7 container image:

[student@workstation ~]$ docker rmi rhel7

This concludes the demonstration.

Next
Lesson 2 (of 9)

Managing Containers
Docker provides the following commands to manage containers:

docker ps: This command is responsible for listing running containers:

Each container, when created, gets a container ID, which is a hexadecimal number and looks like an image ID, but is actually unrelated.

Container image that was used to start the container.

Command that was executed when the container started.

Date and time the container was started.

Total container uptime, if still running, or time since terminated.

Ports that were exposed by the container or the port forwards, if configured.

The container name.

Stopped containers are not discarded immediately. Their local file systems and other states are preserved so they can be inspected for post-mortem analysis. Option -a lists all containers, including
containers that were not discarded yet:

$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4829d82fbbff do180/httpd "httpd -D FOREGROUND" 15 hours ago Exited (0) 3 seconds ago my-httpd-container

docker inspect: This command is responsible for listing metadata about a running or stopped container. The command produces a JSON output:

$ docker inspect my-httpd-container


[
{
"Id": "980e45b5376a4e966775fb49cbef47ee7bbd461be8bfd1a75c2cc5371676c8be",
...OUTPUT OMITTED...
"NetworkSettings": {
"Bridge": "",
"EndpointID": "483fc91363e5d877ea8f9696854a1f14710a085c6719afc858792154905d801a",
"Gateway": "172.17.42.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"HairpinMode": false,
"IPAddress": "172.17.0.9",
...OUTPUT OMITTED...

This command allows formatting of the output string using the given go template with the -f option. For example, to retrieve only the IP address, the following command can be executed:

$ docker inspect -f '{{ '{{' }} .NetworkSettings.IPAddress {{ '}}' }}' my-httpd-container

docker stop: This command is responsible for stopping a running container gracefully:

$ docker stop my-httpd-container

Using docker stop is easier than finding the container start process on the host OS and killing it.

docker kill: This command is responsible for stopping a running container forcefully:

$ docker kill my-httpd-container

It is possible to specify the signal with the -s option:

$ docker kill -s SIGKILL my-httpd-container

The following signals are available:

SIGNAL Default action Description

SIGHUP Terminate process Terminate line hangup

SIGINT Terminate process Interrupt program

SIGQUIT Create core image Quit program

SIGABRT Create core image Abort program

SIGKILL Terminate process Kill program

SIGTERM Terminate process Software termination signal

SIGUSR1 Terminate process User-defined signal 1

SIGUSR2 Terminate process User-defined signal 2

docker restart: This command is responsible for restarting a stopped container:

$ docker restart my-httpd-container

The docker restart command creates a new container with the same container ID, reusing the stopped container state and filesystem.

docker rm: This command is responsible for deleting a container, discarding its state and filesystem:

$ docker rm my-httpd-container

It is possible to delete all containers at the same time. The docker ps command has the -q option that returns only the ID of the containers. This list can be passed to the docker rm command:

$ docker rm $(docker ps -aq)

Before deleting all containers, all running containers must be stopped. It is possible to stop all containers with:

$ docker stop $(docker ps -q)


The commands docker inspect, docker stop, docker kill, docker restart, and docker rm can use the container ID instead of the container name.

Demonstration: Managing a Container


Watch this video as the instructor shows how to manage a container.

1. Run the following command:

[student@workstation ~]$ docker run --name demo-container -d rhscl/httpd-24-rhel7

This command will start a HTTPD container as a daemon.

2. List all running containers:

[student@workstation ~]$ docker ps

3. Stop the container with the following command:

[student@workstation ~]$ docker stop demo-container

4. Verify that the container is not running:

[student@workstation ~]$ docker ps

5. Run a new container with the same name:

[student@workstation ~]$ docker run --name demo-container -d rhscl/httpd-24-rhel7

A conflict error is displayed. Remember that a stopped container is not discarded immediately and their local file systems and other states are preserved so they can be inspected for post-mortem analysis.

6. It is possible to list all containers with the following command:

[student@workstation ~]$ docker ps -a

7. Start a new HTTPD container:

[student@workstation ~]$ docker run --name demo-1-httpd -d rhscl/httpd-24-rhel7

8. An important feature is the ability to list metadata about a running or stopped container. The following command returns the metadata:

[student@workstation ~]$ docker inspect demo-1-httpd

9. It is possible to format and retrieve a specific item from the inspect command. To retrieve the IPAddress attribute from the NetworkSettings object, use the following command:

[student@workstation ~]$ docker inspect -f '{{ '{{' }} .NetworkSettings.IPAddress {{ '}}' }}' \


demo-1-httpd

Make a note about the IP address from this container. It will be necessary for a further step.

10. Run the following command to access the container bash:

[student@workstation ~]$ docker exec -it demo-1-httpd /bin/bash

11. Create a new HTML file on the container and exit:

bash-4.2# echo do180 > \


/opt/rh/httpd24/root/var/www/html/do180.html
bash-4.2# exit

12. Using the IP address from step 8, try to access the previously created page:

[student@workstation ~]$ curl IP:8080/do180.html

The following output is be displayed:

do180

13. It is possible to restart the container with the following command:

[student@workstation ~]$ docker restart demo-1-httpd

14. When the container is restarted, the data is preserved. Verify the IP address from the restarted container and check that the do180 page is still available:

[student@workstation ~]$ docker inspect demo-1-httpd | grep IPAddress


[student@workstation ~]$ curl IP:8080/do180.html

15. Stop the HTTP container:

[student@workstation ~]$ docker stop demo-1-httpd

16. Start a new HTTP container:

[student@workstation ~]$ docker run --name demo-2-httpd -d rhscl/httpd-24-rhel7

17. Verify the IP address from the new container and check if the do180 page is available:

[student@workstation ~]$ docker inspect demo-2-httpd | grep IPAddress


[student@workstation ~]$ curl IP:8080/do180.html

The page is not available because this page was created just for the previous container. New containers will not have the page since the container image did not change.

18. In case of a freeze, it is possible to kill a container like any process. The following command will kill a container:

[student@workstation ~]$ docker kill demo-2-httpd

This command kills the container with the SIGKILL signal. It is possible to specify the signal with the -s option.

19. Containers can be removed, discarding their state and filesystem. It is possible to remove a container by name or by its ID. Remove the demo-httpd container:

[student@workstation ~]$ docker ps -a


[student@workstation ~]$ docker rm demo-1-httpd

20. It is also possible to remove all containers at the same time. The -q option returns the list of container IDs and the docker rm accepts a list of IDs to remove all containers:

[student@workstation ~]$ docker rm $(docker ps -aq)

21. Verify that all containers were removed:

[student@workstation ~]$ docker ps -a

22. Clean up the images downloaded by running the following from a terminal window:

[student@workstation ~]$ docker rmi rhscl/httpd-24-rhel7

This concludes the demonstration.

Back Next

You might also like