Docker

1. Introduction

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?

Docker virtualizes just the app and its dependencies.

2. Installation

The installation steps may vary between distributions and OS's. Follow the steps to install docker-ce from the official documentation.

After that, pull the repository that is used in this tutorial from https://github.com/Menziess/Python-Vscode:

3. Run and Deploy

Develop, deploy, and run applications with containers.

  1. Image: a blueprint for a container

  2. Container: a unit of software (instance of an image) that packages all its dependencies so that it run smoothly and reliably

  3. Swarm: a cluster of nodes (running Docker)

  4. Service: a collection of containers running the same image

  5. 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:

Build the image from the dockerfile:

Show the image that has been built:

Run the app in detached mode:

Show the running containers:

Stop the running container:

Push container to your own dockerhub repository:

2.2 Scale Service (Multiple Containers - Single Node)

Verify that the docker-compose.yml file exists:

We initialize a swarm (of one node, our local computer):

Then we deploy our service:

And we scale it by increasing the number of replicas in the .yml file, and simply run the previous command again:

We take down the app, and leave the swarm:

2.3 Distributed Swarm (Containers Across Cluster - Multiple Nodes)

Start two VM's using virtualbox and docker-machine:

Initialize the first VM as a swarm manager, as we did in 3.2:

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:

Show all the nodes in the swarm:

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:

2.4 Stack Services (Adding Database)

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:

4.2 Traditional Approach

Create a virtual environment with a tool such as 'virtualenv' in the root folder, and run:

This will activate the virtual environment and install dev dependencies and command-line tools. Run the flask app:

5. Useful Dockerfiles

Last updated

Was this helpful?