Day 19 : Dockerfile

Sudipa DasSudipa Das
3 min read

1.What is Dockerfile?

A Dockerfile is a text document that contains all the commands the user could call on the command line to assemble an image. Using docker build users can create an automated build of a Dockerfile that contains several command-line instructions in succession.

The name of the docker file will always be Dockerfile and nothing else.

Let's see how to create and what to write in it:

A Dockerfile will always start with FROM.

E.g.:

--"FROM" is a mandatory keyword. FROM specifies the Base Image that the Docker images are to be built from according to our customization. Docker images are never built from scratch. They are built on base images.

--The "ADD" keyword is used to add files to the containers being built. ADD is used to copy files from the host machine to the container directory. It can also copy tar files and unzip them on the container directory.

In case we use "COPY" instead of "ADD", it does the same thing. The only difference between ADD and COPY is ADD can automatically unpack compressed files like tar, zip, gzip etc. whereas COPY cannot.

-- The "RUN" keyword is used to add layers to the base image by installing components. Each RUN statement adds a new layer to the docker image. RUN is a keyword that can be repeated multiple times. It is used to execute the Linux commands like installing a package, uninstalling, upgrading a package, creating a directory, or permitting users.

--The "Entrypoint" keyword helps to strictly run commands the moment the container is initialized. We can even use "CMD" instead of "ENTRYPOINT". CMD also executes a command or the script file containing multiple commands when the container is launched. The only difference between CMD and ENTRYPOINT is CMD can be overwritten by the user by passing certain arguments. Entrypoint will be executed irrespective of any argument specified.

--"ENV" is a keyword used for environment variables in the container run-time. It defines variables and values that can be used in Dockerfile.

Let's Create a Dockerfile for a Django todo application. Build the image using the Dockerfile and Push the image to a public or private repository (e.g. Docker Hub ).

Using the command vim open the dockerfile "vim Dockerfile". And the following code is present in the file.

Now to build an image from a Dockerfile you need to run the below command:

docker build . -t <image-name>

Once image is built you can check by "docker images" command.

Now we can see that the image is created. Let's push that image to docker-hub.

To achieve that, you need to connect to the remote repository from local via "docker login" command. Then it will ask for your docker-hub username and password.

Then you can push the image to docker-hub using "docker push" command.

And to run a container from an image you need to execute the below command:

docker run -p 8000:8000 sudipadas/django-todo-app:latest

Then you can see an application like below:

If this post was helpful, please follow and click the ๐Ÿ’š button below to show your support.

_ Thank you for reading!

_Sudipa

1
Subscribe to my newsletter

Read articles from Sudipa Das directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sudipa Das
Sudipa Das

DevOps || Git || Terraform || Cloud | Linux | Shell Scripting | Splunk | Kubernetes | Docker || Jenkins