Docker is a tool that makes it easy to run applications in containers. Containers are like small packages that hold everything an application needs to run. To create these containers, developers use something called a Dockerfile.

A Dockerfile is like a set of instructions for making a container. It tells Docker what base image to use, what commands to run, and what files to include. For example, if you were making a container for a website, the Dockerfile might tell Docker to use an official web server image, copy the files for your website into the container, and start the web server when the container starts.

1. What Is Dockerfile?

As we know docker image is a read-only file or a template with a set of instructions that is used to create a container. Usually, we used to create a docker image from a file that's called a Dockerfile.

So, In simple terms, Dockerfile is a simple text file that consists of instructions to build Docker images.

While creating a docker file letter 'D' should be a Capital letter.

Like this : vim Dockerfile / nano Dockerfile

2. Dockerfile Commands

Dockerfile follows a set of commands to build an image. All commands will be in capital form.

  1. FROM: Defines a base image, it can be pulled from the docker hub
    (for example- if we want to create a javascript application with the node as the backend then we need to have node as a base image, so it can run node application). This will be the first command in a docker file always.

    eg: FROM ubuntu:20.4

  2. RUN: RUN is an image build step, the state of the container after a RUN command will be committed to the container image. A Dockerfile can have many RUN steps that layer on top of one another to build the image.

    eg: RUN apt-get install tree -y

  3. COPY: It is used to copy your local files/directories to Docker Container.

    eg: COPY . .

    First . [dot] is used as a source which means if there are some files present in your current working directory they will be copied into the docker file i.e. second . [dot]

  4. WORKDIR: Defines the working directory for subsequent instructions in the Dockerfile(Important point to remember that it doesn't create a new intermediate layer in Image).

    eg: WORKDIR /app

    This means after creating the container working directory will be /app.

  5. EXPOSE Documents in which ports are exposed (It is only used for documentation).

    eg: EXPOSE 8000

    If we want to access our application from outside for that we need to expose our container port here i.e 800 [suppose].

  6. CMD: Command to be executed when running a container( It is asked to have one CMD command, If a Dockerfile has multiple CMDs, it only applies the instructions from the last one.

    eg: CMD ["python", "hello.py"]

    Whatever command you want to run just keep it inside of this [] bracket as per the example.

  7. ENTRYPOINT: This is where the ENTRYPOINT instruction shines. The ENTRYPOINT instruction works very similarly to CMD in that it is used to specify the command executed when the container is started. However, where it differs is that ENTRYPOINT doesn't allow you to override the command.

    eg: ENTRYPOINT ["echo","hello world"]

  8. ENV - Sets environment variables inside the image.

    eg: ENV env_name xyz

There are a few more commands like VOLUME, ADD etc...but these are some important commands to write a docker file.

3. Docker Image Layers Architecture:

Docker images consist of many layers. Each layer is built on top of another layer to form a series of intermediate images. This arrangement ensures that each layer depends on the layer immediately below it. The way layers are placed in a hierarchy is very significant. It allows you to place the layers that frequently change high up the hierarchy so that you manage the Docker image’s lifecycle efficiently.

Changes made to a Docker image layer trigger Docker to rebuild that particular layer and all other layers built from it. Making changes to a layer at the top of the stack ensures the rebuilding of the entire image using fewer computational resources. This means you should keep the layers with the least or no changes at the bottom of the hierarchy formed.

Task:

Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)

Here I will be creating referring a simple short python code, that calculates the growth, on taking inputs from the user.

Create a directory docker and do all activity in it.

Here is the code, I save it in app.py

import streamlit as st

st.title("Hello Streamlit")
st.header("Calculate % Growth")
initial = st.number_input("Initial investment in USD")
yr = st.number_input("Growth Period in years")
growth = st.number_input("Growth Rate in %")
terminal_value = 0
current_val = initial
for year in range(int(yr)):
   current_val += growth * current_val
   terminal_value = current_val

# perform cashflow projections for the next 5 years
st.write(f'Terminal value of {initial} after {yr} years at a growth rate of {growth} is {terminal_value}')

This project requires Python libraries to be installed for it to run. The libraries will be recorded in a requirements.txt file. Copy the contents below into your requirements.txt file.

echo "streamlit" requirements.txt

Docker Setup

The dockerfile required for this project mainly has to achieve the following logical steps:

  1. Create base image

  2. Copy source code

  3. Install requirements and dependencies

  4. Expose required port

  5. Run the Streamlit app within the Docker environment

Copy the Docker commands below in a file and name it Dockerfile

FROM python:3.8

ENV MICRO_SERVICE=/home/app/webapp
# set work directory
RUN mkdir -p $MICRO_SERVICE
# where your code lives
WORKDIR $MICRO_SERVICE

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN pip install --upgrade pip
# copy project
COPY . $MICRO_SERVICE
RUN pip install -r requirements.txt
EXPOSE 8501
CMD streamlit run app.py

Build the image using the Dockerfile and run the container

With both files set up, you are ready to build and run your image. To build your image, run the command

docker login
docker build -t kshitibartakke/calculategrowth .

After the docker image is built, it is time to run your Docker image.

docker run -p 8501:8501 kshitibartakke/calculategrowth

Verify that the application is working as expected by accessing it in a web browser

The app is now running at the localhost address provided by the Docker interface.

Push the image to a public or private repository (e.g. Docker Hub )

Get the docker image info and run the command suggested by docker hub. To get the image info first run docker images command

docker images 
docker push kshitibartakke/calculategrowth:latest

Done! :D

Conclusion

In this article, we learn about the Docker Hub, building Docker images for both our usernames and organization. We then pushed those Docker images to our Docker Hub repository