Docker is software platform used to run applications in isolated containers, similar to virtual machines. It helps in the easy deployment, testing and shipping of applications or codes. Docker containers are resource-friendly, portable and secure.
In this tutorial, we will show you how Docker can be installed and used on a server running CentOS 7.
Prerequisites
- 64 bit CentOS 7 OS
- Kernel version equal to or greater than 3.10
Installation
Docker can be installed with yum utility or directly from the installation script. It is recommended to install it with their installation script because the Docker installation package might not be the latest in CentOS’ official repo.
To install it with the script, just download the package from the docker official website with curl and run it. For this to work, curl needs to be installed in the server first.
To install curl, run:
1 sudo yum install curl
If curl is installed, just run:
1 curl -fsSL https://get.docker.com/ | sh
This will install latest version of Docker. This screen marks the completion of installation:
Now, start the Docker daemon.
1 sudo systemctl start docker
Status of Docker service can be verified with:
1 sudo systemctl status docker
Here is how it looks like if the service is active.
There you go! Docker client or Command line utility is now available. You will be able to manage Docker containers with Docker command now. Let’s see how it can be executed.
Executing Docker Commands
By default, executing Docker command requires root or sudo privilege. To execute Docker command as a normal user, it is required to add that user to the docker group. This can be done with:
1 sudo usermod -aG docker username
Syntax of Docker command is as follows:
1 docker [option] [command] [arguments]
To see the subcommands:
1 docker
To see the available switches of a subcommand:
1 docker docker-subcommand --help
How to run a Docker container with these commands can be seen in the coming sections.
Docker Images and pulling images from Docker Hub
Before anything else, let’s clarify what a Docker image is. Instances of Docker images are called Docker containers. An image consists of a set of layer. If you start a particular image, you’ll get a running container of this image. Images are therefore the basis of containers.
By default, images are pulled from a repository provided by Docker called Docker Hub. You can see if a particular image is available in Docker hub with:
1 docker search centos
Here is a screenshot of the output.
If the image exists in the hub, you can download it with:
1 docker pull centos
Check out the screenshot for a better picture.
To list all the images downloaded to your workstation:
1 docker images
The output will look similar to this:
Running Docker container from Images
As discussed previously, Docker containers are instances of these images. When you run an image, you can see a running Docker container.
Docker images can be run with:
1 docker run image_name
Replace ‘image_name‘ with the name of an actual image. For example:
1 docker run centos
If you need an interactive shell:
1 docker run -it centos
With an interactive shell, you can work inside the container, which is similar to working inside a VPS. Once you are logged into the container, you can see the container ID as shown in the example below.
1 [root@5f3a7bd2a62f /]#
You can create, delete and modify files inside these containers but the changes made are temporary. If you restart this container, all the changes made will be lost.
To commit changes in a container to an Image, exit from the container:
1 exit
Then commit the changes as shown in the example below:
1 docker commit -m "Installed cPanel" -a "VPSCheap" 59839a1b7de2 finid/centos-cpanel
Where:
- -m switch is used to specify the ‘Commit Message’,
- -a for the ‘Author Name’,
- 59839a1b7de2 is the container ID
- finid is the repository name
- centos-cpanel is the name of your updated Docker image.
You can also save this image on Docker hub. For this, you may follow this link.
To list the Docker containers:
1 docker ps
To start a container:
1 docker start Container-ID
To stop a container:
1 docker stop Container-ID
How to uninstall Docker Container
Find the Docker package you’ve installed:
12345 [root@test ~]# yum list installed | grep dockerRepodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fastdocker-engine.x86_64 1.12.0-1.el7.centos @docker-main-repodocker-engine-selinux.noarch 1.12.0-1.el7.centos @docker-main-repo[root@test ~]#
Remove the package with yum:
1 sudo yum -y remove docker-engine.x86_64
To delete all the images, containers and volume:
1 rm -rf /var/lib/docker
This will help you to get started with Docker. You can signup to their community forum and subscribe to blog for updating yourself with Docker.
Thanks for your time, cheers!






Comments