This repo is a template for building Docker images for Python and MySQL on a Docker network, both of which can have data loaded into them from the outset--the former through a Git clone, the latter through included data definition language and data manipulation language files. While the most basic form of this template creates programs that will contained on a single workstation, this template serves as a slightly more sophisticated version of Python's own virtual environments, as they include a linked SQL database and can have their own changes tracked through version control.
While the majority of a project's code will be in a Git repo hosted online, this template has the ability to save additional files that will be copied into the image containing that repo in locations that cause the files to be interspersed with those from the Git repo. For example, if the GitHub repo contains a directory repo/foo with no subdirectories, and this template has a file at ./src/foo/bar/file.txt, the image will have a file ./root/repo/foo/bar/file.txt. Including ./root/repo/foo/bar in the Git repo .gitignore keeps the files added in with this method from being added to the public Git repo.
- Replace
<<schema_name>>in "mysql/DDL.sql" and "mysql/DML.sql" with the name of the schema the project will use - Add the data definition language (the
CREATE TABLEstatements) to "mysql/DDL.sql" - Add the data manipulation language (the
INSERTstatements) to "mysql/DML.sql" - Replace
<<MySQL_root_password>>in "mysql/Dockerfile" with the MySQL root password the project will use
- Add version numbers to the packages in "requirements.txt"
- All all other non-standard library packages required by the project to "requirements.txt"
- Add any passwords the project needs to "src/repo_secrets.py"
- Add any other files needed in the project that cannot be committed to its public repository to "src/"
- In "Dockerfile"
- Replace
<<Python_version>>with the Python version being used for the project - Replace
<<Git_clone_URL>>with the URL for cloning the project repo - Replace
<<project_file_name>>with the name of the file the project will have in the container
- Replace
- In "docker-compose.yaml"
- Replace
<<Python_version>>with the Python version being used for the project - Replace
<<MySQL_root_password>>with the MySQL root password the project will use
- Replace
- Open the folder of the project using this template in Windows Explorer, then type
cmdin the address bar to open a Command Prompt window - Create the image by typing
docker compose buildin the Command Prompt window - Create a detached container from that image by typing
docker compose up -din the Command Prompt window
- Access the Python container by typing
docker exec -it python-container bashin the Command Prompt window - Interact with the program via the CLI
- Exit the Python container by typing
exitin the Command Prompt window
- Access the MySQL container by typing
docker exec -it mysql-container mysql --user=root --password=and then the value used for<<MySQL_root_password>>in the Command Prompt window - Select the usage statistics database by typing
USE `<<schema_name>>`;in the Command Prompt window - Type SQL statements into Command Prompt to run them
- Exit the MySQL container by typing
exitin the Command Prompt window
- Access the MySQL container by typing
docker exec -it mysql-container mysql --user=root --password=and then the value used for<<MySQL_root_password>>in the Command Prompt window - Dump the database contents to a file by typing
mysqldump -u root -p <<schema_name>> > filename.sqlwhere<<schema_name>>is replaced by the same schema name value used in the DDL and DML files andfilenameis the name the SQL file will have - If prompted for a password, enter the value used for
<<MySQL_root_password>>in the Command Prompt window - Exit the MySQL container by typing
exitin the Command Prompt window - Copy the file from the container to the local machine by typing
docker container cp mysql-container:/filename.sql .wherefilenameis the name given to the SQL file in themysqldumpcommand
- Exit out of any containers
- Destroy the containers by typing
docker compose down -vin the Command Prompt window - Destroy the images and clear the cache by typing
docker system prune -ain the Command Prompt window