How to write a basic docker File?

Meghal Chhabria
4 min readDec 20, 2020

--

Creating your first docker file.

I hope you guys are doing well. :)

Let’s start with our first docker file, where we will create a image with our python script to get a high level view of image creation, bundling scripts to image.

Great Things are not done by impulse, but by a series of small things brought together. ~ Vincent Van Gogh

With this note, lets follow this one-step at a time,

  • First create a directory and navigate inside it.
...# mkdir first-dockerfile
# cd first-dockerfile
  • Now a docker file
...# touch Dockerfile
  • Each docker file follows a set of pre-programmed instructions, let’s use some of them to create a image for our container

Content of my docker file:

...# cat Dockerfile# Use python runtime image as base image
FROM python:3
#set working directory to /usr/src/app
WORKDIR /usr/src/app
#copy requirement file for any external pkgs required for our app
COPY requirements.txt ./
#Install the pkgs mentioned in requirements file
RUN pip install --no-cache-dir -r requirements.txt
#Copy all the contents from first-dockerfile dir to container #working dir
COPY . .
#run the script
CMD [ "python", "./hello-world.py" ]

Also for your ease of understanding, currently my requirements.txt file is empty for the purpose of this article

Content of hello-world.py

...# cat hello-world.py#!/usr/bin/pythonprint(“Hello World, I’m Meghal, Nice to Meet you :)”)
  • Once these files are in place, run this one liner to create the image.
...# docker build -t first-dockerfile . // -t is the tag name
Sending build context to Docker daemon 3.584kB
Step 1/6 : FROM python:3
---> d1eef6fb8dbe
Step 2/6 : WORKDIR /usr/src/app
---> Running in 0214b61d282c
Removing intermediate container 0214b61d282c
---> 7a44a1bdf1bf
Step 3/6 : COPY requirements.txt ./
---> 5e060e9f2341
Step 4/6 : RUN pip install --no-cache-dir -r requirements.txt
---> Running in 95e0b04fb32b
Removing intermediate container 95e0b04fb32b
---> b0663aab964c
Step 5/6 : COPY . .
---> eecb6bdaf42e
Step 6/6 : CMD [ "python", "./hello-world.py" ]
---> Running in f37c35d28ead
Removing intermediate container f37c35d28ead
---> e655c9b8fe26
Successfully built e655c9b8fe26
Successfully tagged first-dockerfile:latest

Lets analyze the workflow when this one liner is executed.

  1. First of all the image is pulled i.e. mentioned in FROM ( in our example its python 3), once the image is pulled successfully, its runs the image as container.
  2. So internally it does docker commit of Python image with our app scripts. It basically adds a layer on top of the existing python3 image.
  3. Similarly, from last committed stage it executes the instructions until the end of docker file, and add the layer for each instruction using docker commit. This statement can be verified in the log mentioned. If you see after each step the image id changes.
  4. So this process continues till the last instruction is executed.

*Note : If some instruction fails to execute, still the image is created with the previous instruction.

To check the image , use docker images command,

...# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
first-dockerfile latest e655c9b8fe26 12 minutes ago 890MB

Also you can check the history of the image, which is very helpful to debug incase of errors,

...# docker history first-dockerfile
IMAGE CREATED CREATED BY SIZE COMMENT
e655c9b8fe26 11 minutes ago /bin/sh -c #(nop) CMD ["python" "./hello-wo… 0B
eecb6bdaf42e 11 minutes ago /bin/sh -c #(nop) COPY dir:181bfda60b7c75613… 516B
b0663aab964c 11 minutes ago /bin/sh -c pip install --no-cache-dir -r req… 5.44MB
5e060e9f2341 12 minutes ago /bin/sh -c #(nop) COPY file:fd3aeac6820b476e… 0B
7a44a1bdf1bf 12 minutes ago /bin/sh -c #(nop) WORKDIR /usr/src/app 0B
d1eef6fb8dbe 2 days ago /bin/sh -c #(nop) CMD ["python3"] 0B

If you observed properly in the above logs, each instruction executed successfully is logged here, which helps in backtracking.

Now, once you do docker run first-image , it should print out the message on console from our python file.

# docker run first-dockerfile
Hello World, I’m Meghal, Nice to Meet you :)

So I hope, now you will able to create a basic image with your app running inside it. For any queries reach out to me on LinkedIn. Will be more than happy to help :)

That’s all for today.

Please take a minute to appreciate your efforts. You made it. Learnt something new. :) Congratulations

Article 1 : Hello World Docker

Article 2 : Docker Terminologies

Article 3 : Docker Basic Commands. PS Cheat Sheet Ep-01

Article 4 : Docker Images! WTH is that?

Follow me on linkedin,

Stay tuned , Keep Learning. Cheers :D

Thanks for reading 👍🏻

--

--