Install and Use Docker on Centos 7

In this article, we want to teach you how to install and use Docker on Centos 7.

Docker is an open-source platform that is set up based on a Linux operation system. To know more details about Docker check our article about What is Docker and how it works. Now follow the steps below to start your Docker installation on Centos 7.

Steps To Install and Use Docker on Centos 7

There are two ways to install Docker on Centos 7. One way involves installing it on an existing installation of the operating system. The other involves spinning up a server with a tool called Docker Machine that auto-installs Docker on it.

In this article, you learn to install docker on an existing installation of Centos 7.

Requirements

You need a 64-bit Centos 7 Droplet. Also, you need a non-root user with Sudo permissions. If you don’t know how to add a user with Sudo privileges, you can visit this article Initial server setup with Centos 7.

Note: Docker requires a 64-bit version of Centos 7 as well as a kernel version equal to or greater than 3.10. The default 64-bit Centos 7 Droplet meets these requirements.

After you are finished with these requirements, let’s start to install Docker on Centos 7.

Step 1 – Download and Install Docker on Centos 7

The Docker installation package is in the Centos 7 repository by default. But it may not be in the latest version. To get the latest version and install Docker, follow these steps:

Update the package database with the following command:

sudo yum update -y

To add the official Docker repository, download the latest version, and install it on Centos 7 run the following command:

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

Step 2 – Start and Run Docker on Centos 7

After the installation is finished, you need to start the Docker service with the command below:

sudo systemctl start docker

Verify that the Docker is active and running on Centos 7 using the following command:

sudo systemctl status docker

Your output should be like this:

Output
docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2021-08-28 01:47:09 EDT; 20s ago
     Docs: https://docs.docker.com
 Main PID: 7672 (dockerd)

To start Docker at every server reboot run the following command:

sudo systemctl enable docker

Now the installation of Docker on Centos 7 is finished. It will give you the Docker service and also the Docker command-line utility.

Step 3 – How To Run Docker Commands on Centos 7?

Note: As we mentioned before to run the docker commands you need to log in as a non-root user with root privileges. Or you can run a command with a user that is in a docker group that is created during the installation of Docker.

If you don’t want to use Sudo to run docker commands, you need to add your user to the docker group. To do this, run the following command:

sudo usermod -aG docker $(whoami)

At this point, log out of your server and then back in with the same user to enable these changes.

If you need to add a user to the docker group that you’re not logged in, run the following command:

sudo usermod -aG docker username

In the rest of the article, we run commands as a user in the docker group, if you don’t want to use this run the commands with sudo.

Step 4 – Use Docker Command Line Utility on Centos 7

In this step, you get to know the command-line utility of Docker on Centos 7. The syntax of the docker command is like the following phrase:

docker [option] [command] [arguments]

To list available subcommands type the docker:

docker

In your output, you will see the complete list of available subcommands:

 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

To see the options available for a specific command use the following command:

docker docker-subcommand --help

You can see system-wide information with the below command:

docker info

At this point, you learn how to install Docker and use the Docker command on Centos 7.

Let’s learn how to work with Docker images on Centos 7.

Step 5 – Work with Docker Images

The Docker Image is a portable file that contains a set of instructions that specify which software components the Container should run and how to run it. Anybody can build and host their Docker images on Docker Hub, so most applications and Linux distributions you’ll need to run Docker containers have images that are hosted on Docker Hub.

To check that you can access and download images from Docker Hub, run the following command:

docker run hello-world

Your output should be similar to this:

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

You can search for available images on the Docker hub with the following command, for example, search for the Centos image:

docker search centos

Here your output should be similar to this:

Output
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                            The official build of CentOS.                   6723      [OK]
ansible/centos7-ansible           Ansible on Centos7                              134                  [OK]
consol/centos-xfce-vnc            Centos container with "headless" VNC session…   130                  [OK]
jdeathe/centos-ssh                OpenSSH / Supervisor / EPEL/IUS/SCL Repos - …   120                  [OK]
centos/systemd                    systemd enabled base container.                 101                  [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   91     
imagine10255/centos6-lnmp-php56   centos6-lnmp-php56                              58                   [OK]

Now choose an image that you want to use then download it with the following command:

docker pull centos

After you download the image you need to run a container using the downloaded image.

Note: If the image hasn’t been downloaded, the run subcommand will download it first and then run a container using it.

docker run centos

To see the images that were downloaded run the following command:

docker images

You will see something like this in your output:

Output
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   5 months ago   13.3kB
centos        latest    300e315adb2f   8 months ago   209MB

At this point, images that you use to run containers can be modified and used to generate new images, which may then be uploaded to Docker Hub or other Docker registries.

Step 6 – Run a Docker Container

Here let’s run a container using the latest image of Centos as an example. Run the following command:

docker run -it centos

Note: -it switch gives you interactive shell access into the container.

Your output should be similar to this:

Output
[root@8ff73d2a54e1 /]#

Important Note: Remember the container ID. Here it is 8ff73d2a54e1.

Now you can run any command inside the container. For example, install the MariaDB server in the running container. No need to run any command with sudo, because you’re operating inside the container with root privileges.

Run the following command:

[root@8ff73d2a54e1 /]# yum install mariadb-server

Step 7 – Commit changes in a container to a Docker image

In this part, you learn how to save the state of a container as a new Docker image on Centos 7.

After you installed MariaDB in the Centos container, now you have a container running off an image, but the container is different from the image you used to create it.

First of all, you need to exit from it to save the state of the container as a new Docker image.

exit

Then run the command below:

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

In the above command remember to put the container ID that we mentioned above.

For example:

docker commit -m "added mariadb-server" -a "olivia viera" 8ff73d2a54e1 olivia/centos-mariadb

After you complete this step, list docker images to see the new image too:

docker images

Your output should seem like this:

Output
REPOSITORY                  TAG       IMAGE ID       CREATED         SIZE
olivia/centos-mariadb       latest    4a2e9b238641   3 minutes ago   456MB
hello-world                 latest    d1165f221234   5 months ago    13.3kB
centos                      latest    300e315adb2f   8 months ago    209MB

Here Centos-MariaDB is the new image, the size difference means the changes were made.

Step 8 – List Docker Containers

In this step, we want to show how to list Docker containers on Centos 7.

To see active containers, run the following command:

docker ps

You can see all containers including active and non-active with the command below:

docker ps -a

If you want to see the latest container you created, type:

docker ps -l

To stop a running or active container, run the following command:

docker stop container-id

Note: The container-id can be found in the output from the docker ps command.

Step 9 – Push Docker images to a Docker repository

After you create a new image from an existing image you may want to share it with a few of your friends, the whole world on Docker Hub, or other Docker registries that you have access to. To push an image to Docker Hub or any other Docker registry, you must have an account there.

To have an account on Docker Hub you need to register at Docker Hub.

If you want to log in to the Docker hub you will be asked for authentication :

docker login -u docker-registry-username

If you enter the correct password, authentication should succeed. Then, you may push your own image using the following command:

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

It will take a little time to complete. After you push an image to a registry, it should be listed on your account’s dashboard.

Note: If a push attempt results in an error of this sort, log in, then repeat the push attempt.

That’s it, you are done.

Conclusion

At this point, you have learned how to install Docker, work with its command-line utility, and work with Docker Images and Docker containers. Also, you have learned to push docker images to a docker repository by using the DockerHub.

I hope you enjoy this article.

Also, if you are interested, you can see the following guide:

Use Docker Compose on Ubuntu 22.04

Install and Use Docker on AlmaLinux 9

Install and Use Docker on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!