Docker is now widely used for the deployment of any project to make runnable as the production server. There are many methods for running Python in Docker Container and here you will know all these methods in an easy way. You will learn the following things.
- How to install Python in a Docker Container?
- Run the Python script file in Docker Container
- How to set the Environmental Variables Container?
How to install Python in a Docker Container?
To install python in a docker container the first step is to run the Docker container as a background process. To run Container You will use the docker run command. Type the command written below and enter. I am running the ubuntu images from the docker hub,
docker run -ti -d ubuntu: latest
It will download the ubuntu images from the docker hub and run the container in the background. You can check it using the docker ps command.
Now let’s go inside the container using the docker exec command and install python in it.
docker exec -it container_name
In this example the name is musing_lichterman. You will get a random name if you have not defined it while running the container initially.
Update the container
apt-get update apt-get install python3
After all the things are done check the version of python using the command.
If there is output then You have installed python in a docker container.
How to Run the Python script file in Docker Container?
After installing the python you can create a python script and run it easily. But one thing you should note is that any editor is not available in the docker ubuntu container thus you have to first install it using the apt-get install command.
apt -get install nano
After installation lets create a run.py file and then write the line print(“Hello Data Science Learner”)and run it.
Run this script
How to set the Environmental Variables inside a Container?
In this section, you will learn how to set the environment variables inside the container. But before doing so you have to make sure you have the running containers as without them you are not able to do so. Suppose I want to add Our Python Package path to the PYTHONPATH. Type the following command and enter it.
docker exec -it -e PYTHONPATH=/path/to/your/project continer_id or name bash
I have created a folder named project. I want it to add to the path variable PYTHONPATH.
docker exec -it -e PYTHONPATH=/project musing_lichterman bash
It will add your project path to the PYTHONPATH. You can verify it using typing the env command. It will list out all the environment variables inside the docker.
After doing all the above things the last step is to save the docker container as an image. It is necessary because when you exit the container then all the things that you have done in this container will be lost. Therefore commit the docker with your name using the docker commit command.
docker commit musing_lichterman bash my-python-installed-image
It will create an image of all the changes made inside the container. you can share it or use it in other projects and as a backup.
Get more detail about docker from here.
Hope this article has cleared all the queries for installing and running python in a docker container. If you have any queries you can message us. You can also contact us on the data science learner facebook page.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.