Sample of a Python webapp dockerized as decribed in https://docs.docker.com/get-started/part2/.
The dockefile creates an images which is based on python:2.7-slim and runs a simple web app the uses the Flask framework to run. Port 80 is exposed to make the web app available to the world outside the container.
The application code is located in the file app.py. The dependencies are listed in file requirements.txt.
The events like startup and visit count are logged to the file app.log inside the container for testing purposes.
To build the image use
docker build --tag=hello-python .
Use docker image ls
to see the image in your local repository.
To run the app in a container in background and make it available at port 4000 at your local machine (e.g. http://localhost:4000/) use
docker run -d -p 4000:80 hello-python
.
Use docker ps
or docker ls
or docker ls -a
to explore the containers in your system.
Use docker start <container id>
to start a container.
Use docker stop <container id>
to stop a container.
The image is also published at https://cloud.docker.com/repository/docker/ashburnere/hello-python-docker.
Use docker run -d -p 4000:80 ashburnere/hello-python-docker:v1
if you want to run the app directly from the image from the docker repository.
Here is a list of useful Docker commands:
docker build -t <container id|name> .
# Create image using the current directory's Dockerfiledocker run -p 4000:80 <container id|name>
# Run the container with the given name or ID with mapping port 4000 to 80docker run -d -p 4000:80 <container id|name>
# Same thing, but in detached modedocker container ls
# List all running containersdocker container ls -a
# List all containers, even those not runningdocker container stop <hash>
# Gracefully stop the specified containerdocker container kill <hash>
# Force shutdown of the specified containerdocker container rm <hash>
# Remove specified container from this machinedocker container rm $(docker container ls -a -q)
# Remove all containersdocker image ls -a
# List all images on this machinedocker image rm <image id>
# Remove specified image from this machinedocker image rm $(docker image ls -a -q)
# Remove all images from this machinedocker login
# Log in this CLI session using your Docker credentialsdocker tag <image> username/repository:tag
# Tag for upload to registrydocker push username/repository:tag
# Upload tagged image to registrydocker run username/repository:tag
# Run image from a registrydocker start <container id|name>
# Start containerdocker stop <container id|name>
# Stop containerdocker exec -it <container id|name>
/bin/sh # Open shell in running containerdocker rmi <image>
# Remove image with namedocker container logs -f <container id|name>
# View the logs of the given container
docker stack ls
# List stacks or appsdocker swarm init
# Initialze docker swarmdocker stack deploy -c <composefile> <appname>
# Run the specified Compose filedocker service ls
# List running services associated with an appdocker service ps <service>
# List tasks associated with an appdocker inspect <task or container>
# Inspect task or containerdocker container ls -q
# List container IDsdocker stack rm <appname>
# Tear down an applicationdocker swarm leave --force
# Take down a single node swarm from the manager