The next step is to create a Dockerfile:
RUN mkdir -p /var/docker-example
COPY ./ /var/docker-example
ENTRYPOINT python /var/docker-example/main.py
First, we need to decide which base image we want to use in ours. We can search for images in Docker Hub. Official Python images can be found here https://hub.docker.com/_/python/.
Next we run some commands to create a directory in our Docker image where we can store our project. It would be /var/docker-example.
Then we copy our entire project.
Finally we set an entrypoint, which is the command and parameters that will be executed first when we run our Docker image.
As we’re using PyCharm, it creates files and directories that I do not need in our project, such as .idea, and venv. We’re also using Git as version control, so there is also a .git directory.
To handle this, we will create a .dockerignore file. It looks similar to the .gitignore file.
# Virtual Environment
# Project Settings
All that is left for us to do now is to build an image and then run it.
We can build our image by executing the command ‘docker build . -t docker-example’
Where . indicates that our context (Dockerfile) is in the current directory. We also want to add a tag called docker-example to it so that we can identify the image more easily.
Docker pulled Python:3.7 image. Since we didn’t have it before and then executed all steps from our docker file, docker-example is already built and can be used.
We can run it by simply typing ‘docker run docker-example’:
As we can see, our entrypoint command was executed successfully.
You can find this example on the repository: https://github.com/krzyszti/docker-example.
The benefits of using Docker are almost unparalleled by any other technology. There may be other containerization platforms on the market, but none of them have yet managed to provide a product that can rival Docker.
Among its many benefits, Docker can vastly reduce resource requirements, which also has the effect of reducing costs, and increasing speed. In addition, the tool’s popularity has ensured that it is now supported by the major cloud computing providers which is invaluable in ensuring good portability.
As we have shown in our example, using Docker with https://www.netguru.com/blog/python-software-development
Developers are easily able to harness the benefits of these two powerhouses working together.