Last update: 2019-11-26

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:

<bash>
yum install yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Installing Docker

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

<bash>
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:

<bash>
yum update
yum remove docker docker-common

Once installed, you can start and enable the daemon:

<bash>
systemctl start docker
systemctl enable docker

Finally, check the version installed:

<bash>
docker version

Listing images

You can get a list of images with this command:

<bash>
docker image ls

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

<bash>
docker login

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

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

And put this content in the file:

[Service]
Environment="HTTPS_PROXY=http://proxy.your-domain.com:8000/" "NO_PROXY=localhost,centos,*.your-domain.com"

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:

<bash>
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/:

<bash>
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:

<bash>
docker start jenkins1
docker stop jenkins1

You can also view which containers are running with this:

<bash>
docker ps

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

<bash>
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:

<bash>
docker container prune

Looking to expand into the cloud? See what a certified AWS administrator can do for you.


 
© 2007-2019 Patrick Lambert - All resources on this site are provided under the MIT License - You can contact me at: contact@dendory.net