Install Docker and Learn Basic Container Manipulation in CentOS and RHEL 8/7


Introduction

Docker has revolutionized the world of software development and deployment by providing a lightweight and efficient containerization platform. With Docker, developers can package their applications and dependencies into containers, making it easier to deploy and run them consistently across different environments. In this article, we will guide you through the process of installing Docker on CentOS and RHEL 8/7 and provide examples of basic container manipulation.

Installing Docker on CentOS/RHEL 8

Preparing the System

Before installing Docker, ensure that your system is up to date by running the following commands −

$ sudo yum update
$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Adding Docker Repository

To install the Docker package, you need to add the Docker repository to your system. Execute the following command −

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

Installing Docker Engine

Now, install Docker Engine using the following command −

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

Starting Docker Service

Start the Docker service and enable it to launch at system boot −

$ sudo systemctl start docker
$ sudo systemctl enable docker

Verifying Docker Installation

Confirm that Docker is installed and running by checking the version −

$ docker --version

Basic Container Manipulation

Running Your First Container

Let's start by running a simple container. Execute the following command to run an Nginx web server container −

$ docker run -d -p 80:80 nginx

Listing Running Containers

To view the list of running containers, use the following command −

$ docker ps

Stopping a Container

Stop a running container using its container ID or name −

$ docker stop <container_id or container_name>

Removing a Container

To remove a stopped container, use the following command −

$ docker rm <container_id or container_name>

Pulling Docker Images

You can pull images from Docker Hub using the docker pull command. For example, to pull the Ubuntu 20.04 image, use −

$ docker pull ubuntu:20.04

Running Interactive Containers

To run an interactive container and get a shell prompt inside it, use the following command −

$ docker run -it ubuntu:20.04 /bin/bash

Copying Files to/from Containers

You can copy files between the host system and a container using the docker cp command. For instance, to copy a file named file.txt to a container, execute −

$ docker cp file.txt <container_id>:<destination_path>

Managing Container Resources

Docker provides options to limit container resources like CPU and memory usage. For example, to restrict a container to use only 1 CPU core, use −

$ docker run --cpus=1 <image_name>

Installing Docker on CentOS/RHEL 7

Enabling the Docker CE Repository

First, enable the Docker CE repository by running the following command −

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

Installing Docker

Install Docker using the following command −

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

Starting Docker Service

Start the Docker service and enable it to launch at system boot −

$ sudo systemctl start docker
$ sudo systemctl enable docker

Verifying Docker Installation

Confirm that Docker is installed and running −

$ docker --version

Docker Example and Output

Building a Custom Docker Image

Docker allows you to create your own custom images by specifying a Dockerfile. Let's create a simple Dockerfile that builds an image with a custom HTML file −

Create a file named Dockerfile with the following content −

FROM nginx:latest
COPY index.html /usr/share/nginx/html/

Create an index.html file in the same directory with your desired content.

Build the Docker image using the following command −

$ docker build -t custom-nginx .

Running a Container from the Custom Image

Now that we have built the custom Docker image, let's run a container based on it −

$ docker run -d -p 8080:80 custom-nginx

Access the containerized web server by opening your browser and navigating to http://localhost:8080. You should see the content of your custom HTML file.

Inspecting Container Details

To view detailed information about a running container, such as its IP address, network configuration, and more, use the following command −

$ docker inspect <container_id or container_name>

Container Logs

To check the logs generated by a container, use the following command −

$ docker logs <container_id or container_name>

Managing Docker Images

Docker allows you to manage your images efficiently. Here are a few useful commands −

List all the available images on your system −

$ docker images

Remove an image −

$ docker rmi <image_id or image_name>

Pulling and pushing images to Docker Hub −

$ docker pull <image_name>
$ docker push <image_name>

Docker Networking

Understanding Docker Networking

Docker provides various networking options to facilitate communication between containers and the host system. Let's explore some common networking concepts −

  • Bridge Network − Docker creates a bridge network by default, allowing containers to communicate with each other and the host using internal IP addresses.

  • Host Network − Containers share the network namespace with the host, enabling them to use the host's network interface directly.

  • Overlay Network − Used for communication between containers running on different Docker hosts, forming a distributed network.

Creating a Bridge Network

To create a custom bridge network, use the following command −

$ docker network create mynetwork

Running Containers on a Custom Network

You can specify a network while running a container to connect it to a specific network −

$ docker run -d --network=mynetwork --name container1 nginx
$ docker run -d --network=mynetwork --name container2 nginx

The above commands create two containers (container1 and container2) and connect them to the mynetwork bridge network.

Inspecting Network Details

To view information about a Docker network, such as connected containers and IP addresses, use the following command −

$ docker network inspect mynetwork

Connecting Containers to Multiple Networks

Containers can be connected to multiple networks, allowing communication between different networks. Use the following command to connect a container to an additional network −

$ docker network connect mynetwork2 <container_id or container_name>

Exposing Container Ports

To expose a container's port to the host system, use the -p or --publish flag while running the container −

$ docker run -d -p 8080:80 nginx

The above command exposes port 80 of the container to port 8080 on the host.

Linking Containers

The --link option allows containers to securely communicate with each other using private DNS names. For example, to link container1 to container2, use the following command −

$ docker run -d --link container2 --name container1 nginx

In this case, container1 can communicate with container2 using the DNS name container2.

Conclusion

In this article, we have covered the installation of Docker on CentOS and RHEL 8/7. We walked through the step-by-step process of setting up Docker, including repository configuration, package installation, and service management. Additionally, we explored basic container manipulation commands such as running, stopping, and removing containers, pulling images, interacting with containers, and managing container resources. With this knowledge, you can now begin exploring the vast world of containerization and leverage Docker to simplify your software deployment processes.

Updated on: 17-Jul-2023

149 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements