Table of Contents
There’s always a new version of Python under development. However, it can be cumbersome to compile Python yourself to try out a new version! As you work through this tutorial, you’ll see how to run different Python versions usingDocker, including how you can have the latest alpha running on your computer within minutes.
In this tutorial, you’ll learn:
Let’s get started!
Free Download:Get a sample chapter from Python Tricks: The Book that shows you Python’s best practices with simple examples you can apply instantly to write more beautiful + Pythonic code.
The long journey of moving from Python 2 to Python 3 iscoming to a close. Still, it’s important that going forward, you know about the different versions of Python and how to try them out. In general, there are three different kinds of versions you should be aware of:
Released versions: Typically, you’ll be running something like Python3.6,3.7, or3.8. Each of these versions adds new features, so it’s good to be conscious of which version you’re running. For instance,f-strings were introduced in Python 3.6 and won’t work in older versions of Python. Similarly,assignment expressions only became available in Python 3.8.
Development versions: The Python community is continuously working on new versions of Python. At the time of this writing,Python 3.9 wasunder development. To preview and test new features, users have access to development versions labeledalpha,beta, andrelease candidate.
Implementations: Python is a language that has several implementations. An implementation of Python contains aninterpreter and correspondinglibraries.CPython is the reference implementation of Python and the one that is most commonly used. However, there are other implementations likePyPy,IronPython,Jython,MicroPython, andCircuitPython that cover specific use cases.
You’ll usually see which version of Python you’re using when you start aREPL. You can also inspectsys.implementation
for more information:
>>>importsys>>>sys.implementation.name'cpython'>>>sys.implementation.versionsys.version_info(major=3, minor=9, micro=0, releaselevel='alpha', serial=1)
You can see that this code is running the first alpha version of CPython 3.9.
Traditionally, you’d use tools likepyenv
andconda
to manage different Python versions. Docker can replace these in most cases, and it’s often simpler to use. In the rest of this tutorial, you’ll see how to get started.
Docker is a platform for running containers with prepackaged applications. It’s a very powerful system that’s particularly popular for packaging and deploying applications andmicroservices. In this section, you’ll see the fundamental concepts you’ll need to know to use Docker.
Docker is available on all major operating systems: Windows, macOS, and Linux. See theofficial guide for instructions on how to install Docker on your system. Unless you have special needs, you can use theDocker Engine - Community version.
Docker uses the concepts of images and containers. Animage is a self-contained package that can be run by Docker. Acontainer is a running image with a certain state. There are several repositories containing pre-built Docker images.Docker Hub is the default repository that you’ll use in this tutorial. For a first example, run thehello-world
image:
$dockerrunhello-worldUnable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-world1b930d010525: Pull completeDigest: sha256:451ce787d12369c5df2a32c85e5a03d52cbcef6eb3586dd03075f3...Status: Downloaded newer image for hello-world:latestHello from Docker!This message shows that your installation appears to be working correctly.[ ... Full output clipped ... ]
The first lines show that Docker downloadedhello-world
from Docker Hub. When it runs this image, the resulting container produces a"Hello from Docker!"
message that prints to your terminal.
You can create your own images usingDockerfiles, which is a plain text file that describes how a Docker image should be set up. The following is an example of a Dockerfile:
1FROMubuntu 2RUNaptupdate&&aptinstall-ycowsay 3CMD["/usr/games/cowsay","Dockerfiles are cool!"]
A Dockerfile consists of a list of Dockercommands. In the example above, there are three steps:
ubuntu
. You can do this independently of which system you’re running Docker on.cowsay
.cowsay
when the image is executed.To use this Dockerfile, save it in a text file namedDockerfile
, without any file extension.
Note: You can build and run Linux images on any platform, so images likeubuntu
are great for building applications that should be available cross-platform.
In contrast, a Windows image will only run on Windows, and a macOS image will only run on macOS.
Next, build an image from your Dockerfile:
$dockerbuild-tcowsay.
The command will give a lot of output as it’s building the image.-t cowsay
will tag your image with the namecowsay
. You can use tags to keep track of your images. The final dot in the command specifies the current directory as the build context for your image. This directory should be the one containingDockerfile
.
You can now run your very own Docker image:
$dockerrun--rmcowsay _______________________< Dockerfiles are cool! > ----------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
The--rm
option will clean up your container after use. It’s a good habit to use--rm
to avoid filling up your system with stale Docker containers.
Note: Docker has several commands for managing your images and containers. You can list your images and containers usingdocker images
anddocker ps -a
, respectively.
Both images and containers are assigned a 12-character ID that you can find in these listings. To delete an image or container, use eitherdocker rmi <image_id>
ordocker rm <container_id>
with the correct ID.
Thedocker
command line is very powerful. Usedocker --help
and theofficial documentation for more information.
TheDocker community releases and maintains Dockerfiles for all new versions of Python, which you can use to try out new Python features. Additionally, the Python core developers maintain aDocker image with all currently available versions of Python. In this section, you’ll learn how to run different Python versions in Docker.
When you run a Python image fromDocker Hub, the interpreter is set up so you can play with theREPL directly. To start the REPL in a Python container, run the following command:
$dockerrun-it--rmpython:rcPython 3.8.0rc1 (default, Oct 2 2019, 23:30:03)[GCC 8.3.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>>
This command will download thepython:rc
image from Docker Hub, start a container, and runpython
inside that container. The-it
options are necessary for running the container interactively. Therc
tag is shorthand forrelease candidate and points to the latest development version of Python. In this case, it’s the last release candidate ofPython 3.8:
>>>importsys>>>f"{sys.version_info[:]= }""sys.version_info[:] = (3, 8, 0, 'candidate', 1)"
The first time you run a container, it may take some time to download. Later invocations will essentially be immediate. You can exit the REPL like usual, for example, by typingexit()
. This also exits the container.
Note: The Docker Hub Python images are kept reasonably well up-to-date. As new releases mature, their alpha and beta versions are made available at therc
tag.
However, if you want to test out the absolute latest versions of Python, then the core developers’ image might be a better bet:
$dockerrun-it--rmquay.io/python-devs/ci-image:master
You’ll see a few more examples of using this imagelater.
For more installation options, you can also check out the complete guide toinstalling pre-release versions of Python.
You can find a list of all available Python images atDocker Hub.python:latest
will always give you the latest stable version of Python, whilepython:rc
will provide you with the most recent development version. You can also request specific versions likepython:3.6.3
orpython:3.8.0b4
, the fourth beta version of Python 3.8. You can even runPyPy using a tag likepypy:latest
.
A Docker container is an isolatedenvironment. Therefore, you usually don’t need to add avirtual environment inside the container. Instead, you can runpip
directly to install the necessary packages. To modify the container to include the extra packages, you use a Dockerfile. The following example addsparse
andrealpython-reader
to a Python 3.7.5 container:
1FROMpython:3.7.5-slim 2RUNpython-mpipinstall\ 3parse\ 4realpython-reader
Save this file with the nameDockerfile
. The-slim
tag in line 1 points to a Dockerfile based on a minimal Debian installation. This tag gives a significantly slimmer Docker image, but the downside is that you may need to install more additional tools yourself.
Other designations include-alpine
and-windowsservercore
. You can find more information about these image variants onDocker Hub.
Note: If you’d like to use a virtual environment inside a Docker container, then there’s one caveat you should be aware of. EachRUN
command runs in a separate process, which means that the typical activation of a virtual environment won’t work inside of a Dockerfile.
Instead, you should manually activate the virtual environment by setting theVIRTUAL_ENV
andPATH
environment variables:
FROMpython:3.7.5-slim# Set up and activate virtual environmentENVVIRTUAL_ENV"/venv"RUNpython-mvenv$VIRTUAL_ENVENVPATH"$VIRTUAL_ENV/bin:$PATH"# Python commands run inside the virtual environmentRUNpython-mpipinstall\parse\realpython-reader
SeeElegantly activating a virtualenv in a Dockerfile for more information.
To build and run your Dockerfile, use the following commands:
$dockerbuild-trp.[ ... Output clipped ... ]$dockerrun-it--rmrp
As you build the image, you tag it with the namerp
. This name is then used when you run the image, starting a new REPL session. You can confirm thatparse
has been installed in the container:
>>>importparse>>>parse.__version__'1.12.1'
You can also start containers that run custom commands:
$dockerrun--rmrprealpythonThe latest tutorials from Real Python (https://realpython.com/) 0 Run Python Versions in Docker: How to Try the Latest Python Release[ ... Full output clipped ... ]
Instead of starting a REPL, this runs therealpython
command inside therp
container, which lists the latest tutorials published onReal Python. For more information about therealpython-reader
package, check outHow to Publish an Open-Source Python Package to PyPI.
In this section, you’ll see how to run scripts inside Docker. First, save the following example script to a file namedheadlines.py
on your computer:
# headlines.pyimportparsefromreaderimportfeedtutorial=feed.get_article(0)headlines=[r.named["header"]forrinparse.findall("\n##{header}\n",tutorial)]print("\n".join(headlines))
The script first downloads the latest tutorial fromReal Python. Then it usesparse
to find all headlines in the tutorial and prints them to the console.
There are two general ways to run scripts like this in your Docker container:
The first option is especially useful during testing, as you don’t need to rebuild your Docker image when you make changes to your script. To mount your directory as a volume, use the-v
option:
$dockerrun--rm-v/home/realpython/code:/apprppython/app/headlines.pyUnderstanding Python Versions and DockerUsing DockerRunning Python in a Docker ContainerConclusionFurther Reading
The option-v /home/realpython/code:/app
says that the local directory/home/realpython/code
should be mounted as/app
inside the container. You can then run the script with the commandpython /app/headlines.py
.
You’ll want to copy your script into your container if you’re going to deploy your script to another machine. You do this by adding a few steps to your Dockerfile:
FROMpython:3.7.5-slimWORKDIR/usr/src/appRUNpython-mpipinstall\parse\realpython-readerCOPYheadlines.py.CMD["python","headlines.py"]
You set a working directory inside your container to control where commands are run. You can then copyheadlines.py
to that working directory inside the container, and change the default command to runheadlines.py
withpython
. Rebuild your image as usual, and run the container:
$dockerbuild-trp.[ ... Output clipped ... ]$dockerrun--rmrpUnderstanding Python Versions and DockerUsing DockerRunning Python in a Docker ContainerConclusionFurther Reading
Note that your script is run when you run the container because you specified theCMD
command in the Dockerfile.
See thePython image description on Docker Hub for more information about building your own Dockerfiles.
So far, you’ve been pulling images from Docker Hub, but there are many image repositories available. For instance, many cloud providers likeAWS,GCP, andDigitalOcean offer dedicated container registries.
The core developers’ Python image is available atQuay.io. To use images from non-default repositories, you use the fully qualified named. For instance, you can run the core developers’ image as follows:
$dockerrun-it--rmquay.io/python-devs/ci-image:master
By default, this starts a shell session inside the container. From the shell session, you can explicitly run Python:
$python3.9-c"import sys; print(sys.version_info)"sys.version_info(major=3, minor=9, micro=0, releaselevel='alpha', serial=1)
You can see all available versions of Python by looking inside/usr/local/bin
:
$ls/usr/local/bin/2to3 get-pythons.sh pydoc3.5 python3.7m2to3-3.4 idle pydoc3.6 python3.7m-config2to3-3.5 idle3.4 pydoc3.7 python3.82to3-3.6 idle3.5 pydoc3.8 python3.8-config2to3-3.7 idle3.6 pydoc3.9 python3.92to3-3.8 idle3.7 python2.7 python3.9-config2to3-3.9 idle3.8 python2.7-config pyvenv-3.4codecov idle3.9 python3.4 pyvenv-3.5coverage mypy python3.4m pyvenv-3.6coverage-3.6 mypyc python3.4m-config pyvenv-3.7coverage3 pip3.5 python3.5 smtpd.pydmypy pip3.6 python3.5m stubgeneasy_install-3.5 pip3.7 python3.5m-config toxeasy_install-3.6 pip3.8 python3.6 tox-quickstarteasy_install-3.7 pip3.9 python3.6m virtualenveasy_install-3.8 pydoc python3.6m-configeasy_install-3.9 pydoc3.4 python3.7
This image is especially useful if you want totest your code on several Python versions. The Docker image is updated often and includes the latest development versions of Python. If you’re interested in checking out the latest features of Python, even before they’re officially released, then this image is a great choice.
In this tutorial, you’ve seen a quick introduction to working with different Python versions using Docker. This is a great way to test and see that your code is compatible with newer versions of Python. It only takes a few minutes to wrap your Python script in a Docker container, so you can try out the latest alpha as soon as it’s released!
Now you can:
As you test new Python versions in Docker, you’re providinginvaluable help to the Python community. If you have any questions or comments, then please leave them in the comments section below.
For more information about Docker, and especially workflows for larger projects, check outBuild Robust Continuous Integration With Docker and Friends.
You can also read about other examples of working with Python and Docker in the following tutorials:
🐍 Python Tricks 💌
Get a short & sweetPython Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.
AboutGeir Arne Hjelle
Geir Arne is an avid Pythonista and a member of the Real Python tutorial team.
» More about Geir ArneMasterReal-World Python Skills With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
MasterReal-World Python Skills
With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
What Do You Think?
What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.
Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students.Get tips for asking good questions andget answers to common questions in our support portal.
Keep Learning
Related Topics:intermediatedocker
Related Tutorials:
Already have an account?Sign-In
Almost there! Complete this form and click the button below to gain instant access:
"Python Tricks: The Book" – Free Sample Chapter (PDF)