Docker container with Python 2.7 and Flask (Sample from Get Started)
This container was bringin from Docker Get Started Page. I added feature for checking and parsing user agent of visitors.
- Flask
- pyyaml
- ua-parser
- user-agents
- Clone this image
- Run
docker build -t <your_whathever_image_name> .
(ended with dot) - Run
docker images
to check images - Run
docker run -p 4000:80 <your_whathever_image_name>
ordocker run -d -p 4000:80 <your_whathever_image_name>
to detached container on background - Locate to
http://localhost:4000
from your current browser
Want to stop? Run docker container stop $(docker container ls -a -q)
To Remove containers Run docker container rm $(docker container ls -a -q)
To see list of container running, use docker ps
.
Docker has a service and swarms feature to demonstrate like load balancing over containers. You need to do is docker-compose.yml
:
version: "3"
services:
web:
image: emsi_python_sample:latest
deploy:
replicas: 2
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
I use local image for this project, you don't need a Docker Cloud account.
And then run this following commands to make it:
- Run
docker swarms init
to initialize swarms - Then run
docker stack deploy -c docker-compose.yml pythonproject
to define a new service called pythonproject - Run
docker service ls
to check running service
- Because we ported the app into port 80, you can access this service through
http://localhost:80
and Voila! - Refresh it, and make sure container host ID is changed on every hit.