Welcome to the second article in our series of articles about container technologies in Fedora! This article will deal with Docker, a tool used to automate containerization of software using features of the Linux kernel.
What is Docker?
Docker is a high level set of tools that allows multiple isolated applications to run on a same host platform. Unlike other containerization software we talk about in this series, Docker is meant to run a system with a single process. In addition to providing these tools, Docker provides a “hub” for containers created by others.
Creating the container
Creating a container with Docker is extremely easy. We will be using the Docker command line tool to set up a Fedora 24 container named “fedora-container”. You’ll need to install Docker, if you don’t already have it, with the usual
sudo dnf install docker.
Before it is possible to run a container, you will need to start the Docker daemon. To do this, run
systemctl start docker.service. You can then run a container for Fedora using the command
sudo docker run --name=fedora-container -it fedora bash, which will download an image for a basic Fedora container from Docker Hub.
Using the container
Docker is designed to run one process per container, but you can run more if you want to.
sudo docker exec -it fedora-container bash would run
bash in your container alongside the process you had originally started. You can replace
bash with any command you’d like to run in the container.
The network will already be available, and you can use it to install packages using the standard DNF commands. You can stop the container either by stopping the Docker daemon, with
systemctl stop docker, or with
docker stop fedora-container. You can then start the container again when you need it with
docker start fedora-container. It is also possible to temporarily pause execution of a container, using
docker pause fedora-container.
Particularly useful is the
docker cp command, which you can use to copy files from and to the container. The command
docker cp fedora-container:/root/source target will copy the file source from your home folder on the container to the name ‘target’ in the current folder, while
docker cp source fedora-container:/root/target will copy in the reverse direction.
You can use images for other systems than Fedora by pulling images from the Docker Image Library. However, if you want to create and run applications or services using Docker, you will want to create your own images. Docker is able to create images automatically if you tell it how to in a Dockerfile. A Dockerfile contains the instructions and commands that will assemble the image you want to run in a container. The Dockerfile reference explains the format.
Fedora packages many example Dockerfiles that you can use to learn about Docker images and to create your own. You can install the Dockerfiles with
sudo dnf install fedora-dockerfiles; they will be put in
/usr/share/fedora-dockerfiles/. The Fedora Developer Portal has additional useful information about Docker in Fedora.