Install and Use Docker CE on CentOS 8

0
1144

Welcome to our guide on how to install and use docker ce on CentOS 8. Docker is a platform that enables developers and system administrators to build, run, and share applications with containers.

The exits two editions of docker available. Docker CE and Docker EE

  • Docker CE (Community Edition) is the open-source, community supported version of Docker and is available for free.
  • Docker EE (Enterprise Edition) is a commercial/premium version of Docker CE and is supported by Docker Inc.

Install and Use Docker CE on CentOS 8

There are different methods in which you can install Docker CE on CentOS 8;

Installing Docker CE on CentOS 8 from Docker Repositories

Run System Update

Ensure that your system package cache is up-to-date.

dnf update

Uninstall Docker Versions

If any of the old docker versions are installed on your system, ensure that you remove them and their dependencies before you proceed.

dnf remove docker*

Install Docker CE Repository on CentOS 8

To ensure seamless future upgrades of Docker packages, you can install it from their official repos. For this, you need to setup the Docker repositories on CentOS 8 by executing the command below;

dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

The command above installs and enable Docker CE repo on CentOS 8.

Install Docker Engine (Docker CE) on CentOS 8

Once the repos are in place, proceed to install docker-ce and other tools including containerd.io, an open and reliable container runtime on CentOS 8.

The latest docker-ce package provided by the Docker CE repos installed above requires containerd.io >= 1.2.2-3 (as of this writing), which is not available on the Docker CE repos.

As such, install containerd.io package manually before you can proceed to install Docker CE itself.

dnf install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.13-3.2.el7.x86_64.rpm

Next, install Docker CE and Docker CE CLI utility.

dnf install docker-ce docker-ce-cli

Installing Docker CE from an RPM package

You can as well install Docker ce on CentOS 8 using an RPM package.

Download the latest versions of the individual packages installed above from Docker Packages page.

for i in containerd.io-1.2.13-3.2.el7.x86_64.rpm docker-ce-19.03.12-3.el7.x86_64.rpm docker-ce-cli-19.03.12-3.el7.x86_64.rpm; do dnf install -y https://download.docker.com/linux/centos/7/x86_64/stable/Packages/$i; done

Replace the version/release numbers of the packages accordingly.

Starting Docker Service

To start and enable Docker and Containerd service to run on system boot;

systemctl enable --now docker containerd

Running Docker as a non-root user

If you run Docker as standard user, you may get such an error;

[[email protected] ~]$ docker ps -a
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: ...
/var/run/docker.sock: connect: permission denied.
See 'docker run --help'.

Therefore, if you need to run docker as non-root user, simply add the user you want to run docker as to docker group or simply, grant the user sudo rights. For example to add a user, koromicha, to docker group;

usermod -aG docker koromicha
groups koromicha
koromicha : koromicha docker

Next, log out and login again as the non root user that you added to the docker group so that your group membership can be re-evaluated.

You can then run docker as a non root user.

Verifying Docker CE installation

Check the docker version;

docker --version
Docker version 19.03.12, build 48a66213fe

To verify that Docker CE is running well, you can try running the hello-world container image.

[[email protected] ~]$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76
Status: Downloaded newer image for hello-world:latest

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

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

To list running containers;

