Run Python Versions in Docker: How to Try the Latest Python Release

Run Python Versions in Docker: How to Try the Latest Python Release

by Geir Arne Hjelle intermediate docker

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 using Docker, including how you can have the latest alpha running on your computer within minutes.

In this tutorial, you’ll learn:

  • Which versions of Python are available
  • How to get started with Docker
  • How to run different Python versions in Docker containers
  • How to use Docker containers as Python environments

Let’s get started!

Understanding Python Versions and Docker

The long journey of moving from Python 2 to Python 3 is coming 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:

  1. Released versions: Typically, you’ll be running something like Python 3.6, 3.7, or 3.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.

  2. Development versions: The Python community is continuously working on new versions of Python. At the time of this writing, Python 3.9 was under development. To preview and test new features, users have access to development versions labeled alpha, beta, and release candidate.

  3. Implementations: Python is a language that has several implementations. An implementation of Python contains an interpreter and corresponding libraries. CPython is the reference implementation of Python and the one that is most commonly used. However, there are other implementations like PyPy, IronPython, Jython, MicroPython, and CircuitPython that cover specific use cases.

You’ll usually see which version of Python you’re using when you start a REPL. You can also inspect sys.implementation for more information:

Python
>>> import sys
>>> sys.implementation.name
'cpython'

>>> sys.implementation.version
sys.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 like pyenv and conda 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.

Using Docker

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 and microservices. In this section, you’ll see the fundamental concepts you’ll need to know to use Docker.

Installing Docker

Docker is available on all major operating systems: Windows, macOS, and Linux. See the official guide for instructions on how to install Docker on your system. Unless you have special needs, you can use the Docker Engine - Community version.

Running Containers

Docker uses the concepts of images and containers. An image is a self-contained package that can be run by Docker. A container 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 the hello-world image:

Shell
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:451ce787d12369c5df2a32c85e5a03d52cbcef6eb3586dd03075f3...
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
[ ... Full output clipped ... ]

The first lines show that Docker downloaded hello-world from Docker Hub. When it runs this image, the resulting container produces a "Hello from Docker!" message that prints to your terminal.

Building Your Own Images Using Dockerfiles

You can create your own images using Dockerfiles, which is a plain text file that describes how a Docker image should be set up. The following is an example of a Dockerfile:

Dockerfile
 1FROM ubuntu
 2RUN apt update && apt install -y cowsay
 3CMD ["/usr/games/cowsay", "Dockerfiles are cool!"]

A Dockerfile consists of a list of Docker commands. In the example above, there are three steps:

  • Line 1 bases the image on an existing image called ubuntu. You can do this independently of which system you’re running Docker on.
  • Line 2 installs a program named cowsay.
  • Line 3 prepares a command that runs cowsay when the image is executed.

To use this Dockerfile, save it in a text file named Dockerfile, without any file extension.

Next, build an image from your Dockerfile:

Shell
$ docker build -t cowsay .

The command will give a lot of output as it’s building the image. -t cowsay will tag your image with the name cowsay. 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 containing Dockerfile.

You can now run your very own Docker image:

Shell
$ docker run --rm cowsay
 _______________________
< 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.

The docker command line is very powerful. Use docker --help and the official documentation for more information.

Running Python in a Docker Container

The Docker 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 a Docker image with all currently available versions of Python. In this section, you’ll learn how to run different Python versions in Docker.

Playing With the REPL

When you run a Python image from Docker Hub, the interpreter is set up so you can play with the REPL directly. To start the REPL in a Python container, run the following command:

Shell
$ docker run -it --rm python:rc
Python 3.8.0rc1 (default, Oct  2 2019, 23:30:03)
[GCC 8.3.0] on linux