Docker application is used to manage application processes in container. Using docker you can build, test and deploy applications that can run anywhere as portable and self-sufficient containers. In this tutorial, we will learn how to install Docker on CentOS 7.
Before continuing with this tutorial, make sure you are logged in as a user account with sudo privileges.
Install Docker on CentOS
Docker package is available in the official CentOS 7 repository but it may not always be the latest version. So We will install latest Docker package on CentOS system from the Docker’s repositories. Following are the steps to install docker on CentOS.
At first, update the packages list using below command:
sudo yum update
We also need to install dependencies which are required to enable Docker repository.
sudo yum install yum-utils device-mapper-persistent-data lvm2
After that, add Docker repository to your system’s software repository list using below command:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Now Docker repository is enabled so Install latest version of Docker Community Edition using yum as given below:
sudo yum install docker-ce
Once the installation completed we need to start docker service. We will also enabl service so it will auto start on system boot:
sudo systemctl start docker sudo systemctl enable docker
You can check status of docker service using below command:
sudo systemctl status docker
Output ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2019-07-05 08:36:43 EDT; 12min ago Docs: https://docs.docker.com Main PID: 4485 (dockerd) Tasks: 8 CGroup: /system.slice/docker.service └─4485 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
You also can check the version of Docker using below command:
Docker version 18.09.7, build 2d0083d
Executing docker command without sudo
administrator privileges to run. If you want to run docker command without
sudo then you need to add your user to
docker group. The docker group is created at the installation of docker CE package.
You should run below command to add user to docker group:
sudo usermod -aG docker $USER
After that, Log out and log back in to apply membership.
Run the below command to verify that you have added docker group :
tecnstuff sudo docker
How to Use Docker Command
Below is the basic syntax for the
docker [option] [subcommand] [arguments]
You can get list of all docker subcommands by issuing below command:
If you need any help about subcommand then you can use following command:
docker docker-subcommand --help
Working with Docker Images
Docker images are as a snapshot of a Docker container. Docker containers are built from Docker Images and these images are available on Docker Hub. It is a cloud-based registry service which managed by Docker Company.
Search Docker Images
Use search sub command to search an image on Docker Hub registry.
For example, you can search for an CentOS image using below command:
docker search centos
NAME DESCRIPTION NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 2224 [OK] jdeathe/centos-ssh CentOS-6 6.7 x86_64 / CentOS-7 7.2.1511 x8... 22 [OK] jdeathe/centos-ssh-apache-php CentOS-6 6.7 x86_64 / Apache / PHP / PHP M... 17 [OK] million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 11 [OK] nimmis/java-centos This is docker images of CentOS 7 with dif... 10 [OK] torusware/speedus-centos Always updated official CentOS docker imag... 8 [OK] nickistre/centos-lamp LAMP on centos setup 3 [OK] ...
You can see above all the images with versions. In addition, if the version is not specified then docker picks latest version which is available.
Download Docker Images
You can download official build of image using
pull command along with docker command. For example you would like to download CentOS 7 image, then you should issue below command:
docker image pull centos
It will show output as below:
Output Using default tag: latest latest: Pulling from library/centos 6b98dfc16071: Pull complete 4001a1209541: Pull complete 6319fc68c576: Pull complete b24603670dc3: Pull complete 97f170c87c6f: Pull complete Digest: sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d Status: Downloaded newer image for centos:latest
You can list all downloaded images by typing:
docker image ls
Remove Docker Image
If you want to remove downloaded docker image then you can remove using below command:
docker image rm centos
Above command will remove ubuntu image from your system.
Docker container is an instance of Docker Image. A container represents a runtime for a single application, process, or service. In simple way we can say Docker image is a class and Docker container as an instance of a class. Using
docker container command we can manage operations on a container.
Start Docker Container
Below command will start a Docker container which is an instance of an image. If you don’t have the image locally, it will download it first:
docker container run centos
In above command we have not provided any command so the container will bootup and ran an empty command and will exit at last.
You can interact with CentOS image using
-it switch. Run below command to start an interactive container:
docker container run -it centos /bin/bash
As you can see above the command prompt changed. Now you can execute command and interact with CentOS container directly.
List Docker Containers
You can get list of all active docker containers using below command:
docker container ls
It will list all the active containers and if no one container is running then output will be empty.
Also you can list active and inactive containers issuing below command:
docker container ls -a
Remove Docker Containers
You can remove docker containers using container id. So just copy container id and put it after container rm in below command:
docker container rm r70023ag870r
You have learned how to install and use of docker on CentOS 7 system. Also you learned how to download Docker images and manage Docker containers. To learn more about Docker check out the official Docker documentation.
If you have any question or suggestion, please leave a comment below.