DEV Community

Cover image for Dockerfile For A Python Playground On Ubuntu
Matthew Cale
Matthew Cale

Posted on

Dockerfile For A Python Playground On Ubuntu

What is this?

This is a tutorial on getting a supremely simple Python3 playground running on an Ubuntu Docker container. Like my first tutorial on Ubuntu and Docker this is not extremely useful, but it is extremely simple and that goes a long way in a field where things can get very tricky very fast.

Why make this?

To keep it in a spot I remember and to share simple things with others. In the future you might be glad you have a starting point for tinkering with Python and Ubuntu together Matt!

Steps

  • Create a directory and Dockerfile


# From the terminal 
mkdir ubuntu-python-playground && \
cd ubuntu-python-playground && \
touch Dockerfile


Enter fullscreen mode Exit fullscreen mode
  • Open the Dockerfile in a text editor (use VSCode if you're a human and use Vim if you're 3xtra l33t) and fill it in with the requirements for getting Python to run on an Ubuntu container.


# Using the Ubuntu image (our OS)
FROM ubuntu:latest
# Update package manager (apt-get) 
# and install (with the yes flag `-y`)
# Python and Pip
RUN apt-get update && apt-get install -y \
    python3.8 \
    python3-pip


Enter fullscreen mode Exit fullscreen mode
  • Make your new fancy image


# From the terminal 
docker build -t ubuntu-python-playground-img .


Enter fullscreen mode Exit fullscreen mode
  • Run the image interactively and get a Python playground set up


# Run the image / make a container jump into it
docker run -it ubuntu-python-playground-img bash
# Install some libraries that do fun things
pip3 install Requests Pygments
# Open the Python REPL
python3


Enter fullscreen mode Exit fullscreen mode
  • Once you're in the Python REPL feel free to run some totally bomb python code ๐Ÿ. Since we installed some fun packages in the step before consider trying them out!


# There will be a little >>> symbol 
# this indicates you're in the Python REPL

# Get some cool libs imported
from requests import get
from pygments import highlight
from pygments.lexers.data import JsonLexer
from pygments.formatters import TerminalFormatter

# Get and format some JSON
r = get('https://jsonplaceholder.typicode.com/users/2')
print(highlight(r.text, JsonLexer(), TerminalFormatter()))


Enter fullscreen mode Exit fullscreen mode
  • Enjoy ๐Ÿ™‹โ€โ™€๏ธ

Screen Shot 2021-09-08 at 11.26.55 AM

Alternative Approach

This works well for running Python in the REPL, but what if I wanted to write the Python code in my text editor, but run it on the container?

Well, with a few changes to the Dockerfile you can! To do this we will copy and paste the Python code example above into a file called script.py so our ubuntu-python-playground directory from before now has two files in it.

  • Dockerfile (the one we have been working with)
  • script.py (the code we previously ran in the REPL)

Once our script.py file is in place we will just make some changes to the Dockerfile and re-run it.

Steps For Alternative Approach

  • Change the Dockerfile to look like this:


# Using the Ubuntu image (our OS)
FROM ubuntu:latest
# Update package manager (apt-get) 
# and install (with the yes flag `-y`)
# Python and Pip
RUN apt-get update && apt-get install -y \
    python3.8 \
    python3-pip

# =====
# The new stuff is below
# =====

# Install our Python dependencies
RUN pip install Requests Pygments

# Copy our script into the container
COPY script.py /script.py

# Run the script when the image is run
ENTRYPOINT ["python3", "/script.py"]



Enter fullscreen mode Exit fullscreen mode
  • Build and run the new image


docker build -t ubuntu-python-playground-img .
docker run ubuntu-python-playground-img 


Enter fullscreen mode Exit fullscreen mode
  • Enjoy ๐Ÿ™‹โ€โ™€๏ธ

Screen Shot 2021-09-08 at 12.13.04 PM

Resources For Extra Learning

Top comments (0)