How to Build a Docker Image using Docker File on Linux

Build a Docker Image using

In this blog, we will learn How to Build a Docker Image using Docker File on Linux.

Docker is the most useful open-source software for installing and running applications very fast. Docker easily runs on any type of operating system. The Docker will allow installing all applications & packages into the container. All the containers depending on the images. Docker image stores the all configurations of the applications & packages.

If we build the docker images, all the configurations are defined in a Dockerfile. The Docker file simply defines the steps and instructions to deploy a docker image.

Read Also: How to install docker and docker-compose in CentOS 8

Prerequisites

  • Linux-based Operating System or server like Ubuntu, CentOS, etc.
  • Access of the Root User or User with sudo privileges.

Dockerfile format

The first part defines the docker commands you can use in a docker shell. The second represents the arguments or specific values to pass to the primary instruction.

  • LABEL – Used to add metadata to an image.
  • FROM – This instruction defines the parent image for subsequent instructions. FROM clause must be the first entry in a Dockerfile.
  • RUN – Set the instructions to be executed during the image build.
  • CMD – Used to specify a command executed when you run a container.
  • ADD – This format copies the files and directories from the source to a destination.
  • ENV – It sets environment variables.
  • USER – It helps to Set the username when running the following commands “RUN, CMD, COPY, ADD.”
  • EXPOSE – It will be used to specify the port on which the container is used at the runtime.
  • ARG – It can allow you to define a variable.

Create a Dockerfile

Firstly, We will create a directory that will contain the docker file. After that, we will enter the directory with the following command.

mkdir ~/Docker
cd Docker

Now create a file named Dockerfile with the help of the vim command.

vim Dockerfile

Now copy and paste the following configuration in the Dockerfile. Your Dockerfile will look like this.

FROM Ubuntu:latest

RUN apt-get update 
RUN apt-get install apache2 -y

CMD apachectl -D FOREGROUND

Now we will understand the meaning of each of the lines in the Dockerfile.

  1. The first line defines the image which we will use in the Dockerfile. These images are pulled from DockerHub. We can pull and use any type of image from the docker hub.
  2. The second and third lines define what types of commands we will run in the container and what type of packages we will install into the container. All the commands will start with the RUN command.
  3. In the end, We use a default command that will be run on the execution time of the container.

When all the configurations are done, then Save the file and exit.

Build the Image

After creating the Dockerfile, now we need to build the Docker image from the Dockerfile. To build the image run the following command.

docker build .

When the building process got completed, It will show a Successful message as mentioned below.

Build a Docker Image
Linuxpanda

We can check the newly created docker image using the following command:-

docker image ls
Build a Docker Image
Linuxpanda

Create a Docker Container

Now that the image is created, then we launch the container using the previously created docker image.

docker run -d -p 80:80 64e163dde18c

In this command, we use the “-d” option to run the docker container in the detached mode, and “-p 80:80” defines the port, and what type of port we should use to run the docker container. Last, we add the image ID in the container run command.

when the container is started, we can see the list of total containers using the following command.

docker ps
Build a Docker Image
Linuxpanda

After the container has been created. To check whether the docker container is perfectly working or not, simply enter the server’s public IP address in the URL sections of the web browser. If the browser shows the default Apache web page, it means your Docker container is perfectly running.

apache2
Linuxpanda

Conclusion

In this tutorial, we have learned how to build a docker image using a docker file. Using Dockerfile, we can fastly build a docker image.

If you have any questions regarding this tutorial, please leave a comment.