Python with Docker



This tutorial documents the way of using Python3 with docker containers, which may be alternative path to installing Python3 on your system. 


Docker containers with Python3 support is growing within multiple IDE / editors for developers. Let's try to execute some simple program written in python within docker container environment.

1. Install Docker Desktop app as per user guide published here

https://docs.docker.com/desktop/install/mac-install/

2. Start Docker app and wait for it to be completely ready - it will take some time for the first time. Open Terminal app and check that everything is working by running any docker command, for example

$ docker images

Execution of this command should not raise any errors if installation was successful


  Note

  If you are experiencing any problems with docker installation - please try to find solution here

  https://docs.docker.com/desktop/troubleshoot/overview/


3. To start working with Python3 - we need to download docker image for latest version, execute this docker command in Terminal app

$ docker pull python:latest

This image gets updates from time to time - to get latest version after it was downloaded on your system you can execute this same command again


4. Once image download process been finished, we can execute this docker command to start using python in terminal app


$ docker run --rm -ti python:latest

This will open Python prompt where you can type simple Python command for execution


>>> print("Hello World")

To exit from this docker container you can simply type this python command

>>> exit()


5. For more advance scenario - you can add python file created on your system for execution inside of docker container. Lets create file example.py with same python command as before inside of the file by executing this command in Terminal app


$ echo 'print("Hello World")' > example.py


We can execute this python file inside of docker container by running following command


$ docker run --rm -ti -v "$(pwd)/example.py":"/home/example.py" python python /home/example.py

6. In more broad sense to make use of docker containers, we would need to start making progress with our own docker images. Lets create this file and name it "Dockerfile" with following lines inside

FROM python:latest

RUN mkdir -p /app/src
WORKDIR /app/src
ENV PATH /app/src:$PATH
CMD ["python3"]


7. Lets build our own version of python image with following command


$ docker build -t mypython .


8. With this changes we can now create following alias to execute any python script in the current folder inside our own built docker python image / container 



$ alias dockerpy='docker run --rm -ti -v "$(pwd)":"/app/src" mypython python'


$ dockerpy example.py



9. You can also use this custom created python docker image with IDE / editors for developers.


We can cover this in more details in tutorials dedicated for that type of use:


Visual Studio Code with Docker for Python




Post a Comment

Previous Post Next Post