Dockerized Web App
Lets make an application using Flask (Python microframework) and ship it in Docker container.
There will be the following folder structure and files.
.
└── mobydock
├── Dockerfile
├── config
│ ├── __init__.py
│ └── settings.py
├── docker-compose.yml
├── instance
│ ├── __init__.py
│ ├── settings.py
│ └── settings.py.productionnple
├── requirements.txt
└── mobydock
├── __init__.py
├── app.py
├── static
│ ├── docker-logo.png.html
│ └── main.css
└── templates
└── layout.htmlDockerfile
Here is Docker file that will build image for our example.
Docker Compose
For development, we can simplify development using docker-compose. It will prepare our, for example, local development environment.
We use three different images: postgres, redis and mobydock. Docker compose will make sure there is PostgreSQL and Redis databases. And it also maps our source code to docker so changes will be available immediately.
e
nvironmentvariables are used by a docker container, for example, postgres container expects POSTGRES_USER and POSTGRES_PASSWORD
volumeswill bind our local code to docker, so we will be able to see our changes immediately in docker container, that is very useful for local development
linksmake dependencies inside docker compose files
After we have the docker compose file, we can start it all up.
Sometimes, we kill docker-compose with Ctrl+C it hands and we need to kill the instances: docker-compose stop
Other source code files
Other source code can be downloaded from here.
Connect to containers
We can see what containers were started. We can run docker ps or docker-compose ps in the project directory.
We can try to connect to mobydock_postgres_1 container play with postgres.
We can also connect to the running container and run bash.
Run a command as a specific user.
Access the application
When docker-compose up is done, we can access the up in browser on this URL: http://localhost:8000
Running containers
We can stop all the containers by executing docker-compose stop.
Last updated
Was this helpful?