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

Install and Run Python in Docker Container

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.

  1. How to install Python in a Docker Container?
  2. Run the Python script file in Docker Container
  3. 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.

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 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.

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 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.

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 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.

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 of 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

Get more detail about docker from here.

install python in docker
install python in docker

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.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner