How to install and use docker and containers on centos 7


In this article, we will learn how to install Dockers. Dockers is a very good tool which runs the application on its own containers. It works like a virtual machine which are more portable and more resource friendly and more dependent on the host OS. There are two methods of installing Docker on CentOS 7. First method is to by installation using the existing OS or spinning the server with a tool called Docker Machine and making the auto installation.

Here for demo purpose, we will install on the existing OS.

Installing the Docker on CentOS 7

Before we begin the installation we need CentOS 7 64 bit and need a user who is non root with sudo permissions. Docker installation is available in the official CentOS 7 repository which is not the latest version. In order to get the latest version, we will install the Dockers from the Dockers Official repository.

$ sudo yum check-update

Now run the below command to add the official Docker repository and download the latest version of the dockers and install it.

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

Remember that you will have to log out and back in for this to take effect!

After the installation has completed, we need to start the Docker daemon with the below command

$ sudo systemctl start docker

To know the docker services running or not we can run the below command

$ sudo systemctl status docker

We needed to enable the docker to start at the boot time, we needed to run the below command

$ sudo systemctl enable docker

Adding the Current user to Docker Group

We needed to add the current user to the Docker group on the system which was enabled by the installation without that we cannot run the docker command line utility with non root user.

$ sudo usermod –aG docker $(whoami)

Or

$ sudo usermond –aG docker username

Using the Docker Command

As we just installed the Docker installed and working, we needed to learn some command line utility with some sub-command and arguments

$ docker [option] [command] [arguments]

To view all the sub-commands we needed to type the below command

$ docker
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@centos-linux-1 ~]# docker
Usage: docker [OPTIONS] COMMAND [arg...]
docker daemon [ --help | ... ]
docker [ --help | -v | --version ]
A self-sufficient runtime for containers.
Options:
--config=~/.docker Location of client config files
-D, --debug Enable debug mode
-H, --host=[] Daemon socket(s) to connect to
-h, --help Print usage
-l, --log-level=info Set the logging level
--tls Use TLS; implied by --tlsverify
--tlscacert=~/.docker/ca.pem Trust certs signed only by this CA
--tlscert=~/.docker/cert.pem Path to TLS certificate file
--tlskey=~/.docker/key.pem Path to TLS key file
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Sub Commands:
attach   Attach to a running container
build    Build an image from a Dockerfile
commit   Create a new image from a container's changes
cp       Copy files/folders between a container and the local filesystem
create   Create a new container
diff     Inspect changes on a container's filesystem
events   Get real time events from the server
exec     Run a command in a running container
export   Export a container's filesystem as a tar archive
history  Show the history of an image
images   List images
import   Import the contents from a tarball to create a filesystem image
info     Display system-wide information
inspect  Return low-level information on a container or image
kill     Kill a running container
load     Load an image from a tar archive or STDIN
login    Log in to a Docker registry
logout   Log out from a Docker registry
logs     Fetch the logs of a container
network  Manage Docker networks
pause    Pause all processes within a container
port     List port mappings or a specific mapping for the CONTAINER
ps       List containers
pull     Pull an image or a repository from a registry
push     Push an image or a repository to a registry
rename   Rename a container
restart  Restart a container
rm       Remove one or more containers
rmi      Remove one or more images
run      Run a command in a new container
save     Save one or more images to a tar archive
search   Search the Docker Hub for images
start    Start one or more stopped containers
stats    Display a live stream of container(s) resource usage statistics
stop     Stop a running container
tag      Tag an image into a repository
top      Display the running processes of a container
unpause  Unpause all processes within a container
update   Update configuration of one or more containers
version  Show the Docker version information
volume   Manage Docker volumes
wait     Block until a container stops, then print its exit code

Working with Docker Images

Docker images are used to run the Docker containers which pulls the images from Dockers Hub.

To check whether we have access to the Dockers Hub, run the below command

$ docker run hello-world

We can search for the images available on the Docker Hub by using the docker command with sub-command search for example we will search for CentOS images, below is the command to search for CentOS image on Docker Hub.

