Lately Docker has emerged as a key technology for deploying applications in the cloud environment. Compared to traditional hardware virtualization, Docker-based container sandbox provides a number of advantages for application deployment environment, such as lightweight isolation, deployment portability, ease of maintenance, etc. Now Red Hat is steering community efforts in streamlining the management and deployment of Docker containers.
Not only for the cloud environment, Docker can also be quite useful for end users, especially when you want to test out particular software under a specific Linux environment. You can easily spin up a Docker container for the target environment, install and test the software in it, and then throw away the container once you are done. The whole process from beginning to end is quite efficient, and you can avoid messing up your end system all along.
In this tutorial, I am going to describe how to create and manage Docker containers on CentOS or Fedora. Note that Docker is supported only on 64-bit host systems at this time. If you want to try out Docker on Ubuntu, refer to this tutorial.
Install Docker on CentOS or Fedora
To install Docker on CentOS, first enable EPEL repository, and then use yum command:
$ sudo service docker start
$ sudo chkconfig docker on
To install Docker on Fedora, use the following commands:
$ sudo systemctl start docker.service
$ sudo systemctl enable docker.service
After installing Docker on CentOS or Fedora, you need to add yourself to docker group to be able to run Docker as a non-root user. Use this command for that:
Log out, and log back in to activate the group change.
At this point, you should be able to run docker command as a unprivileged user.
Basic Usage of Docker
To start a new Docker container, you need to decide which Docker image to use for the container. You can search the official Docker image index which lists publicly available Docker images. The Docker index includes Linux base images managed by Docker team (e.g., Ubuntu, Debian, Fedora, CentOS), as well as user-contributed custom images (e.g., MySQL, Redis, WordPress).
For example, to start a Ubuntu container in the interactive mode, run the following command. The last argument ‘/bin/bash’ is to be executed inside a container upon its launch.
The first time you run the above command, it will download available Ubuntu docker image(s) over networks, and then boot up a Docker container using the image. A Ubuntu container will boot up instantly, and you will see a console prompt inside the container. You can access a full-fledged Ubuntu operating system inside the container sandbox.
If you type ‘exit’ at the prompt, you will get out of the container, and it will be stopped.
To get a list of all containers (including stopped ones), run:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a08a0b2bb4c ubuntu:14.04 /bin/bash About a minute ago Exit 0 cocky_ritchie
To re-start a stopped container in daemon mode:
To remove a stopped container:
To attach to a background-running container in order to view or interact with the container:
You can freely customize a running container (e.g., installing new software). If you want to save the changes in the current container, first get out of the container’s interactive mode by typing “exit” at the prompt. Then save the changed image as a different image by using this command:
To get the container ID of your container, you can use “docker ps -a” command as described earlier.
Once you have created a new image like this, you can launch a new container off of this image.
You can also download any public container images (e.g., ubuntu, bowery/mysql) and store them in a local repository as follows.
To view all locally downloaded/saved container images:
You can choose a specific image to boot a container from:
To remove a container image from the local repository:
In this tutorial, I describe how to create standalone Docker containers in Red Hat based environment. On the other hand, more practical Docker use cases may involve interconnecting multiple Docker containers over network. For that, I strongly recommend you check out weave, a powerful Docker networking tool.