How to run Docker containers on CentOS or Fedora

Last updated on September 23, 2020 by Dan Nanni

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 repo, 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 more advanced and user-friendly Docker networking tools such as Weave.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean