DAY 17 - Dockerfile

DAY 17 - Dockerfile

What is Dockerfile?

Dockerfile is a text file written in a specific format i.e., instructions and arguments format that contains all the commands a user could call on the command line to assemble an image.

FORMAT : INSTRUCTIONS arguments

Everything in caps is an instruction like FROM, RUN, COPY, etc., each of these instruct docker to perform a specific action while creating the image.

Everything written after that instruction is an arguments.

All Dockerfile must start with FROM instruction.

When docker builds the images, it builds these in a layered architecture. Each line of instruction creates a new layer in the docker image with just the changes from the previous layer.
When we change the Dockerfile and rebuild the image, only those layers which have changes are rebuilt. This is the part of what makes images so lightweight, small and fast.

Here are some of the most commonly used commands in a Dockerfile:

  • FROM: Specifies the base image that the new image will be built on top of. For example, you might use an official Node.js image as the base for an application that runs on Node.js.

  • RUN: Executes a command in the image. This command is run during the image build process. For example, you might use the RUN command to install necessary packages or dependencies for your application.

  • COPY: Copies files from the host machine to the image. For example, you might use the COPY command to copy the files for your application into the image.

  • ENV: Sets an environment variable in the image. For example, you might use the ENV command to set a variable that holds the version of your application.

  • EXPOSE: Specifies the ports that should be exposed on the container. For example, you might use the EXPOSE command to specify that port 8000 should be exposed on the container.

  • CMD: Specifies the command that should be run when a container is created from the image. For example, you might use the CMD command to specify that your application should be started when the container is created.

  • ENTRYPOINT: Specifies a command that will always be executed when the containers starts and cannot be changed using the CLI.

Once the image is created from the Dockerfile, you can use it to create as many containers as you want. Each container will be exactly the same and will run the same way.

Creating a Dockerfile

main.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def get_data():
    return [1,2,3]

if __name__=='__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

Dockerfile

FROM python:3.9-slim-buster
WORKDIR /app
COPY . .
RUN pip install flask
EXPOSE 5000
CMD [ "python", "main.py"]

Building Image from Dockerfile

docker build Dockerfile -t appname -> To build an image from Dockerfile.

Creating Container from the image

docker run -d appname
docker run -d -p hostport:containerport appname -> while specifying the ports.

Setting up inbound rules

A security group acts as a virtual firewall for your instance to control inbound and outbound traffic. For each security group, you add rules that control the inbound traffic to instances, and a separate set of rules that control the outbound traffic.

The inbound rule in your security group must allow traffic on all ports. It needs to do this because the destination port number of any inbound return packets is set to a randomly allocated port number.

By default, a security group includes an outbound rule that allows all outbound traffic.

We add inbound rules in security group of our instance so that we can access our application via browser or from anywhere using our IP with the specified port which we have given in our Dockerfile.
Without adding your port under inbound rule in the security group of your instance will not give you access to run it from anywhere.

Run your app in browser

Run your app in browser using http://your_public_ip:port_no. as shown in the below snapshot:

Conclusion

In conclusion,

A Dockerfile is a script used in Docker to automate the process of creating a Docker image. It contains a series of instructions that define the steps needed to build the image, including specifying a base image, adding dependencies, configuring settings, and executing commands.

Dockerfile play a crucial role in simplifying the deployment and scaling of applications by encapsulating the environment and dependencies required for an application to run consistently across different environments. They contribute to the portability, reproducibility, and efficiency of containerized applications, making Dockerfile a fundamental tool in the world of containerization and DevOps.

*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊

~Smriti Sharma✌