This talk will explain what Docker is, why developers would want to containerize their Python application, and then demonstrate how to dockerize two simple Python applications:
- a terminal-based random movie picker
- a FastAPI server that returns json
- Install Docker Desktop (if installed)
- Install Python 3 (if not installed)
- clone this repo to your machine
- cd into
example1directory - set up a Python env running Python 3.9:
python3.9 -m venv .venv(if you don't have python 3.9 already installed on your machine, you'll need to install it using your preferred package manager. I use homebrew, so I ran:brew install [email protected]) - activate the new virtual env:
. .venv/bin/activate - confirm the Python version is 3.9:
python --version - install the required packages into your virtual env:
pip install requests bs4 - play around with the "movie picker app" to get familiar with how it works:
python main.py - containerize this movie picker app using:
docker build -t random-movie-picker .(-tparameter gives the container image a tag of "random-movie-picker", making it easier to identify) - run the newly containerized app:
docker run -i -t random-movie-picker(-iparameter ensures the running container interactive while the-tparameter creates a pseudo-terminal connection to the Docker container) - Done! The movie picker app is now running inside a Docker container.
- clean up by hitting
command-Cto stop the container and typingdeactivateto end the virtual env python session
- cd into the
example2directory - set up a Python env running Python 3.12:
python3.12 -m venv .venv(if you don't have python 3.9 already installed on your machine, you'll need to install it using your preferred package manager. I use homebrew, so I ran:brew install [email protected]) - activate the new virtual env:
. .venv/bin/activate - confirm the Python version is 3.12:
python --version - install the fastAPI and uvicorn packages into your virtual env:
pip install fastapi uvicorn - in the root directory, start the uvicorn server to understand how it works:
uvicorn app.main:app. Open the link where Uvicorn is running:127.0.0.1:8000and check the HTTP responses from/,/docs, and/items/foobar - run
pip freeze --local > requirements.txtto generate the required packages file - containerize the api service:
docker build -t fastapi-demo . - run the containerized api service:
docker run -p 8000:8000 fastapi-demo(-p 8000:8000tells docker to expose the local port 8000 to the container's port 8000) - open
0.0.0.0:8000/on your browser (localhost:8000/should work too) and interact with the service. Sending thelocalhost:8000/items/foobarshould return{"item": "foobar"} - Done! The api service is running inside a Docker container. You can view the running containers on the "Docker Desktop > Containers" page
- clean up by hitting
command-Cto stop the container and typingdeactivateto end the virtual env python session
I didn't cover the .dockerignore file, but basically it works just like a .gitignore file, except it tells Docker what to ignore when copying files from the local "source" directory (.) into the docker image "working" directory.