Dockerfile is a simple text file that contains a list of commands that the Docker client calls while creating an image. It’s a simple way to automate the image creation process.
Here is an example:
Don’t panic, I will walk you through this line by line.
Step 1: Define of the base image.
FROM python:3.9
Step 2: Set a working directory
WORKDIR /code
Step 3: Copying the files and installing the dependencies.
COPY ./requirements.txt /code/requirements.txt
Step 4 : Install the dependencies
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
--no-cache-dir
: Avoids using a cache for downloaded packages, ensuring that the latest versions are fetched.--upgrade
: Upgrades already installed packages to the latest compatible versions.
Step 5: run the command and specify the host and port.
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
The CMD
tells the container which command it should run when it is started. With that, the Dockerfile
is now ready to go.
"--host", "0.0.0.0"
: Specifies that the server should listen on all available network interfaces."--port", "80"
: Specifies the port on which the server should liste
Dockerfile instructs Docker Client to create image and execute tasks line by line. Once you understand the purpose behind each line, the elegance and simplicity of the configuration become apparent.