Learn Docker networking with a Python website check and a MySQL query.
1.2K
Learn Docker networking with a Python website check and a MySQL query.
tree command to show the directory structureThe working directory is /x. The default command runs python ./my-test.py.
It requests https://www.google.com, then queries the user table in the database.
Requests use a 15-second timeout. A failed website check stops the program before the database query.
It runs once and exits. It does not start a web server.
| Tag | Meaning | Platform |
|---|---|---|
latest | Latest successful main build | linux/amd64 |
docker pull stden/my-python-app:latest
docker run --rm stden/my-python-app:latest \
python -c 'import requests, flask, numpy, mysql.connector; print("Libraries are ready.")'
To run the full example, prepare a MySQL database with the sample SQL. The database must accept connections before you start the application.
In this example, a MySQL container named db is on the course-net Docker network.
Set the database user and password to match your database. change-me is a placeholder.
docker run --rm --network course-net \
-e DB_HOST=db -e DB_USER=user -e DB_PASSWORD=change-me -e DB_NAME=my_db \
stden/my-python-app:latest
The repository includes a Compose file that starts MySQL, creates the sample data, waits for MySQL, and builds the Python application:
git clone https://github.com/stden/docker-gems.git
cd docker-gems
docker compose up --build --abort-on-container-exit --exit-code-from app
docker compose down
The Compose file uses example passwords. The named database volume keeps its data after
docker compose down. Use a separate project and credentials for your own application.
GitHub Actions runs the Compose example twice: first with a new volume, then with the same volume.
Dockerfile · Compose file · Docker tutorial · Build results
Content type
Image
Digest
sha256:d77301b2f…
Size
430.2 MB
Last updated
2 days ago
docker pull stden/my-python-app