Dockerfile For A Python Playground On Ubuntu
Tags: docker, ubuntu, tutorial, mcdau
Published: 2021-09-08 17:18:50

---
title: Dockerfile For A Python Playground On Ubuntu
published: true
description: What if I wanted to play around with Python running on Ubuntu inside Docker?
tags: Docker,Ubuntu,Tutorial,mcdau
cover_image: https://i.ibb.co/255XFCH/snake.jpg
---

## 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](https://dev.to/mattcale/simple-ubuntu-docker-playground-23g2) 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 

```bash
# 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.


```Dockerfile
# 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

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

- Run the image interactively and get a Python playground set up 

```bash
# 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!

```python
# 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31ent7wu06pqbqoz5fiv.png)

## 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:

```Dockerfile
# 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

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

- Enjoy 🙋‍♀️

![Screen Shot 2021-09-08 at 12.13.04 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4oobgavyt3qxrz63dz3i.png)
  

### Resources For Extra Learning
- [What is Docker RUN](https://docs.docker.com/engine/reference/builder/#run)
- [Best Practices for Docker RUN](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#run)
- [Basic Linux Python Install Instructions](https://docs.python-guide.org/starting/install3/linux/)