Installing and configuring Docker Engine and Docker Compose on CentOS

Installing & Configuring Docker Engine on CentOS
Installing & Configuring Docker Engine & Docker-compose on CentOS. Source: pixabay 


I have written this post as a quick reference guide for installing and configuring Docker Engine and Docker compose on CentOS servers. Knowing the basics of Docker containers helps you focus on the end goal of solving problems, than spending your energy on other less important aspects. If you have experimented with multiple packages and applications, you might be knowing that, Docker containers makes it really easy to install and running softwares without worrying about dependencies, scripts and configurations, etc. Also, when we are building and releasing our solutions, it is important to let others consume it in simple process, Docker images helps you achieve that. I will be covering below topics in this article: 
  • Supported CentOS Versions
  • Setting up Docker Repository
  • Installing Docker Engine
  • Starting Docker Service
  • Verifying Docker Service
  • Enabling auto-start for Docker Engine on boot
  • Changing the data storage directory (Optional)
  • Uninstalling Docker Engine (Optional)
  • Installing Docker Compose
  • Example docker commands
  • Example docker-compose commands

Supported  CentOS Versions

Docker Engine supports CentOS 7 & 8, in this article we will install on CentOS 7, and it will be similar for CentOS 8 as well.

Setting up Docker Repository

When you are installing Docker Engine for the first time in a CentOS machine, need to add Docker Repository. This is a one time activity, if you uninstall Docker Engine for some reason, not required to remove this repo and can be used to install Docker Engine next time from the same repo.


sudo yum install -y yum-utils
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo


Installing Docker Engine

Run the below command to install the Docker engine. This will install the latest version of Docker Engine and containerd packages.
sudo yum install docker-ce docker-ce-cli containerd.io


Starting Docker Service

After installing Docker Engine, it will not be started automatically, to start the Docker Engine run the below command

sudo systemctl start docker

Verifying Docker Installation

You can verify the Docker Engine installation by running hello-word image as shown below

sudo docker run hello-world
This will display welcome image and information on successful installation of Docker engine.

Enabling auto-start for Docker Engine on boot

To configure Docker engine and containerd service to start automatically at system boot, run the below commands:

sudo systemctl enable docker.service
sudo systemctl enable containerd.service

Changing the data storage directory

If you need to change the default image installation directory, follow the below steps:

Create or modify the Docker configuration file: /etc/docker/daemon.json and update/add the "data-root" value as shown in the example below

{
    "data-root": "/new/path/to/docker-data"
}
After making the changes, restart the Docker engine service.

sudo systemctl daemon-reload

sudo systemctl restart docker 

Please note, this steps are for new Docker engine configuration, if you are already running docker container images, need to stop the Docker service, change the configuration, move the existing images and start the service to take effect - we are not going to discuss about detailed steps for now.

Uninstalling Docker Engine (Optional)

To uninstall Docker engine, Command Line Interface and Containerd packages run the below command:

sudo yum remove docker-ce docker-ce-cli containerd.io

Images, containers, volumes and customized configuration files on your machines are not removed automatically, run the below command to do that:

sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

Note, if you have changed the storage path of Docker image installation directory, as seen in previous steps, empty the directory manually to remove the contents.

Installing Docker Compose

Docker compose used to defining and running multiple container services required for your application. All application service configurations specified in the  docker-compose.yml  file. With single command you can start and run all the container services from the configuration file.

Docker compose commands provides options manage the entire application lifecycle, such as starting, stopping, rebuilding services, status of the services and streaming running service logs, etc.

Most simplest way to install Docker compose is using   pip  installer (I am extensively using Python, so it is the easiest option for me to manage through virtual environment). Run the below command to install  docker-compose 

pip install docker-compose

Example docker commands

Let's try some of the docker command examples:

docker <command>
Example: docker-version
version	Show the Docker version
top	Display the running processes
start   Start containers
stop	Stop containers
stats	Display resource usage statistics
service	Manage services
rm	Remove containers
restart	Restart containers
ps	List containers
kill	Kill running containers
images	List images
pull	Pull an image or a repository from a registry
push	Push an image or a repository to a registry
logs	Fetch the logs of a container
build	Build an image from a Dockerfile

Example docker-compose commands

To get start quickly, let me add few example commands, this will be helpful, when you are dealing with Docker or containerization for the first time.

docker-compose <command>
Example: docker-compose upstart              Start services
  down               Stop and remove containers, networks, images, and volumes
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  ps                 List containers
  restart            Restart services
  rm                 Remove stopped containers
  start              Start services
  stop               Stop services
  top                Display the running processes
  up                 Create and start containers
  version            Show the Docker-Compose version information


I hope this article is helpful to you. Share in comment section, if you are facing any error or challenges in following the above steps.


0 thoughts:

Post a Comment