Introduction
Docker is a platform that makes running and managing application processes in containers easier. It provides a way to separate your applications from your infrastructure. Containers are very similar to virtual machines, but they are more portable, efficient, and easy to use. There are various ways to install Docker on Linux distributions. The most popular and easy way to install Docker on the existing operating system is by using yum commands.
This tutorial will demonstrate how you can set up and use Docker on CentOS 7 in a few simple steps.
Prerequisites:
- 64-bit CentOS 7 Instance.
- A non-root user with sudo privileges.
All the commands you execute throughout this tutorial are run as a non-root user and if necessary root access will be provided using the sudo keyword.
Step 1: Setting up Docker on CentOS 7 Instance
The official CentOS 7 repository may not have the latest installation package for Docker. In this section, you’ll be installing the latest version of Docker from the official Docker repository. First, you need to update the package database using:
1 |
sudo yum check-update |
After the update step is complete, run the below command to download and install the latest version of Docker:
1 |
curl -fsSL https://get.docker.com/ | sh |
Docker is now installed, hence you can start the Docker daemon by running the command below:
1 |
sudo systemctl start docker |
To verify if the Docker daemon is running type the following:
1 |
sudo systemctl status docker |
The output of the systemctl status command should be similar to the below-shared output, which shows it is running:
1 2 3 4 5 6 |
Output: ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2021-02-17 18:22:06 UTC; 10s ago Docs: https://docs.docker.com Main PID: 21884 (dockerd) |
Finally to make sure Docker starts up as you reboot your machine use the command:
1 |
sudo systemctl enable docker |
The Docker installation provides you with Docker service as well as the client utility (Docker command-line client). In the next sections of the tutorial, you’ll be able to get more hands-on using the Docker commands.
You can also take a look at our in-depth tutorial on how to install & operate Docker on Ubuntu in the public cloud.
Step 2: Using Docker Commands Without Sudo Prefix
Docker commands need root privileges to run. Thus, if you want to run the commands you will have to prefix them with sudo. During installation, a Docker group is created by default. If you add a user to the group you can run the Docker commands without sudo. Trying to run Docker commands without sudo or adding the user to the group will result in an output similar as below:
1 2 3 |
Output: docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?. See 'docker run --help'. |
Adding your username to the Docker group will ensure you don’t have to use sudo to run the Docker commands:
1 |
sudo usermod -aG docker $(whoami) |
To add another user to the Docker group you can simply replace the username in the command:
1 |
sudo usermod -aG docker username |
For the rest of this guide, we will assume that all the commands are executed by a user in the Docker user group. If this is not the case, you can use the sudo prefix with the commands.
Step 3: Executing the Docker Commands
Now that you have Docker installed and running, let’s look at some commands to get familiar with the Docker command-line utility. Docker commands usually take the form of:
1 |
docker [option] [command] [arguments] |
To find all the available subcommands you should use:
1 |
docker |
As of Docker 20.10.3, the complete list of available subcommands includes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
Output: attach Attach local standard input, output, and error streams 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 to files or directories 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 Docker objects kill Kill one or more running containers 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 pause Pause all processes within one or more containers 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 one or more containers 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 (streamed to STDOUT by default) 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 one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes |
You can use –help flag with a specific command to get more information about it:
1 |
docker subcommand --help |
To get detailed information about the system, use:
1 |
docker info |
Step 4: Working with Docker Images
Docker images can be called the blueprint for Docker containers. These images are usually pulled from the Docker Hub, which is a registry managed by the Docker project. Anyone can create and push their images on the Docker Hub. As a result, you can easily find a wide variety of applications and os distributions in the registry. Let’s try out a simple program that will confirm access to the Docker Hub:
1 |
docker run hello-world |
You should get an output as below, which shows that Docker is working:
1 2 3 4 |
Output: Hello from Docker! This message shows that your installation appears to be working correctly. ... |
You can find various Docker images on the Docker Hub by using the search command. For example, see the below command to search for a CentOS image:
1 |
docker search centos |
The search query will show up a list of all the images which matched with the substring. In your case the output should be like:
1 2 3 4 5 6 7 8 |
Output: NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 6410 [OK] ansible/centos7-ansible Ansible on Centos7 132 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC sess… 125 [OK] jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos… 117 [OK] centos/systemd systemd enabled base container. 96 [OK] ... |
In the search results, there are different columns describing information about the image. The OK in the OFFICIAL column determines that the image was created and supported by the company behind the application. Once you have finalized the image, you can download it to your local machine using the Docker pull command:
1 |
docker pull centos |
After downloading the image, you can run the container using the Docker run command. If you directly try to run an image without prior downloading, Docker will download the image and run the container afterward:
1 |
docker run centos |
You can list the images that are downloaded to your local machine, using the below command:
1 |
docker images |
You should get a similar output:
1 2 3 4 |
Output: REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 300e315adb2f 2 months ago 209MB hello-world latest bf756fb1ae65 13 months ago 13.3kB |
Later in this tutorial, you’ll be able to modify the images to run the containers. These new images can be added or pushed in the Docker Hub and other registries that host the Docker images.
Step 5: Running a Container Interactively
There are different types of containers. The hello-world container you ran in Step4 is a type of container which runs and exits after printing a message. Another type of container is the interactive one. You can use interactive containers in a similar fashion as a virtual machine.
Let’s create a container from the latest CentOS image. Using -i and -t flags in the Docker run command will give interactive access to the CentOS container:
1 |
docker run -it centos |
The command prompt will change and it should look like the output below:
1 2 |
Output: [root@3ce69d2a35b9 /]# |
1 |
Note: The container id displayed in the command prompt is unique and it will come in handy afterwards, in the example it is 3ce69d2a35b9. |
Now any command you execute will run inside the container. That is similar to running a command in a virtual machine. Let’s try installing MySQL server in the CentOS container. You can do this using:
1 |
yum install mysql |
Step 6: Committing Changes in a Container to a Docker Image
After starting the container, you can do all the operations which are doable in a similar virtual machine, like creating/modifying the files or setting up an app. Please note that these changes will only stay for that container, and after you destroy the container the changes you made will be lost.
In this part of the tutorial, you’ll learn how to create a new Docker image from a container with the changes you have made. After Step5 you have a CentOS container running with MySQL server installed. This container is now different than the plain CentOS image. You can save this state of container for further use. First, you need to exit the container using:
1 |
exit |
Commit the changes you have made in the container to a new Docker image using the below command:
1 |
docker commit -m "Changes done to the image" -a "Author Name" container-id repository/new_image_name |
In the command, -m refers to the commit message and should basically denote the changes made, -a tag is used to mention the author. Container-id is the one from Step5, which you got after running the container in interactive mode, and usually, the repository is your username for Docker Hub. For example:
1 |
docker commit -m "added mysql-server" -a "CloudSigma" 59839a1b7de2 finid/centos-mariadb |
1 |
Note: After the new image is committed it is saved on your system. In the next stages of this tutorial, you will learn how to push an image to Docker Hub and in the similar registries which host docker images. Once your image is pushed to the registry, it can be accessed by others as well. |
Now that the image is committed, the Docker images command should list the new image as well as the old ones:
1 |
docker images |
The output of the command should be similar as below:
1 2 3 4 5 |
Output: REPOSITORY TAG IMAGE ID CREATED SIZE cloudsigma23/centos-mysql latest 1b9368efea70 13 seconds ago 308MB centos latest 300e315adb2f 2 months ago 209MB hello-world latest bf756fb1ae65 13 months ago 13.3kB |
As seen in the example, a new image centos-mysql is created using the CentOS image from the Docker Hub. The difference in size determines that some changes were made. In this example, it was the addition of the MySQL server in the container. Next time if you need a container with a MySQL server, you can just run the new image, and voila! You have a CentOS container with a pre-installed MySQL server running.
Step 7: Managing Docker Containers
Now that you are familiar with Docker, after using it for some time you already have some running and some inactive containers. To get the list of active containers you should use:
1 |
docker ps |
You should see a similar output:
1 2 3 |
Output: CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 604c889cf404 centos "/bin/bash" 20 minutes ago Up 20 minutes intelligent_easley |
In order to list both the active and inactive containers, you should use the -a flag with the command:
1 |
docker ps -a |
To find the last container you created, you can provide -l flag:
1 |
docker ps -l |
To stop a running/active container run a simple command:
1 |
docker stop container-id |
You can find the container-id in the output of Docker ps command.
Step 8: Publishing the Images to a Repository
The next step after creating the new image is to share that with your friends. You can also make it available for the whole world using Docker Hub or any other registry. You are required to login into the respective registry before pushing the image.
In the next part of the tutorial, you’ll learn how to push the images to Docker Hub. First, sign up on Docker Hub. You’ll need to log into Docker Hub to push your image using the command below:
1 |
docker login -u docker-registry-username |
Once you provide the right password, and authentication is successful, you can push your image. To push the image use the below command:
1 |
docker push docker-registry-username/docker-image-name |
The output for the command will be similar to this:
1 2 3 4 5 |
Output: The push refers to repository [docker.io/cloudsigma23/centos-mysql] ee30e80cbcc5: Pushed 2653d992f4ef: Mounted from library/centos ... |
Once you have pushed the image, it should show up on your account’s dashboard, as shown in the image below:
In case of failure in a similar way, chances are that you have not logged in:
1 2 3 4 5 6 7 |
Output: The push refers to repository [docker.io/cloudsigma23/centos-mysql] ee30e80cbcc5: Layer already exists 2653d992f4ef: Layer already exists errors: denied: requested access to the resource is denied unauthorized: authentication required |
You can log in, and repeat the push attempt.
Conclusion
There are a number of ways in which you can make use of Docker. This tutorial should provide you with enough information to get you started. And since Docker is a really trending project, you can find many details about the usage as well as different use cases from the project’s blog page.
You can also check out our other Docker tutorials to learn more about what you can do with Docker:
- Clean Up Docker Resources – Images, Containers, and Volumes
- Deploying Laravel, Nginx, and MySQL with Docker Compose
- Run your own VPN server under Docker with OpenVPN Access Server
Happy Computing!
- How to Deploy WordPress with Persistent Volume on Kubernetes Cluster - March 17, 2023
- Deploying Applications on Kubernetes Using Argo CD and GitOps - October 26, 2022
- Using Node.js Modules with npm and package.json: A Tutorial - October 6, 2022
- Using Ansible to Install and Configure WordPress with LAMP on Ubuntu - September 23, 2022
- Creating Views in the Django Web Application Framework - September 22, 2022