[[email protected] ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
4083300e3660        hello-world         "/hello"            3 minutes ago       Exited (0) 5 minutes ago                       laughing_visvesvaraya

Running Docker Images on CentOS 8

While verifying docker installation above, we ran the hello-world docker container image. Building and running docker containerized applications is the next step after setting up your docker environment.

So there are two things here: You can either build and run your own docker container images or pull already created and shared images from docker hub, an image repository for Docker images.

In this demo, we will learn how to use already created docker images from docker hub. With docker command, you can easily pull docker images from docker hub, as we did with the hello-world image above.

If you want to build your own images and share on docker hub, then you need to sign up to create your own namespace for storing your images, which then can be publicly available.

Using docker command

docker command has quite a number of command line options for running various tasks.

The command line syntax is;

docker [OPTIONS] COMMAND

You can view various command options and commands by executing;

docker --help
...
Options:
      --config string      Location of client config files (default "/home/koromicha/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
...
Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
...
Commands:
  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
...

To view help for a specific docker command;

docker COMMAND --help

For example, to find the usage of docker run command;

docker run --help

Running Docker Images from Docker Hub

Searching for Docker Images on Docker Hub

You can search for the specific publicly available Docker images on Docker Hub using docker search command. The docker search command syntax is;

docker search [OPTIONS] TERM

Use docker search --help to see more options.

For example, to search all the images containing the term hello-world;

docker search hello-world
NAME                                       DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   1226                [OK]                
kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   147                                     
tutum/hello-world                          Image to test docker deployments. Has Apache…   72                                      [OK]
dockercloud/hello-world                    Hello World!                                    19                                      [OK]
...

Some column headings are self explanatory. Other columns include;

STARS : Shows the number of likes the image has had.
OFFICIAL : Specifies whether the image is built from a trusted source.
AUTOMATED : Shows whether the image has been automatically build from source code in an external repository and automatically pushed to Docker repositories.

Usually, an image with no prefix is considered official docker hub image, stable and being maintained. It also contains [OK] on the Official column.

Pulling Docker Image to Local Registry

Once you have identified the image that you need to run, you can either pull it and store it on your local registry using the the docker pull command or simply run it using docker run command.

For example, to search and pull an Ubuntu system docker image;

docker search ubuntu
NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   11046               [OK]                
dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   441                                     [OK]
rastasheep/ubuntu-sshd
...

Let us pull the official Ubuntu image.

docker pull ubuntu

This will by default pull the most recent version of Ubuntu currently available, usually the latest LTS versions.

Using default tag: latest
latest: Pulling from library/ubuntu
a4a2a29f9ba4: Pull complete 
127c9761dcba: Pull complete 
d13bf203e905: Pull complete 
4039240d2e0b: Pull complete 
Digest: sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

You can specify other tags for specific types of Ubuntu though;

docker search ubuntu:20.04

List Available Docker Images

You can list locally available images using docker images command.

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              74435f89ab78        11 days ago         73.9MB
hello-world         latest              bf756fb1ae65        5 months ago        13.3kB

Running a Docker Container Image

Once you have pulled a container from Docker hub and is available locally, you can either run it with docker run or docker create command.

With docker run, you can create a container, start it and access it using a shell to run any command inside it. The docker run command line syntax is;

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

For example, to run our Ubuntu image we pulled from Docker hub above in an interactive manner (option -i) drop to the shell (option -t, to allocate pseudo-TTY);

docker run -it ubuntu

This drops to the shell;

[email protected]:/#

You are now inside an Ubuntu container, with a container ID, 43ff7a031a0b, as root user. Anything that you run or execute under this shell happens within the filesystem of the container itself.

You can execute the commands directly without dropping to container shell;

docker run ubuntu cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

If you run the container interactively and started a shell, you can run your commands inside it.

When you exit the shell, the shell attached to the container is detached and container stops. If you used the options, -i and -t when running docker container, you can exit the shell without stopping the container, use the keystrokes Ctrl + p then Ctrl + q.

You can as well run container in background and print container ID

docker run -dit --name ubuntudemo ubuntu

To attach it to the console;

docker attach ubuntudemo

Listing Docker Containers

You can list live (currently running) containers using docker ps command.

docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1c280cf21322        ubuntu              "/bin/bash"         2 minutes ago       Up 2 minutes                            ubuntudemo

To list all containers, those running and those exited;

docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                        PORTS               NAMES
1c280cf21322        ubuntu              "/bin/bash"             3 minutes ago       Up 3 minutes                                      ubuntudemo
b5453d11a544        ubuntu              "/bin/bash"             10 minutes ago      Exited (0) 8 minutes ago                          stupefied_hawking
a65d1797c504        ubuntu              "/bin/bash"             14 minutes ago      Exited (0) 12 minutes ago                         zen_ramanujan
c4d68a2b1b39        ubuntu              "/bin/bash"             23 minutes ago      Exited (137) 20 minutes ago                       sweet_dirac
9b7867c1daad        ubuntu              "cat /etc/os-release"   40 minutes ago      Exited (0) 40 minutes ago                         great_allen

Start and Stop Docker Containers

You can start exited/stopped containers using docker start command. For example, to start container named, stupefied_hawking, with an ID of b5453d11a544, run the command;

docker start stupefied_hawking

Or

docker start b5453d11a544

Similarly, you can stop a running docker using its name or container ID with docker stop command.

docker stop ubuntudemo

Removing Docker Containers

Once you are done with your containers, you can remove them using docker rm command.

To remove a stopped container with an ID of 9b7867c1daad, for example, in our docker ps -a output above;

docker rm 9b7867c1daad

You cannot remove a running container unless you force the removal using using -f or --force option of docker rm command.

docker rm 1c280cf21322 -f

Removing Docker Images

Docker images can as well be removed using docker rmi command. For example, you can list images and get the image ID of the specific image you want to remove;

docker images -a

You can show numeric IDS using -q option.

docker images -a -q
74435f89ab78
bf756fb1ae65

Delete an image;

docker rmi 74435f89ab78

Or use the name repo name;

docker rmi ubuntu

Use option -f or --force to force the deletion.

That concludes our guide on how to install and use Docker CE on CentOS 8.

Further Reading

Docker Documentation

Related Tutorials

Install Docker CE on Ubuntu 20.04

Install and Use Docker on Debian 10 Buster

Install and Setup Ansible on Ubuntu 20.04

Install and Deploy Kubernetes Cluster on Ubuntu 20.04

LEAVE A REPLY

Please enter your comment!
Please enter your name here