The Dockerfile allows the user to create their application images. This file describes the instructions that specify what environment to use and which commands to run to execute the user's file.
There are four steps in dockerizing any application:
Let's understand this by dockerizing a Python application:
Start with the new empty directory, which will contain all the essential things you need to build an image for your application.
Create a file with the name, Dockerfile
, without any extension.
Note: No extension is required for the Dockerfile. And you can use any text editor to edit it.
We'll use the Python3 image as our launching point in this Answer.
FROM python:3
Add this line to your Dockerfile. Since we want to run some Python script, myScript.py
, you also need to add the script to your DockerFile.
ADD myScript.py /
You'll add this script to our Dockerfile as well. Now, it's time to add the dependencies of your application. For example, our Python script depends on the myScript.py
.
RUN pip install pystrich
Add this line to the D
ockerfile to install this library. You can use the following command is used to run the script.
CMD [ "python", "./myScript.py" ]
Finally, the Dockerfile will look like this:
FROM python:3ADD myScript.py /RUN pip install pystrichCMD [ "python", "./myScript.py" ]
All the elements are self-explanatory. Now, create your application :
Our Python application will look like this:
from pystrich.datamatrix import DataMatrixEncoderencoder = DataMatrixEncoder('This is a DataMatrix.')encoder.save('./datamatrix_test.png')print(encoder.get_ascii())
As our docker file is ready and our application is prepared, you need to build an image using your Dockerfile.
You'll use the following command to build the image of your application:
docker build -t python-app .
You can also run this image as a container in your local terminal.
Note: To view all docker images, we can use the
docker images
command.
As we build our application's image with the name python-app
, you can run it by running the following command:
docker run python-app
To run a single file script, you can prevent writing a complete Dockerfile. In the examples below, assume you store myScript.py
in /my-first-docker-app/
, and you want to name the container my-first-python-script
:
docker run -it --rm --name my-first-python-script -v "$PWD":/my-first-docker-app/ python:3 python myScript.py
Free Resources