We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Dockerizing Python

Okay, so our first attempt at Dockerizing the Python script didn't work... let's fix it!

The problem is our image doesn't have the python interpreter installed, so it can't run the script.

Assignment

docker build -t bookbot -f Dockerfile.py .

It might take a few minutes to build the image... we're installing an entire Python interpreter after all!

Maybe now you can see why I like Go so much...

docker run bookbot

If Bookbot ran, then you did it correctly! We've bundled up the Python script, its required data, and its required runtime all into a nice little container image!

Run and submit the CLI tests.

Common technologies often have images available for ease of use. See the Python Official Image from DockerHub Official Images.

Tip

# Build from a slim Debian/Linux image
FROM debian:stable-slim

# Update apt
RUN apt update
RUN apt upgrade -y

# Install build tooling
RUN apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev

# Download Python interpreter code and unpack it
RUN wget https://www.python.org/ftp/python/3.10.8/Python-3.10.8.tgz
RUN tar -xf Python-3.10.*.tgz

# Build the Python interpreter
RUN cd Python-3.10.8 && ./configure --enable-optimizations && make && make altinstall

# Copy our code into the image
COPY main.py main.py

# Copy our data dependencies
COPY books/ books/

# Run our Python script
CMD ["python3.10", "main.py"]