Docker Client Verbs: Objectives
Docker Client Verbs: Objectives
Objectives
After completing this section, students should be able to manage the life cycle of a container from creation to deletion.
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:
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:
If desired, the container name can be explicitly defined. The --name option is responsible for defining the container name:
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:
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:
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:
Options -t and -i are usually needed for interactive text-based programs, so they get a proper terminal, but not for background daemons.
The docker exec command starts an additional process inside a running container:
The previous example used the container ID to execute the command. It is also possible to use the container name:
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:
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:
4. Return to the original terminal window and verify that the container was stopped:
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:
The reverent_blackwell is the generated name. Students probably will have a different name for this step.
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:
This command starts a new container, lists all files available in the /etc directory in the container, and exits.
12. It is possible to run a container in interactive mode. This mode allows for staying in the container when the container runs:
The -i option specifies that this container should run in interactive mode, and the -t allocates a pseudo-TTY.
14. Remove all stopped containers from the environment by running the following from a terminal window:
15. Remove the container started without a name. Replace the <container_name> with the container name from the step 7:
Next
Lesson 2 (of 9)
Managing Containers
Docker provides the following commands to manage containers:
Each container, when created, gets a container ID, which is a hexadecimal number and looks like an image ID, but is actually unrelated.
Ports that were exposed by the container or the port forwards, if configured.
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:
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 stop: This command is responsible for stopping a running container gracefully:
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:
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:
Before deleting all containers, all running containers must be stopped. It is possible to stop all containers with:
The commands docker inspect, docker stop, docker kill, docker restart, and docker rm can use the container ID instead of the container name.
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.
8. An important feature is the ability to list metadata about a running or stopped container. The following command returns the metadata:
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:
Make a note about the IP address from this container. It will be necessary for a further step.
12. Using the IP address from step 8, try to access the previously created page:
do180
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:
17. Verify the IP address from the new container and check if the do180 page is available:
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:
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:
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:
22. Clean up the images downloaded by running the following from a terminal window:
Back Next