Install Docker on CentOS 7 and Run Containers

Install Docker on CentOS 7 and Run Containers

In this tutorial we are going to learn how to Install Docker on CentOS 7. We will also create our first Docker container on CentOS 7.

We will install the docker engine available from the CentOS Base repository. To install docker, Open the terminal and type:

yum install docker docker-registry

After the installation is finished, start the docker service and enable to start docker automatically when system reboot.

systemctl start docker.service
systemctl enable docker.service

To verify CentOS 7 Docker Installation, Type:

docker version

You should see the output something like the following:

Client:
 Version:         1.12.6
 API version:     1.24
 Package version: docker-1.12.6-28.git1398f24.el7.centos.x86_64
 Go version:      go1.7.4
 Git commit:      1398f24/1.12.6
 Built:           Fri May 26 17:28:18 2017
 OS/Arch:         linux/amd64
 
Server:
 Version:         1.12.6
 API version:     1.24
 Package version: docker-1.12.6-28.git1398f24.el7.centos.x86_64
 Go version:      go1.7.4
 Git commit:      1398f24/1.12.6
 Built:           Fri May 26 17:28:18 2017
 OS/Arch:         linux/amd64

Create your first docker container on CentOS 7

For this example, I will run the Apache HTTPD Web Server on CentOS Docker Engine.

First, Download the latest Apache httpd image using the 'docker pull' command.

docker pull httpd:latest

Then, Start a new Docker Container with the 'docker run' command.

docker run -d --name docker-httpd -p 80:80 httpd:latest

As per the above example, I started a new container called docker-httpd using the httpd image. I also mapped host port 80 to the port 80 of the new container.

We can get an interactive shell session to the container using the 'docker exec' command.

docker exec -ti docker-httpd /bin/bash

List Containers on CentOS 7

To list running containers on CentOS Docker engine, Type:

docker ps

To list all containers, Type:

docker ps -a

Conclusion

So we have successfully installed Docker on CentOS 7 and created our first docker container to run Apache Web Server.