Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. Docker containers wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries.
In a way, Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.
In a nutshell, Docker is an extension of Linux Containers (LXC) : a unique kind of lightweight, application-centric virtualization that drastically reduces overhead and makes it easier to deploy software on servers.
What is Docker for and for whom?
Docker is a tool that is designed to benefit both developers and system administrators, making it a part of many DevOps (development + operations) tool chains. For developers, it means that they can focus on writing code without worrying about the system that it will ultimately be running on. It also allows them to get a head start by using one of thousands of programs already designed to run in a Docker container as a part of their application. For operations staff, Docker gives flexibility and potentially reduces the number of systems needed because of its small footprint and lower overhead.
Install and Configure Docker
Following are the steps to install Docker;
Prerequisites:
- Linux kernel version 3.10 or higher
- CS Docker Engine version 1.12.1 or higher
- 2.00 GB of RAM
- 3.00 GB of available disk space
- A static IP address
The Docker installation package available in the official CentOS 7 repository may not be the latest version. To get the latest and greatest version, install Docker from the official Docker repository. The following command will update the yum repository to the latest available version.
yum makecache fast
The docker binaries are already present in the RHEL/CentOS 7 extras repositories. Install Docker package by issuing the following command with root privileges;
yum install docker
After the installation, start the docker daemon. To start the docker issue the following commands,
systemctl start docker
systemctl status docker
systemctl enable docker
As seen in the above screenshot, docker is set up and running fine.
Verify that docker is installed correctly by running the hello-world image.
docker run hello-world
To know the docker version, issue the command docker version
#docker version
Client: Version: 1.12.5 API version: 1.24 Package version: docker-common-1.12.5-14.el7.centos.x86_64 Go version: go1.7.4 Git commit: 047e51b/1.12.5 Built: Mon Jan 23 15:35:13 2017 OS/Arch: linux/amd64 Server: Version: 1.12.5 API version: 1.24 Package version: docker-common-1.12.5-14.el7.centos.x86_64 Go version: go1.7.4 Git commit: 047e51b/1.12.5 Built: Mon Jan 23 15:35:13 2017 OS/Arch: linux/amd64
To get system-wide information of docker, issue the command
docker info
#docker info
Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 4 Server Version: 1.12.5 Storage Driver: devicemapper Pool Name: docker-253:1-517410-pool Pool Blocksize: 65.54 kB Base Device Size: 10.74 GB Backing Filesystem: xfs Data file: /dev/loop0 Metadata file: /dev/loop1 Data Space Used: 8.285 GB Data Space Total: 107.4 GB Data Space Available: 21.82 GB Metadata Space Used: 8.507 MB Metadata Space Total: 2.147 GB Metadata Space Available: 2.139 GB Thin Pool Minimum Free Space: 10.74 GB Udev Sync Supported: true Deferred Removal Enabled: false Deferred Deletion Enabled: false Deferred Deleted Device Count: 0 Data loop file: /var/lib/docker/devicemapper/devicemapper/data WARNING: Usage of loopback devices is strongly discouraged for production use. Use `–storage-opt dm.thinpooldev` to specify a custom block storage device. Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata Library Version: 1.02.135-RHEL7 (2016-11-16) Logging Driver: journald Cgroup Driver: systemd
Issuing the command
docker
in the CLI, will list all the available Docker commands.
Docker makes use of containers to run different applications which is the major advantage of using a docker. A single application can be installed in a container and can be used from any other containers or from the host OS.
Downloading and creating a Docker image
To set up a Docker container, a docker image is to be downloaded. This image is available in the Docker hub on the host.
To install CentOS search for the corresponding image using the following command
docker search centos
The above command will list all the available images for the search keyword. Select the keyword and download the corresponding image using the following command,
docker pull centos
The above screenshot shows the ‘Pull complete
’ message which resembles the CentOS image has been downloaded.
To list the available docker images, issue the following command,
docker images
To remove a docker image, use the command docker rmi CONTAINER ID
. For example,
docker rmi centos
Running a Docker Container
Running a docker container is essential to get a piece of software to run in the container. This can be accomplished by the docker run
command.
docker run centos cat /etc/redhat-issue
The above command will return the CentOS version installed in the container to the host.
To run one of the containers again with the command that was executed to create it, first you must get the container ID (or the name automatically generated by Docker) by issuing the below command, which displays a list of the running and stopped (non-running) containers:
docker ps -l
Once the container ID has been obtained, start the container by,
docker start 8347c44a3cea
To stop the container issue the command docker stop CONTAINER ID
To run an interactive shell in the container you should run the following command,
docker run -it centos bash
To exit to host machine’s command line, Press Ctrl+P, Ctrl+Q.
To reconnect to the running container you need the container ID
or name
. Issue docker ps
command to get the ID
or name
.
Once the container ID is found, get into the container using docker attach
, say for example
docker attach 8347c44a3cea
This is all about running a docker container. Once a docker container has been set up, it is essential that, the applications should run from within the container.
To know how to install an nginx web server in a docker container and load balance it through Docker Swarm please click here.