[root@centos-linux-1 ~]# docker search centos
NAME                             DESCRIPTION                                      STARS     OFFICIAL  AUTOMATED
centos                            The official build of CentOS.                   2417         [OK]
ansible/centos7-ansible           Ansible on Centos7                               80                   [OK]
jdeathe/centos-ssh                CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8...    26                   [OK]
nimmis/java-centos                This is docker images of CentOS 7 with dif...    13                   [OK]
million12/centos-supervisor       Base CentOS-7 with supervisord launcher, h...    12                   [OK]
consol/centos-xfce-vnc            Centos container with "headless" VNC sessi...    10                   [OK]
torusware/speedus-centos          Always updated official CentOS docker imag...     8                   [OK]
jdeathe/centos-ssh-mysql          CentOS-6 6.7 x86_64 / MySQL. 7                                        [OK]
nickistre/centos-lamp             LAMP on centos setup 4                                                [OK]
centos/mariadb55-centos7 3 [OK]
nathonfowlie/centos-jre           Latest CentOS image with the JRE pre-insta...     3                   [OK]
consol/sakuli-centos-xfce         Sakuli end-2-end testing and monitoring co...     2                   [OK]
blacklabelops/centos              CentOS Base Image! Built and Updates Daily!       1                   [OK]
darksheer/centos Base             Centos Image -- Updated hourly                    1                   [OK]
timhughes/centos                  Centos with systemd installed and running         1                   [OK]
grayzone/centos                   auto build for centos.                            0                   [OK]
ustclug/centos                    USTC centos                                       0                   [OK]
januswel/centos yum update-ed     CentOS image                                      0                   [OK]
kz8s/centos Official              CentOS plus epel-release                          0                   [OK]
grossws/centos                    CentOS 6 and 7 base images with gosu and l...     0                   [OK]
harisekhon/centos-scala Scala +   CentOS (OpenJDK tags 2.10-jre7 - 2...             0                   [OK]
jsmigel/centos-epel               Docker base image of CentOS w/ EPEL installed     0                   [OK]
ericuni/centos                    centos dev                                        0                   [OK]
dmglab/centos                     CentOS with some extras - This is for the ...     0                   [OK]
repositoryjp/centos               Docker Image for CentOS.                          0                   [OK]

To Download the Docker Image to Locally

We can download the images from Dockers Hub, below is the command to download a docker images using pull command

$ docker pull centos
docker pull centos
Using default tag: latest
latest: Pulling from library/centos
0653bff3c5cf: Extracting 29.52 MB/70.6 MB
Message from syslogd@centos-linux-1 at Jul 11 08:06:44 ...
0653bff3c5cf: Pull complete
Digest: sha256:81e4f2f663eaa1bf46ff9be348396dd7053734b257ef4147d7133d6f25bbf7cf
Status: Downloaded newer image for centos:latest

We can use run sub-command to run the image which we downloaded

$ docker run centos

And to see the images downloaded to our computer with below command

$ docker images
docker images
REPOSITORY      TAG          IMAGE ID           CREATED        SIZE
centos          latest       05188b417f30      9 days ago      196.8 MB
hello-world     latest       c54a2cc56cbb      9 days ago      1.848 kB

Running the Docker Container

As we have downloaded a Container images to the local system, we will run the container using the latest image of CentOS with –i and –t switches which gives the interactive shell to the container

$ docker run –it centos
[root@0aac55bf786f /]#

The prompt will be changes to the container id “0aac55bf786f” is the id of the container which we downloaded

We can install some software on the container using the below command we will install mysql server on the container

[root@0aac55bf786f /]# yum install mysql-server

After install the mysql on the container we can commit the changes to the Dockers hub with the below command with –m switch which uses to commit message and –a for adding author name

General Syntax
$ docker commit -m "What did you do to the image" -a "Author Name" container-id repository/new_image_name
Example
$ docker commit -m "added MySQL-server" -a "Chandra Prakash" 0aac55bf786f finid/centos-mysql_db
sha256:77993fb3b476e02324b8bf7ebfebe1e792b93df2eecc70208997ac95455c65e8

To Confirm the Image has submitted and listed in the local repository, you can run the below command.

$ docker images
REPOSITORY               TAG           IMAGE ID        CREATED            SIZE
finid/centos-mysql_db    latest       77993fb3b476     28 seconds ago     268.4 MB
centos                   latest       05188b417f30     9 days ago         196.8 MB
hello-world              latest       c54a2cc56cbb     9 days ago         1.848 kB

After the setup and configuration we have learned the basis of the Dockers and its Containers and who to pull the images and use them and install some softwares, with Dockers we can do a whole lot and more, in further articles we will learn more.

Updated on: 18-Oct-2019

708 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements