Linux Commands

How to Build and Configure the Custom Docker Images with Dockerfile

Docker has altered the way the software is created, distributed, and executed by providing a lightweight and efficient containerization platform. Docker’s ability to automate the construction and configuration of the bespoke Docker images using Dockerfiles is a crucial component of its success. Dockerfiles are simple text files that include a set of instructions to automatically build a Docker image.

In this article, we will talk about the power and variety of Dockerfiles, as well as how to utilize them to automate the creation of the custom Docker images. We will go over some best practices to create the efficient and manageable Dockerfile settings, as well as how to define and organize the Dockerfile instructions. At the end, we can understand how to use the Dockerfile to automatically produce and configure the custom Docker images, streamlining our containerization workflow.

Installing Docker in Centos

The system packages should be updated:

# sudo yum update

Install the following packages in order to set up the Docker repository:

# sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Set up the Docker repository as follows:

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

The Docker Engine must be installed:

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

Docker may be installed by ”/”. The following commands can be used to start the daemon, verify its state, and allow it system-wide:

# systemctl start docker

# systemctl status docker

# systemctl enable docker

Run a hello-world container to test the Docker installation:

# sudo docker run hello-world

We may now execute a few basic Docker commands to learn more about Docker:

Docker system-wide information:

# docker info

Moreover, we may examine the Docker version:

# docker version

Type “docker” on our console to see a list of all possible Docker commands.

# docker

Making or Creating a Dockerfile Repository

First, let’s create several Dockerfile repositories so that we can reuse the files to generate more images in the future. Make an empty directory in the “/var” partition where we construct the file that contains the instructions to build the freshly generated Docker image.

Then, begin editing the file as follows:

# vi /var/docker/ubuntu/apache/Dockerfile

After that, insert the following:

FROM ubuntu

MAINTAINER your_name <user@domain.tld>

RUN apt-get -y install apache2

Creating a New Docker Image Named “Ubuntu-Apache”

The next step is to begin constructing the image by running the following command which creates a new Docker image named “ubuntu-apache” that is locally based on the Dockerfile that is prepared earlier as demonstrated in this example:

# docker build -t ubuntu-apache /var/docker/ubuntu/apache/

After Docker has built the image, we may use the following command to display all available images and identify our image:

# docker images

Starting the Container and Connecting to Apache through LAN

To keep the container running endlessly and to access the container’s exposed services and ports from the host or another remote workstation in our LAN, type the following command into our host terminal prompt:

# docker run -d -p 81:80 ubuntu-apache

To display the network interface IP addresses, use the following IP command line:

# ip addr [List nework interfaces]

# curl ip-address:81 [System Docker IP Address]

# curl localhost:81 [Localhost]

Make a Docker Container System-Wide Configuration File

Make a new systemd file called “apache-docker.service”. Then, execute the following command:

# vi /etc/systemd/system/apache-docker.service

After that, insert the following:

[Unit]

Description=apache container

Requires=docker.service

After=docker.service

[Service]

Restart=always

ExecStart=/usr/bin/docker start -a my-www

ExecStop=/usr/bin/docker stop -t 2 my-www

[Install]

WantedBy=local.target

Conclusion

Automating the construction and setup of Docker images with Dockerfile is a powerful solution that simplifies the containerization workflow. By knowing the fundamentals of Dockerfile instructions, optimizing the settings, and utilizing the build tools, we can expedite the process and save time. We can also assure a consistent and dependable deployments of their apps within the Docker containers.

Similar Posts