Geekdosage
  • Home
  • Computer hardware
    • GPU
      • AMD
      • Nvidia
    • Processor
  • Linux
    • CentOS
    • Ubuntu
  • Docker
  • Computer Software
    • Operating System
      • Windows
      • MacOS
      • Android
  • Gaming
  • Computer Science
    • Network Engineering
    • Comparisons

Geekdosage

  • Home
  • Computer hardware
    • GPU
      • AMD
      • Nvidia
    • Processor
  • Linux
    • CentOS
    • Ubuntu
  • Docker
  • Computer Software
    • Operating System
      • Windows
      • MacOS
      • Android
  • Gaming
  • Computer Science
    • Network Engineering
    • Comparisons
CentOSDocker

How to Install Docker on CentOS 7

by GeekDosage December 10, 2020
written by GeekDosage December 10, 2020
install docker in centos 7

Docker is an Operating System-level virtualization technology that enables you to build, test, and deploy your application. Docker runs your application as a container, like a virtual machine, but more independently from the host operating system and more resource-friendly.

Let’s learn how to install Docker in the CentOS server and how to use basic docker commands. Read this article on how to install Docker in Ubuntu 20.04

Prerequisites

  • CentOS 7 server
  • A non-root user with sudo privileges

It is recommended that you uninstall older Docker versions before proceeding with the installation.

$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

Install Docker on CentOS 7

Installing Docker from the official Docker repository is recommended since the CentOS repository may not have the latest Docker versions. You can use the convenience scripts to install Docker. But it is not recommended for production servers.

$ sudo yum install -y yum-utils
$ sudo yum-config-manager \
--add-repo \
 https://download.docker.com/linux/centos/docker-ce.repo

Now the Docker repository is added, and you can install the required Docker engine (community edition) and containerd packages as follows.

$ sudo yum install docker-ce docker-ce-cli containerd.io

After successfully installing Docker, you can enable and start the Docker service.

$ sudo systemctl enable docker.service
$ sudo systemctl start docker.service

Make sure the Docker service is started and enabled.

$ systemctl status docker

You should see the output similar as follows.

OUTPUT

docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2020-06-15 15:04:20 +0530; 2s ago
Docs: https://docs.docker.com
Main PID: 8623 (dockerd)
Tasks: 20
Memory: 86.9M

CGroup: /system.slice/docker.service
├─8623 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Executing Docker Command Without Sudo

By default, sudo privileges are required to execute Docker commands; hence you always have to use sudo with Docker commands. Therefore as a solution, you can add the user to the Docker group, which gets created at the installation automatically.

$ sudo usermod -aG docker $USER

$USER will add current logged in user to Docker group. If you wish to add different users, add by giving username in the following command.

$ sudo usermod -aG docker username

NOTE: You have to logout from the current session and login again to apply the above changes.

Now you have completed Docker installation and are ready to use Docker. Let’s learn some basic Docker commands.

Using the Docker Command-Line Interface (Docker CLI)

Docker except commands according to the following syntax.

$ docker [option] [command] [arguments]

You can see command help by

$ docker [command] --help

List all available docker commands

$ docker

docker output

Docker Images

Docker image is an immutable binary file that contains all the required binaries, libraries, and other essential data for running a specific application. In other words, the Docker image is a snapshot of a Docker image. By default, Docker-Hub is the cloud-based Docker image registry.

You can search for Docker images in Docker-hub by running,

$ docker search image-name

NOTE: Please be cautious when selecting Docker images since anyone can push Docker images to Docker-Hub. Try to use trusted Docker images whenever it is possible.

You can download Docker images by executing the following command.

$ docker pull image-name

To list all the downloaded images, you can run the following command.

$ docker images

Docker images

To remove the downloaded image,

$ docker rmi image-name

You cant remove a Docker image that is already used for run Docker containers.

Docker Containers

Docker container is a running instance of a Docker image. But there should be a long-term running process to keep the container in the “UP” state. Otherwise, the container will be in the “DOWN” state.

Let’s run our first Docker container, “Hello World”

$ docker run hello-world
OUTPUT

Hello from Docker.

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

We can use -i and -t options with docker run command to interactively work with container shell. You will be bind to the container shell instead of the previously working user shell of your computer.

$ docker container run -it centos /bin/bash
OUTPUT

[[email protected] /]#

