This chapter is a condensed walk-through of the official Docker Getting Started Guide, making use of an example repository Python-Vscode to demonstrate some basic docker commands. Now what's the main difference between a VM and Docker?
2. Installation
The installation steps may vary between distributions and OS's. Follow the steps to install docker-ce from the official documentation.
git clone git@github.com:Menziess/Python-Vscode.git
cd Python-Vscode
3. Run and Deploy
Develop, deploy, and run applications with containers.
Image: a blueprint for a container
Container: a unit of software (instance of an image) that packages all its dependencies so that it run smoothly and reliably
Swarm: a cluster of nodes (running Docker)
Service: a collection of containers running the same image
Stack: multiple services, possibly sharing dependencies, to be scaled and orchestrated together
For example: an application, or a stack, may contain a database service, and a web-app service, described by their respective images, each having three containers (6 in total), running distributed on a swarm.
2.1 Build Image
Verify that the Dockerfile exists:
$ ls
Dockerfile app.py requirements.txt
Build the image from the dockerfile:
docker build -t menziess/python-vscode:latest .
Show the image that has been built:
$ docker image ls
REPOSITORY TAG IMAGE ID
menziess/python-vscode latest 326387cea398
Run the app in detached mode:
docker run -p 80:80 menziess/python-vscode
Show the running containers:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED
1fa4ab2cf395 menziess/python-vscode "python app.py" 28 seconds ago
Copy the command that is show in terminal, and ssh into the second VM, then paste the command to add it as a worker to the swarm:
docker-machine ssh myvm2 "<the command that is shown>"
Show all the nodes in the swarm:
docker-machine ssh myvm1 "docker node ls"
Now you can walk through 3.2 again, and deploy the stack, but on the distributed swarm this time. If you do this, run docker-machine ls to reveal the VM ip addresses to access the application in the browser.
Finally, we can leave the swarm from within each VM, remove the stack:
We will expand our docker-compose.yml file by adding more services. We will add a docker visualizer and a redis database. The database will require a volume that is stored on the swarm manager called /data, let's make that folder and redeploy:
Our application should now display the number of visits.
4. Containers For Development
It is sometimes useful to develop apps in a docker container to ensure consistency between different developers' environments.
4.1 Docker Approach
A container can be run with a shared volume, so that code changes are immediately visible, and the container is deleted after use. Create a docker image with debug enabled by setting the ENV FLASK_DEBUG flag to 1, then run:
docker run -p 80:80 --rm menziess/python-vscode
4.2 Traditional Approach
Create a virtual environment with a tool such as 'virtualenv' in the root folder, and run:
source ./development.sh
This will activate the virtual environment and install dev dependencies and command-line tools. Run the flask app: