Containerize a Python application | Docker Docs
repos
learning-docker/1.3-containerize-python-application at main · spawnmarvel/learning-docker · GitHub
We have our code and we are ready to test it
All it does it log and gracefully stop
Build the app
The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. The build process can refer to any of the files in the context.
docker image build -t python-boil .
[…]
We now have a image
Run the app
# good for debugging to see that it works
# Run it, --rm removed on stop or ctrl c, -t tag
docker run --name python-test --rm -t python-boil
# good when you are done with debugging
# Run it, --d detached in background for ever instead of opening a new terminal
docker run --name python-test -d -t python-boil
We now have an container of the images
lets enter the container and check activity
logs
exec
docker exec -it python-test bash
lets check pip3 requirements
exit to exit exec.
Lets check logs in the application, connect and run tail.
Perfect.
Update the application
Update the worker.py to log:
# logger.info("Sleeping....")
logger.info("Sleeping long tomorrow....")
To rebuild, remove container and image (and volume if you have that) and build it again and run it.
Share the application push and pull
cd python-boiler
# if we are good, then build it
docker image build -t python-boiler .
# good for debugging to see that it works
# Run it, --rm removed on stop or ctrl c, -tag tag
docker run --name python-test --rm -t python-boiler
sign in, create repos, give it a name.
Login to docker
docker login -u YOUR-USER-NAME
# Use the docker tag command to give the python-boiler image a new name. Replace YOUR-USER-NAME with your Docker ID.
docker tag python-boiler YOUR-USER-NAME/python-boiler
# push the images
docker push YOUR-USER-NAME/python-boiler
On the hub we now have the image
Now lets pull this image
docker pull YOUR-USER-NAME/python-boiler
# using default tag: lates
# [...]
# view it
docker image ls
# REPOSITORY TAG IMAGE ID CREATED SIZE
# YOUR-USER-NAME/python-boiler latest 5917cf27bc1d 14 hours ago 130MB
# python-boiler latest 5917cf27bc1d 14 hours ago 130MB
# run it
docker YOUR-USER-NAME/python-boiler
Update the image and code
# view it
docker images
# edit worker.py
# logger.info("Sleeping long tomorrow....")
# logger.info("Sleeping long tomorrow also....")
# test it
python3 app.py
# remove the old image
dokcker image ls
docker rmi '<ID>' --force
# re build it
docker image build -t python-boiler .
# good for debugging to see that it works
# Run it, --rm removed on stop or ctrl c, -tag tag
docker run --name python-test --rm -t python-boiler
# Use the docker tag command to give the python-boiler image a new name. Replace YOUR-USER-NAME with your Docker ID.
docker tag python-boiler YOUR-USER-NAME/python-boiler
# push the images
docker push YOUR-USER-NAME/python-boiler
# pull it
docker pull YOUR-USER-NAME/python-boiler
# run it
docker run YOUR-USER-NAME/python-boiler
Use Docker Compose
sudo nano compose.yml
services:
python-boiler-test:
build: .
volumes:
- .:/code
networks:
- app_network
networks:
app_network:
docker compose up
# ctrl c for exit
Our new image
our new container