How to run Docker containers on CentOS or Fedora

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 yum install docker-io
$ sudo service docker start
$ sudo chkconfig docker on

To install Docker on Fedora 21 or earlier, use the following commands:

$ sudo yum install docker-io
$ sudo systemctl start docker.service
$ sudo systemctl enable docker.service

To install Docker on Fedora 22 or later, use the following commands:

$ sudo dnf install docker
$ 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:

$ sudo usermod -a -G docker $USER

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.

$ docker run -i -t ubuntu /bin/bash

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:

$ docker ps -a
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:

$ docker start [container-id]

To remove a stopped container:

$ docker rm [container-id]

To attach to a background-running container in order to view or interact with the container:

$ docker attach [container-id]

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:

$ docker commit [container-id] [new-image-name]

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.

$ docker pull [image name]

To view all locally downloaded/saved container images:

$ docker images

You can choose a specific image to boot a container from:

$ docker run -i -t [image-id] /bin/bash

To remove a container image from the local repository:

$ docker rmi [image-id]

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.

Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

4 thoughts on “How to run Docker containers on CentOS or Fedora

  1. Have you been able to figure out how to properly do a Ethernet bridge? I want my containers to have an IP address on the same network as the host server/computer. This is great by the way!

    • You can add the Ethernet interface of host computer to the docker bridge docker0. Then your container will get an IP address on the same network as the host. One problem though: since Docker allocates IP addresses in its own way, to avoid IP conflicts, you will have to assign (unused) IP addresses statically to the containers, or allocate docker0 bridge an unused IP subnet (among the entire DHCP ranges).

  2. Hi,
    Do you know if there are any plans to integrate a dhclient program so that docker can pick up an IP automatically? also, is there an actual mac address that can be given out to a dhcp server via dhcpdiscover?

    Thanks for the above tutorial it was great!

Leave a comment

Your email address will not be published. Required fields are marked *