If you are familiar with Docker, you probably know that you can create custom docker image using Dockerfile. I have written about in details.
In this tutorial, I will show you how to follow the same idea but for Python applications alone. This is going to be useful for both users and developers.
Windows + Anaconda. Download and install Anaconda (Python 3+) Open the Anaconda Prompt and go to the labelImg directory. Conda install pyqt = 5 conda install -c anaconda lxml pyrcc5 -o libs/resources.py resources.qrc python labelImg.py python labelImg.py IMAGEPATH PRE-DEFINED CLASS FILE. In this file, we will define the sequence of steps needed to create the Docker Image. Take a look at the below Dockerfile template. FROM python:3 WORKDIR /usr/src/app COPY. CMD 'test.py' ENTRYPOINT 'python3' In the above Dockerfile, we have pulled the Python 3 Base Image. We need to remember that docker images are read only structures and can run independently. On the other side, a container is built on top of an image and it needs an image to run itself. Run the docker image. Let us now run our created image to see the python script’s output from the container on the GIT BASH console. Docker image with uWSGI and Nginx for Flask web applications in Python 3.6, Python 3.5 and Python 2.7 running in a single container. Optionally using Alpine Linux. This Docker image allows you to create Flask web applications in Python that run with uWSGI and Nginx in a single container.
I’ll be using a minimal Python image. Once you've modified and revised that image, you'll no longer need to worry about installing your Python Application on different operating systems. You'll be able to straightaway run your Python application with Docker every single time. So, you can bid goodbye to those host-based installation hiccups!
I am going to use Miniconda here. Miniconda is a free minimal installer for conda and gives you a small, bootstrap version of Anaconda with just the bare-bones necessities you need to run Python applications.
There is more than one reason:
When you install Miniconda on a host, you are not actually using the Python version provided by the operating system's package manager. Miniconda installs on a separate location having its own Python environment. So, this provides you an added level of isolation when done on a Docker container.
Due to the above point, you also get another advantage: Since you are using
conda that Miniconda installed, you can use this tool to change the relevant Python version of your application as and when needed. This is a huge help for developers of applications that are based on say different versions of Python 3: They could be 3.6, 3.7, 3.8, 3.9 or earlier versions too.
For example, if by default you're running Python 3.9 but your Python application requires Python 3.7 because of relevant dependencies, what would you do?
This is where
conda can help you. With it, you can run
conda install python=3.7 to change the Python version required by installing it with all dependencies necessary.
g++). This is when the combined power of Miniconda and Docker becomes a great solution!
conda? Isolation again!
So, let us now go ahead with creating the new Python Application image with Miniconda and Docker!
If you have not already, please install Docker on Ubuntu or whichever Linux distribution you are using. Make sure to add yourself to the docker group so that you can run docker without sudo. You’ll need an active internet connection for downloading the base docker image.
For a sample Python application, I am using a simple 'Hello World!' example named
python-app.pyto make it easier for you to understand how to run it via Miniconda on Docker.
A full-fledged Python application that uses different Python libraries would greatly benefit from the same procedure, especially because of Miniconda's various dependency provisions.
I chose Python Slim in this example instead of Alpine Linux. The latter is really small but can greatly affect performance when it comes to running applications. However, Python Slim is around 40 MB in size, based on Debian Buster and Python 3.9.1.
This step is optional. I included it to show that you can compare it with the customized python application image like in the previous Dockerfile tutorial.
Pull the latest docker image of Python Slim using the
docker pull command:
Now let’s create a new empty file named Dockerfile using the touch command. But first create a directory for your docker app.
I will enclose the complete Dockerfile below after I've finished explaining the steps within the image building process via a step by step walk-through of the file.
Here's how to begin and progress building the image:
Update the latest default packages by using Python Slim as the base image.
Install any non-Python dependencies for your Python app(say
g++ and any other as per your requirement).
Git and Wget can be very handy when fetching Python Applications from different repositories and URLs. Finally, clean up some space with
rm -rf /var/lib/apt/lists/* for minimizing the final Docker image size.
After it's installed, Miniconda updates .bashrc to switch to its own Python version.
Here an environment variable is set within the container system path.
ENV is meant for the containers that you'll be running on the image and
ARG is meant for the intermediate containers that are created while it's built for the first time.
So, the difference between the
ARG instructions in the above code block is that the latter is only available while the image is being built. Check out this beautiful explanation here.
wget, download the latest version of Miniconda from the official Anaconda repository. After creating the essential configuration directory, install it and finally remove the installer.
After installing Miniconda, display its version number for confirmation and initialize it to the Bash shell for the container. The second line updates your default
Reload Bash for the Docker build system to switch to the Miniconda Python version rather than Debian's(the base OS image).
Also, update the current Miniconda packages bundled by default.
Create and activate a separate Conda environment for your Python Application.
Install the relevant Python version you need for your app. Assuming your application is based on Python 3.6, set this version within the new virtual environment alongwith Pip, which also comes very handy when managing Python applications.
Depending upon how you use your app, you can either:
i. Install it with pip that conventionally uses the
setup.py file available in your repository. It is the same tool discussed earlier but here I'm using it via Conda instead.
ii. ..or directly run it with the
In the demo Dockerfile, I'm going use the 'Hello World!' example to make it easier for you to understand how you can run it directly by launching a container or inside its bash shell with Docker. So, let's say I'm using the 2nd way:
So now that I've included the above line, a file called
python-app.py is created that is supposed to generate the Hello World message whenever you execute it with the command
The Miniconda installer automatically updates the .bashrc file after you run
conda init bash as shown earlier. You can do the same for your Python application as well. Whenever you run the container bash, the environment will be activated, and you can also use your Python application name as a command to run it. Here I've used the name as
Finally, I create an entrypoint and assign the command that will enable you to run it every-time you run a container based on this image:
You can use an editor like Vim or Nano or use the
cat command to add the above discussed lines to the Dockerfile.
When you try your own app, replace the
echo 'print('Hello World!')' > python-app.py line above with any of the two ways described in the Install your Python Application section above.
As you may already know, the command to build the modified Docker image from the Dockerfile looks like:
With the -t tag, you specify the name of your app's Docker image. I've set it as
python-app in the above example command.
Considering that the Dockerfile is in your current directory, you can create the new Docker image of your Python Application like this:
The above output is based on cached data but when you run it for the first time, it would take some time and produce a much longer log output.
Now, let’s verify that your modified Docker image has the example app installed by running a container from it:
So, through Docker and Miniconda, you are now able to run the program directly without any prior installation needed! From now onwards, all you need is the image.
Let us now login to the bash shell inside this container:
As you can see, you are now inside the Conda activated environment you created earlier through the Dockerfile. The
-ti flag is used to create an interactive terminal for you. You can now alternatively use the command you aliased to run the app:
Let's also confirm that you are indeed using the Miniconda Python version and not the default Python version:
As I had mentioned earlier, Miniconda is a miniaturized version of Anaconda.
Once you have everything set, you can push your final image to Docker Hub if you host an Open Source Python application on GitHub, GitLab, Gitea, Bitbucket or any other repository.
Exit the container by typing exit in the terminal. Stop the container, remove the container and remove the Docker images (if you want to) to free up disk space.
Congrats! You just learned how to create your very own Docker image for your Python application.
So you see, not only does Miniconda help you make your application more flexible and future proof at the user level, it also makes the developer's role much easier.
Think of how convenient it would be for setting this up with PyCharm! You install Miniconda and your Python Application like you do on a host system, but since you build and save it as a Docker image, it becomes a one-time process only!
If you want, you can experiment with the different examples shown in this previous tutorial instead of the 'Hello World!' example that I've used in this one.
Hope this helps! If you have questions or suggestions, please leave a comment below.
You can run a Python script using Docker containers. This tutorial will help you to run a Python script over command line within Docker isolated environment.
Add the following content:
print('This is Python running in Docker');
Add below content to file.
Here you can choose your preferred Python version and operating systems for your Docker container.
The above command will create a Docker image with name img-python-example.
You can use “-d” option to run as demon mode.