Docker Project:
Deploying application in Docker Container by creating Dockerfile
Dockerfile:
Dockerfile is a simple text file that consists of a set of instructions to build Docker images. A Dockerfile that contains commands that are used to assemble an image. We can use any command that calls on the command line. Docker builds images automatically by reading the instructions from the Dockerfile. The docker build command is used to build an image from the Dockerfile. Docker runs instructions of Dockerfile in top-to-bottom order. The first instruction must be FROM to specify the Base Image.
Here, we are listing some commonly used instructions.
FROM: This instruction is used to set the Base Image for the subsequent instructions. A valid Dockerfile must have FROM as its first instruction.
Example: FROM ubuntu
RUN: This instruction is used to execute any command of the current image.
Example: RUN /bin/bash -c ‘source $HOME/.bashrc; echo $HOME’
WORKDIR: The WORKDIR is used to set the working directory for any RUN, CMD and COPY instruction that follows it in the Dockerfile. If the directory does not exist, it will be created by default.
Example: WORKDIR /var/www/html
COPY: This instruction is used to copy new files or directories from the source to the filesystem of the container at the destination.
Example: COPY abc/ /xyz
LABEL: We can add labels to an image to organize images of our project. We need to use LABEL instructions to set labels for the image.
Example: LABEL vendorl = “JavaTpoint”
CMD: This is used to execute the application by the image. We should use CMD always in the following form
Example: CMD [“executable”, “param1”, “param2”?]
This is the preferred way to use CMD. There can be only one CMD in a Dockerfile. If we use more than one CMD, only the last one will execute.
Tasks :
Create a Dockerfile for a simple web application (I used a Python app)
First, we build a simple web application on python as “app.py” & then use it to create a docker file.
To create a Dockerfile for a python app, create a file named “Dockerfile”.
Here view the Dockerfile & add a Script to it.
Build the image using the Dockerfile and run the container.
Build the image using the command “docker build . -t app.py”.
Verify that the application is working as expected by accessing it in a web browser
To verify that the application is working, you can access it in a web browser by going to “http://<public_ip>:5000".
Push the image to a public or private repository (e.g. Docker Hub).
Use the command “docker login” to log in to dockerhub in CLI. Then use the command “docker tag <image_name>:tag username/image_name”.
At last, perform the command “docker push docker_hub_user-id/<image_name”>
Thank you for reading this blog. I hope you find this interesting.
Source code link: GitHub
Follow me on LinkedIn