Introduction
Docker is an application that simplifies the process of running application processes in containers. These containers are similar to virtual machines, but are more portable, resource-friendly, and rely more on the host operating system.
There are two ways to install Docker on CentOS 7. One way is to install Docker on an existing operating system, and another way is to install Docker on the server using a tool called Docker Machine.
In this tutorial, you will learn how to install and use Docker on an existing CentOS 7 installation.
Prerequisites
- 64-bit CentOS 7 Droplet
- A non-root user with sudo permissions. The CentOS 7 Initial Setup Guide describes how to set this up.
All commands in this tutorial should be run as non-root users. If the command requires root access, it will be added before the commandsudo
。
Step 1 — Install Docker
The Docker installation packages available in the official CentOS 7 repository may not be the latest version. To get the latest version, Docker can be installed from the official Docker repository. This section will show you how to do this.
First, let's update the package database:
sudo yum check-update
Now run the following command. It will add the official Docker repository, download the latest version of Docker and install:
curl -fsSL / | 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 look like the following, showing that the service is active and running:
● - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/; enabled; vendor preset: enabled) Active: active (running) since Sun 2016-05-01 06:53:52 CDT; 1 weeks 3 days ago Docs: Main PID: 749 (docker)
Finally, make sure it starts every time the server restarts:
sudo systemctl enable docker
Installing Docker now not only provides Docker services (daemons), but also providesdocker
Command line utility, or Docker client. We will discuss how to use it later in this tutorialdocker
Order.
Step 2 — No need to use sudo to execute Docker commands (optional)
By default, running the docker command requires root permissions—that is, you must prepend sudo. You can also run the docker command by adding a user to a docker group, which is automatically created when Docker is installed. If you try to run the docker command without adding sudo or not 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 to rundocker
Avoid input during commandssudo
, you can add your username to the docker group:
sudo usermod -aG docker $(whoami)
You need to log out of the Droplet and log in again as the same user to enable this change.
If you need to add a user who is not currently logged in todocker
In the group, the user name can be explicitly declared:
sudo usermod -aG docker username
The rest of this article assumes that you will run as a user in the docker user groupdocker
Order. If you choose not to do this, please addsudo
。
Step 3 — Use the Docker command
After installing and running Docker, it's time to get familiar with the command line utilities. usedocker
Including passing a series of options and subcommands followed by parameters. The syntax is as follows:
docker [option] [command] [arguments]
To view all available subcommands, enter:
docker
As of 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 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
To view the available switches for a specific command, enter:
docker docker-subcommand --help
To view system-wide information, use:
docker info
Step 4 ——Use Docker Image
The Docker container is run from the Docker image. By default, it pulls these images from Docker Hub, a Docker registry managed by Docker Company, a backer of the Docker project. Anyone can build and host their Docker images on Docker Hub, so most applications and Linux distributions need to run in Docker containers are hosted on Docker Hub.
To check if you are accessible and download the image from Docker Hub, enter the following command:
docker run hello-world
The output should include the following to indicate that Docker is working properly:
Hello from Docker. This message shows that your installation appears to be working correctly. ...
You can usedocker
Commands andsearch
Subcommand searches for available images on Docker Hub. For example, to search for a CentOS image, enter:
docker search centos
The script will crawl Docker Hub and return a list of all images whose names match the search string. In this case, the output will look like:
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] ...
existOFFICIALIn the column,OKRepresents a mirror built and supported by the company behind the project. Once you determine which image you want to use, you can usepull
The subcommand downloads it to your computer as follows:
docker pull centos
After downloading the image, you can userun
The subcommand runs the container using the downloaded image. If usingrun
The image has not been downloaded at the time of the subcommand, the Docker client will first download the image and then use it to run the container:
docker run centos
To view the images downloaded to your computer, enter:
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 the Docker Hub or other Docker registry.
Step 5 —— Run the Docker container
The hello-world container you ran in the previous step is an example of a running and exited container that emits a test message and exits. However, containers can be more useful than that, and they can be interactive. After all, they are similar to virtual machines, but are more resource-saving.
For example, let's run a container using the latest CentOS image. The combination of -i and -t switches provides you with interactive shell access to containers:
docker run -it centos
Your command prompt should change to reflect that you are working inside the container now and should take the following form:
[root@59839a1b7de2 /]#
Important tips:Pay attention to the container ID in the command prompt. In the example above, it is59839a1b7de2
。
Now you can run any command inside the container. For example, let's install the MariaDB server in the running container. No need to prepend any commandssudo
, because you are operating inside the container with root permissions:
yum install mariadb-server
Step 6 — Submit changes in the container to a Docker image
When you start a Docker image, you can create, modify, and delete files just like you can use a virtual machine. The changes you make will only apply to the container. You can start and stop it, but once it is destroyed using the docker rm command, the changes will be permanently lost.
This section shows you how to save the state of a container as a new Docker image.
After you install the MariaDB server in a CentOS container, you now have a container running on the image, but that container is different from the image you used to create it.
To save the state of the container as a new image, first exit the container:
exit
Then use the following command to commit the changes as a new Docker image instance. The -m switch is used to submit messages to help you and others know the changes you have made, while -a is used to specify the author. The container ID is the ID you wrote down when you started an interactive Docker session in this tutorial. Unless you have created another repository 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
For example:
docker commit -m "added mariadb-server" -a "Sunday Ogwu-Chinuwa" 59839a1b7de2 finid/centos-mariadb
When you complete this, listing the Docker image on your computer should display the new image, as well as the old image it derives from:
docker images
The output should be similar to:
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 example above, centos-mariadb is the new image, which derives from an existing CentOS image on Docker Hub. The size difference reflects the changes made. In this example, the change is that the MariaDB server is installed. So next time you need to run the container with CentOS with MariaDB server pre-installed, you can use the new image. Mirrors can also be built from so-called Dockerfiles. But this is a very complex process that goes far beyond the scope of this article. We will explore this in future articles.
Step 7 — List Docker containers
After using Docker for a while, there will be many active (running) and inactive containers on your computer. To view the active container, use the following command:
docker ps
You will see an 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 view all containers (including active and inactive), use-a
switch:
docker ps -a
To view the latest container you created, use-l
switch:
docker ps -l
Stopping a running container is simple, just enter:
docker stop containerID
Container ID
Can be indocker ps
Found in the output of the command.
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 some of your friends, the entire world on the Docker Hub, or other Docker registry that you can access. To push the image to the Docker Hub or any other Docker registry, you must have an account there.
This section will show you how to push Docker images to Docker Hub.
To create an account on Docker Hub, register on Docker Hub. Then, to push your image, first log in to Docker Hub. You will be prompted to authenticate:
docker login -u docker-registry-username
If the correct password is entered, the authentication should be successful. Then you can push your own image using the following command:
docker push docker-registry-username/docker-image-name
This will take some time to complete, and once done, the output will look something like:
The push refers to a repository [/finid/centos-mariadb] 670194edfaf5: Pushed 5f70bf18a086: Mounted from library/centos 6a6c96337be1: Mounted from library/centos ...
Once the image is pushed to the registry, it should be listed on your account's dashboard, as shown in the image below.
!Docker image listing on Docker Hub
If the push attempt causes the following error, you may not be logged in:
The push refers to a repository [/finid/centos-mariadb] e3fbbfb44187: Preparing 5f70bf18a086: Preparing a3b5c80a4eba: Preparing 7f18b442972b: Preparing 3ce512daaf78: Preparing 7aae4540b42d: Waiting unauthorized: authentication required
After logging in, repeat push attempts.
in conclusion
There is still a lot of content in Docker that is not covered in this article, but this should be enough to get you started with it on CentOS 7. Like most open source projects, Docker is built on a rapidly evolving code base, so you need to develop the habit of visiting the project blog page to get the latest information.
The above is the detailed content of the methods and steps for installing and using Docker on CentOS 7. For more information about installing and using Docker in CentOS 7, please follow my other related articles!