Skip to main content

Using Docker on CentOS 7

CentOS 7 is a great distribution of Linux for servers and development systems. With micro-services, one potentially useful tool you may want to use is Docker containers, however this doesn’t come built-in on CentOS. This short tutorial will show you how to install Docker and use common comments.

Adding the Docker repository

The first thing to do is to add the Docker repository with this command:

yum install yum-utils
yum-config-manager --add-repo

Installing Docker

Next you can install the Docker Community Edition from the repo:

yum install docker-ce

Note that if you get a conflict with the older version of Docker which is present on the CentOS extras repository, you may need to uninstall that first, along with doing software updates:

yum update
yum remove docker docker-common

Once installed, you can start and enable the daemon:

systemctl start docker
systemctl enable docker

Finally, check the version installed:

docker version

Listing images

You can get a list of images with this command:

docker image ls

Note that you need to setup a registry (either public or private) such as from which you can pull or push images. You can then login to the hub from the command line:

docker login

Also note that if you’re behind a web proxy you need to do the following steps:

mkdir /etc/systemd/system/docker.service.d
nano /etc/systemd/system/docker.service.d/http-proxy.conf

And put this content in the file:

Environment="HTTPS_PROXY=" "NO_PROXY=localhost,centos,*"

Starting a container

Here we’ll cover how to install Jenkins as a Docker container and start it. First, to pull the latest image type the following:

docker pull jenkins/jenkins

Then, you can create a container and run it. This command will create a local container using the image we pulled, give it the name jenkins1, map two ports from the local container to the outside host, and map an internal folder to the local filsystem at /my_data/jenkins/:

docker run -d --name jenkins1 -p 8080:8080 -p 50000:50000 -v /my_data/jenkins:/var/jenkins_home jenkins/jenkins

Here is a list of arguments that may be useful for the run command:

  • -p 8080:80 - map port 80 in the container to port 8080 on the host
  • -v /files:/var/lib/mysql - map /var/lib/mysql in the container to /files on the host
  • -e HOME=”/home/pi” - set the environment variable HOME in the container
  • -net mynet - launch the container in the mynet network

Then, you can start and stop the container using these commands:

docker start jenkins1
docker stop jenkins1

You can also view which containers are running with this:

docker ps

You can also connect to your container and open a shell like this:

docker exec -u 0 -it jenkins1 /bin/bash

Lastly, remember that every container you create requires disk space. If you want to remove all stopped containers, you can prune them:

docker container prune