How to install and use Docker on CentOS 7

Introduction

Docker is an application that makes it easy to run application processes in a container, just like a virtual machine, but more portable, more resource-friendly, and more dependent on the host operating system.

There are two ways to install Docker on CentOS 7. One way is to install it on an existing installation of the operating system. The other involves starting the server with a tool called Docker Machine, which automatically installs Docker on it.

In this tutorial, you will learn how to install and use it in an existing CentOS 7 installation.

Preparation

**Note: ** Docker requires a 64-bit version of CentOS 7 and a kernel version equal to or greater than 3.10. The default 64-bit CentOS 7 Tencent Cloud CVM meets these requirements.

All commands in this tutorial should be run as a non-root user.

Step 1-Install Docker

The Docker installation package provided 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. This section will show you how to do this.

But first, let's update the package database:

sudo yum check-update

Now run this command. It will add the official Docker repository, download the latest version of Docker, and install it:

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

After the installation is complete, start the Docker daemon:

sudo systemctl start docker

Verify that it is running:

sudo systemctl status docker

The output should be similar to the following, indicating that the service is active and running:

● docker.service - Docker Application Container Engine
 Loaded:loaded(/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
 Active:active(running) since Sun 2016-05-0106:53:52 CDT;1 weeks 3 days ago
  Docs: https://docs.docker.com
 Main PID:749(docker)

Finally, make sure it starts every time the server restarts:

sudo systemctl enable docker

Installing Docker now can not only provide you with Docker service (daemon), but also provide you with a docker command line utility or Docker client. We will discuss how to use this command with docker later in this tutorial.

Step 2-Execute Docker commands without Sudo (optional)

By default, running the docker command requires root privileges-that is, you must add sudo before the command. It can also be run by a user in the docker group, which is automatically created during Docker installation. If you try to run the docker command without using sudo or without prefixing it in the docker group, you will get the following output:

docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.

If you want sudo to avoid typing when running the docker command, please add your username to the docker group:

sudo usermod -aG docker $(whoami)

You need to log out of the Droplet and return as the same user to enable this change.

If you need to add a user to a group where you are not logged in to docker, please use the following method to clearly declare the user name:

sudo usermod -aG docker username

The rest of this article assumes that you docker runs the command as a user in the docker user group. If you choose not to do this, please add the command sudo in front.

Step 3-Use Docker commands

With Docker installed and working, it's time to get familiar with the command line utilities. Using docker involves passing a series of options and subcommands, followed by parameters. The syntax takes the following form:

docker [option][command][arguments]

To view all available subcommands, type:

docker

Starting with Docker 1.11.1, the complete list of available subcommands includes:

​
 attach    Attach to a running container
 build     Build an image from a Dockerfile
 commit    Create a newimagefrom a container's changes
 cp        Copy files/folders between a container and the local filesystem
 create    Create a newcontainer
 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 newcontainer
 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 ofcontainer(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

To view the switches available for a specific command, type:

docker docker-subcommand --help

To view system-wide information, use:

docker info

Step 4-Use Docker Mirror

The Docker container runs from the Docker image. By default, it gets these images from Docker Hub, which is a Docker registry managed by Docker, the company behind the Docker project. Anyone can build and host their Docker images on Docker Hub, so most of the applications and Linux distributions needed to run Docker containers have images hosted on Docker Hub.

To check if you can access and download the image from Docker Hub, type:

docker run hello-world

The output should include the following and should indicate that Docker is working properly:

Hello from Docker.
This message shows that your installation appears to be working correctly....

You can use the search command with the docker command to search for images available on Docker Hub. For example, to search for CentOS mirrors, type:

docker search centos

This script will crawl Docker Hub and return a list of all mirrors whose names match the search string. In this case, the output will be similar to:

NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                          The official build of CentOS.2224[OK]       
jdeathe/centos-ssh              CentOS-66.7 x86_64 / CentOS-77.2.1511 x8...22[OK]
jdeathe/centos-ssh-apache-php   CentOS-66.7 x86_64 / Apache / PHP / PHP M...17[OK]
million12/centos-supervisor     Base CentOS-7with supervisord launcher, h...11[OK]
nimmis/java-centos              This is docker images of CentOS 7with dif...10[OK]
torusware/speedus-centos        Always updated official CentOS docker imag...8[OK]
nickistre/centos-lamp           LAMP on centos setup                            3[OK]
​
...

In the OFFICIAL column, OK represents the image built and supported by the company behind the project. Once you have determined the image you want to use, you can use the pull subcommand to download it to your computer as shown below:

docker pull centos

After downloading the image, you can run the container using the downloaded image with the run subcommand. If the image has not been downloaded when docker is executed with the run subcommand, the Docker client will download the image first, and then use it to run the container:

docker run centos

To view the image that has been downloaded to the computer, type:

docker images

The output should be similar to the following:

[ secondary_lable Output]
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              778a53015523        5 weeks ago         196.7 MB
hello-world         latest              94df4f0ce8a4        2 weeks ago         967 B

As you will see later in this tutorial, the image used to run the container can be modified and used to generate a new image, which can then be uploaded (push is a technical term) to Docker Hub or other Docker registry.

Step 5-Run the Docker container

After sending the test message, running the hello-world container in your previous step is an example of the container running and exiting. However, containers are more useful than this, they can be interactive. After all, they are similar to virtual machines, but more resource-friendly.

For example, let's run the container using the latest image of CentOS. The combination of -i and -t switches gives you interactive shell access to the container:

docker run -it centos

Your command prompt should change to reflect the fact that you are now working in the container, and should take the following form:

[ root@59839a1b7de2 /]#

**Important: **Please note the container ID in the command prompt. In the example above, it is 59839a1b7de2.

Now you can run any command inside the container. For example, let us install the MariaDB server in a running container. There is no need to prefix any commands with sudo, because you are operating in a container with root privileges:

yum install mariadb-server

Step 6-Commit the changes in the container to the Docker image

When you start the Docker image, you can create, modify and delete files just like using a virtual machine. The changes you made only apply to this container. You can start and stop it, but once you destroy it with the docker rm command, the changes will be lost forever.

This section describes how to save the state of the container as a new Docker image.

After installing MariaDB server in the CentOS container, you now have a container that runs the image, but the container is different from the image you used to create it.

To save the state of the container as a new image, please exit from it first:

exit

Then use the following command to commit the changes to the new Docker image instance. The -m switch is the submission information, which can help you and others know the changes you made, while the -a is used to specify the author. When you start an interactive docker session, the container ID is the container ID you mentioned earlier in this tutorial. Unless you have created other repositories on Docker Hub, the repository is usually your Docker Hub username:

docker commit -m "What did you do to the image"-a "Author Name" container-id repository/new_image_name

E.g:

docker commit -m "added mariadb-server"-a "Sunday Ogwu-Chinuwa" 59839a1b7de2 finid/centos-mariadb

**Note: **When you submit the image, the new image is saved locally, that is, on your computer. Later in this tutorial, you will learn how to push an image to a Docker registry such as Docker Hub so that you and others can evaluate and use it.

After completing the operation, immediately listing the Docker image on the computer should show the new image and the old image derived from it:

docker images

The output should look like this:

REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
finid/centos-mariadb   latest              23390430ec73        6 seconds ago       424.6 MB
centos                 latest              778a53015523        5 weeks ago         196.7 MB
hello-world            latest              94df4f0ce8a4        2 weeks ago         967 B

In the above example, centos-mariadb is the new image, which comes from the existing CentOS image of Docker Hub. The size difference reflects the changes made. In this example, the change is that MariaDB server is installed. Therefore, next time you need to run a container with CentOS pre-installed with MariaDB server, you can use the new image. The image can also be built from a so-called Dockerfile. But this is a very complicated process that is beyond the scope of this article. We will explore this in a future article.

Step 7-List Docker containers

After using Docker for a while, you will have many active (running) and inactive containers on your computer. To view the active ones, use:

docker ps

You will see output similar to the following:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f7c79cc556dd        centos              "/bin/bash"3 hours ago         Up 3 hours                              silly_spence

To see all containers-active and inactive, pass the -a switch to it:

docker ps -a

To see the latest container you created, pass it to the -l switch:

docker ps -l

Stopping a running or active container is as simple as typing:

docker stop container-id

You can use the docker ps command to find the container-id in the output.

Step 8-Push the Docker image to the Docker repository

The next logical step after creating a new image from an existing image is to share it with a few friends of your choice, the entire world on Docker Hub, or other Docker registries that you can access. To push an image to Docker Hub or any other Docker registry, you must have an account there.

This section describes how to push Docker images to Docker Hub.

To create an account with Docker Hub, register in Docker Hub. Then, in order to push your image, first log in to Docker Hub. You will be prompted for identity verification:

docker login -u docker-registry-username

If you specified the correct password, authentication should be successful. Then you can use the following methods to push your own image

docker push docker-registry-username/docker-image-name

It will take some time to complete, and when complete, the output will look like this:

The push refers to a repository [docker.io/finid/centos-mariadb]
670194 edfaf5: Pushed 
5 f70bf18a086: Mounted from library/centos 
6 a6c96337be1: Mounted from library/centos

...

After pushing the image to the registry, it should be listed on the dashboard of your account as shown in the image below.

If the push attempt results in this type of error, you may not be logged in:

The push refers to a repository [docker.io/finid/centos-mariadb]
e3fbbfb44187: Preparing
5 f70bf18a086: Preparing
a3b5c80a4eba: Preparing
7 f18b442972b: Preparing
3 ce512daaf78: Preparing
7 aae4540b42d: Waiting
unauthorized: authentication required

Log in and repeat the push attempt.

in conclusion

Docker is much more than what is given in this article, but it is enough to get you started using it on CentOS 7. Like most open source projects, Docker is built from a rapidly developing code base, so make a habit of visiting the project blog page to get the latest information.

For more CentOS tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.

Reference: "How To Install and Use Docker on CentOS 7"

Recommended Posts

How to install and use Docker on CentOS 7
How to install and use Docker on Ubuntu 20.04
How to install and use Composer on CentOS 8
How to install and use Curl on CentOS 8
How to install and use Docker on Ubuntu 16.04
How to install and use Cockpit on CentOS 8/RHEL 8
How to install and configure Elasticsearch on CentOS 7
How to install and configure VNC on CentOS 8
How to install and use Curl on Ubuntu 18.04
How to install and use Composer on Ubuntu 18.04
How to install and use Wine on Ubuntu 18.04
How to install and configure Redis on CentOS 8
How to install Node.js and npm on CentOS 8
How to install and use Composer on Ubuntu 20.04
How to install and use BaasBox on Ubuntu 14.04
How to install and use PostgreSQL on Ubuntu 16.04
How to install jdk1.8.0_151 and mysql5.6.38 on centos7.2.1511
How to install and configure phpMyAdmin on CentOS 6
How to install and configure Owncloud on CentOS 8
How to install Docker CE on RHEL 8 / CentOS 8
How to install and uninstall tomcat on centos
How to install and configure Redmine on CentOS 8
How to install jdk1.8 on centOS7
How to install MySQL on CentOS 8
How to install Memcached on CentOS 8
How to install R on CentOS 8
How to install FFmpeg on CentOS 8
How to install Virtualbox on CentOS 8
How to install TensorFlow on CentOS 8
How to install TeamViewer on CentOS 8
How to install Perl 5 on CentOS
How to install Git on CentOS 8
How to install Gradle on CentOS 8
How to install Elasticsearch on CentOS 8
How to install Jenkins on CentOS 8
How to install Java on CentOS 8
How to install Go on CentOS 8
How to install GCC on CentOS 8
How to install Yarn on CentOS 8
How to install Nginx on CentOS 8
How to install Asterisk on CentOS 7
How to install Jenkins on CentOS 8
How to install Vagrant on CentOS 8
How to install Python 3.8 on CentOS 8
How to install Tomcat 9 on CentOS 8
How to install Webmin on CentOS 8
How to install Ruby on CentOS 8
How to install Skype on CentOS 8
How to install htop on CentOS 8
How to install Python on CentOS 8
Install and use docker under CentOS 6.8
How to install Elasticsearch on CentOS 8
How to install Postgresql on CentOS 8
How to install Wordpress on Centos
How to install htop on CentOS 8
How to install TeamViewer on CentOS 8
How to install MariaDB on CentOS 8
How to install MongoDB on CentOS 7
How to install Odoo 13 on CentOS 8
How to install Apache on CentOS 8
How to install OpenCV on CentOS 8