Dockerfile. Dockerfile contains a set of… | by Nishad Joshi | Oct, 2024
Dockerfile contains a set of instructions to build a Docker image. It automates the process of creating a Docker image by specifying the base image, environment settings, application code, and the commands to run when the container starts.
Note: Docker can contanarize anthing
Contents of the Dockerfile
The Dockerfile is made up of [INSTRUCTION] [ARGUMENT]
Dockerfile follows the layered architecture and these layers comes in picture when a new image is build this helps Docker skips the steps which are already performed in the previous docker builds.
example:
FROM ubuntu
RUN apt-get update
RUN apt-get install python
RUN pip install flask
RUN pip install flask-mysql
COPY . /opt/source-code
ENTRYPOINT FLASK_APP=/opt/source-code/app.py flask run
To build the image with above instructions
docker build Dockerfile -t username/myapp:tag .#to push the docker image to the private repo, say on your dockerhub
docker login
docker push username/myapp:tag
- -t : stands for “tag” and allows you to assign a name to the Docker image
Now let’s breakdown the Dockerfile
Base Image:
Every Dockerfile starts with a base image
This can be predefined image or basic OS
in our case: FROM ubuntu
no tag was provide hence it pulled latest version
Executing Commands:
The RUN
instruction is used to execute commands in the shell, typically to install dependencies.
Copying Files:
The COPY
instruction copies files or directories from the host machine to the image. COPY
. →(current direcory ) to →/opt/source-code
Enterypoint:ENTRYPOINT
instruction allows you to configure a container that will run as an executable. It can be used in conjunction with CMD
Other features in Dockerfile
CMD:
The CMD
instruction specifies the default command to run when a container is started from the image. There can only be one CMD
instruction; if multiple are specified, only the last one takes effect.
CMD acts as an append to the ENTRYPOINT. CLI parameters when passed with docker run command replaces the CMD part of the Dockerfile and are appended to commands in Entrypoint.
Examples
# Dockerfile
FROM ubuntu:latest
CMD ["echo", "Hello, World!"]#build the File
docker build -t my-echo-app .
#run the container
docker run my-echo-app
#O/P: Hello, World!
docker run my-echo-app echo "Hello, Docker!"
#O/P: Hello, Docker!
The EXPOSE
instruction informs Docker that the container listens on the specified network ports at runtime. It does not publish the port but serves as documentation
The WORKDIR
instruction sets the working directory for any subsequent instructions (like RUN
, CMD
, etc.).
You can set environment variables using the ENV
instruction, which can be used later in the Dockerfile or by the application running in the container. ENV APP_HOME /usr/src/ap
At any point the Dockerfile fails to build the layered architecture allows the docker to build the dockerfile again from the failed checkpoint.
docker history image_name
Take an example
building the
checking the history of the image