Install and Run Python in Docker Container

Install and Run Python in Docker Container : Must Read for Data Scientists

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 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 a background. You can check it using the docker ps command.

docker ps

run docker ubuntu as a background process

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 while running the container initially.

Update the container

apt-get update
apt-get install python3

After all the things done check the version of python using the command.

python3 --version

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 easily. But one thing you should note that any editor is not available in docker ubuntu container thus you have to first install it using the apt-get install command.

apt -get install nano

After install lets create a run.py file and then write the line print(“Hello Data Science Learner”)and run it.

nano run.py

Run this script

python3 run.py

creating and running python file inside the docker container

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 it 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 env command. It will list out all the environment variables inside the docker.

env

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 for all the changes made inside the container. you can share it or use it in other projects and as a backup.

make images from the container using the docker commit

Hope this article has cleared all the queries for installing and running python in a docker container. If you have any query you can message us. You can also contact us on the data science learner facebook page.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner