Chapter 7. Using the Docker Service
When you initialize the Container Development Kit virtual machine (using the vagrant up
command), various services are automatically pre-configured and running — depending on the Vagrantfile you used to provision the Container Development Kit box. The Docker service is running in all cases, and you can use it immediately after the virtual machine is launched. While it is possible to use and interact with the Docker service using GUI tools (for example, the integrated Docker support provided by the Eclipse IDE), this section introduces a number of basic docker
commands to get you started using the Docker service from the command line.
Command-line usage of the Docker service provided by the Container Development Kit box is possible both from within the virtual machine and from the host machine — see section Using the vagrant-service-manager Plugin for instructions on how to set up your host machine to interact with the Docker service running inside the Container Development Kit virtual machine.
See the Get Started with Docker Formatted Container Images chapter of the Red Hat Enterprise Linux Atomic Host 7 Getting Started with Containers guide for a more thorough introduction into the Docker service.
7.1. Preparing Host System for Using Docker from the Command Line Copy linkLink copied to clipboard!
To use the docker
command on your host system to interact with the Docker service running inside the Container Development Kit virtual machine, you need to install the docker
executable.
If you intend to use Docker (and the docker
command) only from within the Container Development Kit virtual machine, no preparation is required — the docker
command is installed in the Container Development Kit box by default.
7.1.1. Installing the docker Executable Copy linkLink copied to clipboard!
Use the install-cli
command of the vagrant-service-manager
plugin to install the docker
binary on your host system.
For example:
To use the install-cli
command when behind a proxy, the Container Development Environment needs to be configured to operate behind a proxy using the vagrant-service-manager
plugin. See section Using vagrant-service-manager to Set Proxy Environment Variables in the Red Hat Container Development Kit 2.4 Installation Guide.
7.1.1.1. Installing a Custom Version of the docker Binary Copy linkLink copied to clipboard!
By default, the install-cli
command installs the docker
binary from the upstream Docker project in version 1.10.3. To install a different version, use the --cli-version
option.
For example:
7.1.1.2. Installing the docker Binary to a Custom Location Copy linkLink copied to clipboard!
By default, the install-cli
command installs the docker
binary from the upstream Docker project to the following directory: /home/joe/.vagrant.d/data/service-manager/bin/docker/1.10.3/
. To install the executable to a different location, use the --path
option. Note that you need to specify an existing directory and the name of the binary.
For example:
7.2. Learning about the Docker Environment Copy linkLink copied to clipboard!
The docker
command offers several sub-commands that let you acquire information about the Docker service, the environment it runs in, and available resources. You can also query the service for images and containers it manages and for images that are available to you from the pre-configured registries. By default, the Docker service in Container Development Kit can download and use images from both the Docker Hub (docker.io) and the Red Hat Atomic Registry (registry.access.redhat.com).
Use the following commands to obtain information about the Docker service and the working environment.
7.2.1. Verifying the Version of the Docker Service Copy linkLink copied to clipboard!
Run the docker version
command to see the version of both the Docker Client and Docker Server:
7.2.2. Displaying Information about the System and Resources Copy linkLink copied to clipboard!
Run the docker info
command to see information about the host system (which, in this case, is the virtualized Red Hat Enterprise Linux instance managed by Vagrant), utilization of virtualization resources, basic networking information, and the numbers of managed containerrs and images:
7.3. Learning about Containers and Images Copy linkLink copied to clipboard!
Use the following commands to obtain information about images and containers on your system.
7.3.1. Listing Managed Images Copy linkLink copied to clipboard!
Run the docker images
command to display a list of images available in the local registry.
7.3.2. Listing Managed Containers Copy linkLink copied to clipboard!
Run the docker ps
command to display a list of running containers. Add the --all
or -a
parameter to list all available containers (not just the running ones). As you can see from the example output below, there are several running containers in Container Development Kit immediately after launch. These containers provide some of the services offered by Container Development Kit and as such should not be stopped or removed. The example below shows containers with the OpenShift service
Use the --format
parameter to specify what information you want displayed about the individual containers (see the docker-ps(1)
manual page for a list of available placeholders). For example:
7.3.3. Displaying Information about Container Resource Usage Copy linkLink copied to clipboard!
Run the docker stats <container>
command to display a live output showing resource-usage statistics for a specific running container. For example:
docker stats openshift
~]$ docker stats openshift
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O
openshift 0.00% 110.4 MB / 2.986 GB 3.70% 0 B / 0 B 77.24 MB / 3.708 MB
7.3.4. Displaying Detailed Information about Container or Image Configuration Copy linkLink copied to clipboard!
Run the docker inspect <container>
command to show low-level information about a container or an image in JSON format. Use the --type
parameter to specify whether you want to display information about a container
(the default) or an image
in case you have containers and images of the same name.
Use the --format
parameter to specify what part of the output you want displayed (see the docker-inspect(1)
manual page for examples of the output). For example, define the following format to list network ports exposed by the container:
docker inspect --format='{{.Config.ExposedPorts}}' openshift
~]$ docker inspect --format='{{.Config.ExposedPorts}}' openshift
map[8443/tcp:{} 53/tcp:{}]
7.4. Getting New Docker-Formatted Container Images Copy linkLink copied to clipboard!
Existing images for creating containers can be either downloaded (pulled) from one of the two pre-configured repositories (the Docker Hub (docker.io) and the Red Hat Atomic Registry (registry.access.redhat.com)) or imported from a local file.
7.4.1. Searching Registries for Images Copy linkLink copied to clipboard!
Run the docker search <search-term>
command to search the pre-configured registries for images. For example, to search for images containing the string rhscl
in the name or namespace, use the following command:
7.4.2. Downloading Images from a Registry Copy linkLink copied to clipboard!
Run the docker pull <image>
command to download (pull) the specified image to your system for local use. Note that while it is possible to provide just the name of the image, it is better practice to also specify the registry you want to pull from and the namespace in which the particular image is published. The naming convention follows this order: [registry/][namespace/]name
.
For example, to pull the image with the toolchain components of the Red Hat Developer Toolset from the Red Hat Atomic Registry, use the following command:
7.4.3. Loading an Image from a File Copy linkLink copied to clipboard!
Run the docker load < file.tar
command to load an image from a local file file.tar
. By default, the docker pull
command loads image data from the standard input. To load an image from a file, use the --input
or -i
parameter. For example to load the Red Hat Developer Toolset toolchain image that was previously saved to a tar file using the docker save
command, use the following command:
docker load -i devtoolset.tar
~]$ docker load -i devtoolset.tar
7.5. Using Containers Copy linkLink copied to clipboard!
Use the following commands to launch, stop, or remove containers or to run applications from within containers.
7.5.1. Launching a New Container and Running a Command Copy linkLink copied to clipboard!
Run the docker run <image> <command>
command to launch a new container from an image and run the specified command. Use the --name
parameter to specify a name for the container to prevent the Docker service from assigning a random name to the container. For example, to run the uname -a
command (and display its output) in a container created from the Red Hat Developer Toolset toolchain image, use the following command:
7.5.2. Stopping a Container Copy linkLink copied to clipboard!
Run the docker stop <container>
command to stop one or more containers. This action tries to gracefully shut down the applications running in the container by sending them the SIGTERM
signal (followed by the SIGKILL
signal if SIGTERM
does not work). For example, to stop the container created in the previous example, use the following command:
docker stop devtoolset docker ps | grep devtoolset
~]$ docker stop devtoolset
devtoolset
~]$ docker ps | grep devtoolset
7.5.3. Starting an Existing Container Copy linkLink copied to clipboard!
Use the docker start -i <container>
command to run the container stopped in the previous example (the --interactive
or -i
parameter ensures that the container’s standard output is attached to your current shell — in other words, it ensures that the output of the command executed in the container is displayed):
docker start -i devtoolset
~]$ docker start -i devtoolset
Linux 22b819dec3f1 3.10.0-327.el7.x86_64 #1 SMP Thu Oct 29 17:29:29 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
7.5.4. Launching a New Container and Switching to the Container’s Shell Copy linkLink copied to clipboard!
Run the docker run -ti <image> bash
command to launch a new container from an image and switch to an interactive shell within the container. The --interactive
or -i
parameter along with the --tty
or -t
parameter are used to allocate a pseudo-TTY within which the Bash shell is run. For example:
7.5.5. Removing a Container Copy linkLink copied to clipboard!
Run the docker rm <container>
command to remove one or more containers, thus freeing the host’s resources. For example, to remove the container created in the previous example, use the following command:
docker rm devtoolset docker ps -a | grep devtoolset
~]$ docker rm devtoolset
devtoolset
~]$ docker ps -a | grep devtoolset
7.6. Additional Resources Copy linkLink copied to clipboard!
-
See the
COMMANDS
section of thedocker(1)
manual page for a complete list of availabledocker
commands and detailed descriptions of their function. - See the Getting Started with Containers guide for detailed information about the use and management of containers on the Red Hat Enterprise Linux and Red Hat Atomic platforms.