Note that 1923a41b62b7 is the container-ID and, it is a unique hexadecimal number for each container.

Press Ctrl + D or type exit to return to your computer shell.

All the running docker containers can be listed from the following command. You can find container-ID under the ‘CONTAINER ID’ column.

$ docker ps
OUTPUT


CONTAINER ID        IMAGE               COMMAND             CREATED

a6c32edc1a6dc        centos              "/bin/bash"         1 hours ago

Use -a option to list all running Docker containers and exited containers.

$ docker ps -a

To stop running Docker container,

$ docker stop container-name/container-id

You can execute the docker command as follows to start/ restart the container.

$ docker start/restart container-name/container-id

You can check container logs by running the following command.

$ docker logs container-name/container-id

Use -f option to follow log output.

$ docker logs -f container-name/container-id

How to Create a Docker Image from Docker Container

Once you start a docker container from a Docker image, you can add binaries, libraries, or any package. Subsequently, you can create a Docker image from that modified container.

$ docker commit -m "Commit message " -a "Author Name" container-ID repository/image_name

‘-m’ option is used to give a meaningful commit message, and the ‘-a‘ option is used for setting the author name who makes the changes. Container-ID should be the specific container that we are going to make a Docker image of.

Then we can push that created Docker image to the Docker-hub repository or private Docker registry after giving the credentials. You can refer to creating a private Docker registry tutorial here.

$ docker push docker_registry-username/docker_image-name

Conclusion

Here we have explained how to install Docker on CentOS 7 and how to use basic Docker commands. There are more advanced topics in the Docker ecosystem.

You can refer to official docker documentation for more information.

We would like to hear your feedback and answer your questions. Please feel free to leave a comment here.

GeekDosage
dockerdocker commitdocker containerdocker imagesdocker installdocker install centosdocker install in centosdocker runrun docker container
1 comment
0
FacebookTwitterPinterest
previous post
How to change the forgotten root password in CentOS 8
next post
How to Install Docker in Ubuntu 20.04

Related Posts

How to fix “dial unix /var/run/docker.sock: connect: permission...

August 10, 2021

How to connect to your VirtualBox VM from...

August 8, 2021

How To Install Cloudflare Origin SSL Certificate with...

July 20, 2021

How to Stop all Docker Containers

June 8, 2021

Cloudflare Origin SSL vs. Let’s encrypt SSL

May 9, 2021

Best 10 Docker Hosting Services

April 7, 2021

How to Exit Vim

March 11, 2021

SCP Linux – Securely Copy Files in Linux...

February 22, 2021

How to Copy, Paste and Delete in Vim...

February 7, 2021

How to Copy Files Between Linux Servers Using...

February 4, 2021

1 comment

How to fix "dial unix /var/run/docker.sock: connect: permission denied" error | Geekdosage August 10, 2021 - 7:52 pm

[…] How to Install Docker on CentOS 7 […]

Reply

Leave a Comment Cancel Reply

Save my name, email, and website in this browser for the next time I comment.

In This Article

  • 1 Prerequisites
  • 2 Install Docker on CentOS 7
  • 3 Executing Docker Command Without Sudo
  • 4 Using the Docker Command-Line Interface (Docker CLI)
  • 5 Docker Images
  • 6 Docker Containers
  • 7 How to Create a Docker Image from Docker Container
  • 8 Conclusion

Keep in touch

Facebook Twitter Google + Instagram Pinterest Email

Popular Posts

  • How to change the forgotten root password in CentOS 8

  • How to remove/delete user in Linux

  • How to Create a Private Docker Registry in Ubuntu 20.04

  • How to Build a Docker Image from Dockerfile

About Geekdosage

Geekdosage is a technology based website which mainly focuses on network engineering, computer science, automation, computer hardware and software and internet technologies. Our purpose is to create a platform where anyone can access the subject areas without barriers.

Recent Posts

  • How to fix “dial unix /var/run/docker.sock: connect: permission denied” error

    August 10, 2021
  • How to connect to your VirtualBox VM from your Host OS using SSH

    August 8, 2021

Find us

Facebook Twitter Instagram Pinterest Email
  • About Us
  • Contact Us
  • Disclaimer
  • Terms and Conditions
  • Cookie Privacy policy
  • Privacy Policy
  • DCMA

@2021 - All Right Reserved | Designed and Developed by GeekDosage