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 lon…


This content originally appeared on DEV Community and was authored by Matthew Cale

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 trick 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
  • 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
  • Make your new fancy image
# From the terminal 
docker build -t ubuntu-python-playground-img .
  • 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
  • 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()))
  • 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"]

  • Build and run the new image
docker build -t ubuntu-python-playground-img .
docker run ubuntu-python-playground-img 
  • Enjoy ?‍♀️

Screen Shot 2021-09-08 at 12.13.04 PM

Resources For Extra Learning


This content originally appeared on DEV Community and was authored by Matthew Cale


Print Share Comment Cite Upload Translate Updates
APA

Matthew Cale | Sciencx (2021-09-08T17:18:50+00:00) Dockerfile For A Python Playground On Ubuntu. Retrieved from https://www.scien.cx/2021/09/08/dockerfile-for-a-python-playground-on-ubuntu/

MLA
" » Dockerfile For A Python Playground On Ubuntu." Matthew Cale | Sciencx - Wednesday September 8, 2021, https://www.scien.cx/2021/09/08/dockerfile-for-a-python-playground-on-ubuntu/
HARVARD
Matthew Cale | Sciencx Wednesday September 8, 2021 » Dockerfile For A Python Playground On Ubuntu., viewed ,<https://www.scien.cx/2021/09/08/dockerfile-for-a-python-playground-on-ubuntu/>
VANCOUVER
Matthew Cale | Sciencx - » Dockerfile For A Python Playground On Ubuntu. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/08/dockerfile-for-a-python-playground-on-ubuntu/
CHICAGO
" » Dockerfile For A Python Playground On Ubuntu." Matthew Cale | Sciencx - Accessed . https://www.scien.cx/2021/09/08/dockerfile-for-a-python-playground-on-ubuntu/
IEEE
" » Dockerfile For A Python Playground On Ubuntu." Matthew Cale | Sciencx [Online]. Available: https://www.scien.cx/2021/09/08/dockerfile-for-a-python-playground-on-ubuntu/. [Accessed: ]
rf:citation
» Dockerfile For A Python Playground On Ubuntu | Matthew Cale | Sciencx | https://www.scien.cx/2021/09/08/dockerfile-for-a-python-playground-on-ubuntu/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.