"From Ubuntu to Running Flask: My First Dockerfile Journey"

Pragya SaraswatPragya Saraswat
4 min read

Understanding Dockerfile Instructions: A Beginner-Friendly Walkthrough

If you're just getting started with Docker and wondering what each line in a Dockerfile actually does, this post is for you. I've explained each instruction in a simple way—layer by layer—so that you can understand how Docker builds images, handles commands like CMD and ENTRYPOINT, and even how to manage environment variables. Whether you're deploying Flask apps or experimenting with MySQL, you'll walk away with a solid base to start containerizing your own apps.

Dockerfile

INSTRUCTION ARGUMENT
FROM ubuntu
RUN apt-get update
RUN apt-get install
RUN pip install flask
RUN pip install flask-mysql
COPY . /opt/source-code
ENTRYPOINT FLASK_APP=/opt/source-code/app.py flask run
docker build -t my-custom-app Dockerfile
docker push my-custom-app
  1. OS Ubuntu 120 mb

  2. update apt repo 306 mb

  3. install dependencies using apt 6.3 mb

  4. insatll python dependencies using pip 6.3 mb

  5. copy source code tp /opt folder 229 B

  6. Run the web server using "flask" command 0B

Each of these instructions instruct the docker to perfom task while creating the image.

1. what is the base OS should be for the container

(Every docker image must be based of an another image either an image or os, that was created before based on an os ).

start from a base os or another image
Docker image must start from a FROM Instruction .

Layered Architecture

  • Docker build an image in a layered architecture . each of instructions creates a new layer in the doker image with just the chnages from the previous layer.

  • Since each layer only stores the chnages from the previous layer . it is reflected in the size as well.

ALl the layers builds are cached so the layered architecure helps you to restart the docker build from that particular step in case it fails or if you were to add new steps in the build process you wouldnt have to start all over again.

docker build -t <iamge name>

-t for to set with the image name

-To make the instance of that image

docker run -p 8282:80 <image name>

ENVIRONMENT VAR :

docker run -e APP_COLOR=blue -p 3821:8080 --name blueapp -d <imagename>
docker run --name mysqldb -e MYSQL_ROOT_PASSWORD=db-123 -d mysql:8

docker CMD VS ENTRYPOINT

CMD command param1 (in a shell form)
CMD {"Command", "param1"} - its in a json array format

In json format , first command should be executable

CMD ["bash"]

Here bash is not a process like webserver or db server . it is a shell that listens for input from a terminal. if it cant find a terminal it exits.

By default , docker doesnt attach the terminal to a container when it is run , so the bash program doesnt find the terminal and so it exits.

SInce the process that was started when the conatiner was created finished. the container exits as well.

- How to do you specify the different command to start the conatiner .

  1. append a command to the docker run command and the way it overrides the default command that specifies within the image .
docker run ubunut [command]
docker run ubuntu sleep 5

this waay the conatiner starts its runs the sleep program waits for 5 seconds and then exits .

but how do you make that chnage permanent .

FROM ubuntu
CMD sleep 5
docker run ubuntu-sleeper sleep 10  # commad at startup :sleep10
docker run ubunut-sleeper 10

(only passing the no. of seconds the container should sleep and the sleep command should be involed automatically) and that is where the entry point comes at the place

FROM ubuntu
ENTRYPOINT ["sleep"]

the entry point instruction is like the command instruction as you can be specify the program that will run when the container starts .

IN CMD, the cmd line parameters will get completely replaces entirely. IN case of entry point . the cmd line parameter will be appended.

If you miss appending commadn line parameter at entry point . it give you error "missing operand".

docker run ubuntu-sleeper  # sleep:missing operand

commad at startup:sleep

how do you configure default value of cmd - if not specify in the cmd line. thats where you used entrypoint as wellas the command insteruction.

command instruction will be appended to the entrypoint instruction.

FROM ubuntu
ENTRYPOINT ["sleep"]
CMD ["5"]

command at startup: sleep 5

docker run ubuntu-sleeper 20  # it will be over write

when you want to modify entry point at command line .

docker run --entrypoint sleep2.0 ubuntu-sleeper 10

command at startup:sleep2.0 10

0
Subscribe to my newsletter

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

Written by

Pragya Saraswat
Pragya Saraswat