docker

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

  1. 64 bit CentOS 7 OS
  2. 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:

sudo yum install curl

If curl is installed, just run:

curl -fsSL https://get.docker.com/ | sh

This will install latest version of Docker. This screen marks the completion of installation:

docke2

Now, start the Docker daemon.

sudo systemctl start docker

Status of Docker service can be verified with:

sudo systemctl status docker

Here is how it looks like if the service is active.

docker1

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:

sudo usermod -aG docker username

Syntax of Docker command is as follows:

docker [option] [command] [arguments]

To see the subcommands:

docker

To see the available switches of a subcommand:

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:

docker search centos

Here is a screenshot of the output.

doc3

If the image exists in the hub, you can download it with:

docker pull centos

Check out the screenshot for a better picture.

doc4

To list all the images downloaded to your workstation:

docker images

The output will look similar to this:

doc5

 

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:

docker run image_name

Replace ‘image_name‘ with the name of an actual image. For example:

docker run centos

If you need an interactive shell:

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.

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

exit

Then commit the changes as shown in the example below:

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:

docker ps

To start a container:

docker start Container-ID

To stop a container:

docker stop Container-ID

 

How to uninstall Docker Container

Find the Docker package you’ve installed:

[root@test ~]# yum list installed | grep docker
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
docker-engine.x86_64               1.12.0-1.el7.centos   @docker-main-repo
docker-engine-selinux.noarch       1.12.0-1.el7.centos   @docker-main-repo
[root@test ~]#

Remove the package with yum:

sudo yum -y remove docker-engine.x86_64

To delete all the images, containers and volume:

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!

Facebooktwittergoogle_plusredditpinterestlinkedinmail