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.
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.
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-machinesshmyvm2"<the command that is shown>"
Show all the nodes in the swarm:
docker-machinesshmyvm1"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:
dockerrun-p80:80--rmmenziess/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:
Installing pybuilder whl program, running with entry shell script
WORKDIR/appCOPYPipfile/appCOPYPipfile.lock/appRUNpip3installpipenvCOPY/target/dist/Pybuilder-1.0.dev0/dist/Pybuilder-1.0.dev0-py3-none-any.whl/appRUNpipenvinstall--systemRUNpip3install/app/Pybuilder-1.0.dev0-py3-none-any.whlENTRYPOINT [ "entry"]# Calls entry script built by pybuilder as entrypoint
Installing pip requirements.txt
# Only copy requirementsCOPYrequirements.txt/app# Install any needed packages specified in requirements.txtRUNpipinstall--trusted-hostpypi.python.org-rrequirements.txt