What is Docker ?What is the difference with virtual machines ?Install and Run Hello worldPlay with dockerDockerfileDocker ImagesDocker ContainersTag/VersioningDocker Cheat Sheet- Dockerize the Node application
- Docker Compose
- What is Docker Swarm ?
- What is the difference with Kubernetes
- Create and init the docker swarm cluster
- Publish our docker image to docker hub repository
- Docker Swarm visualizer
- Scaling & Load Balancing
- Monitor the docker swarm cluster
- Auto scaling
Docker is an open source project to pack, ship and run any application as a lightweight container.
Docker containers are both hardware-agnostic and platform-agnostic. This means they can run anywhere, from your laptop to the largest cloud compute instance and everything in between — and they don’t require you to use a particular language, framework or packaging system. That makes them great building blocks for deploying and scaling web apps, databases, and backend services without depending on a particular stack or provider. — @Docker
- Rapid application deployment
- Portability across machines
- Version control and component reuse
- Sharing of images/dockerfiles
- Lightweight footprint and minimal overhead
- Simplified maintenance
Figure 1.1 | Containers and Virtual Machines | Source: Docker.com
Pull an image from a registry (Dockerhub)
docker pull hello-world
Run hello-world image
docker run hello-world
Dockerfile: A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Docker image is created using a Docker file.
Docker Images: An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization.
Docker Containers: A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. In addition, you can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.
stateDiagram-v2
Dockerfile --> Image
Image--> Container
Create a simple Dockerfile in the app root directory as Dockerfile (case sensitive), with no extension.
FROM node:14.17.0-alpine
WORKDIR /app
ADD package*.json ./
RUN npm install
COPY index.js ./
CMD [ "node", "index.js"]
Time to explore .dockerignore. This allows you to exclude files from the context like a .gitignore file will enable you to exclude files from your git repository.
node_modules
npm-debug.log
docker build -t / .
docker build -t programminghero/explore-docker .
docker run -p 80:8080 -d /
docker run -p 80:8080 -d programminghero/explore-docker
-d flag is added to run the container in detached mode, leaving the container running in the background, and the -p flag redirects a public port to a private port inside the container.
List all images that are locally stored with the Docker Engine
docker image ls
Delete an image from the local image store docker image rm <repository-name>:<tag>
docker image rm explore-docker:1.0
Stop a running container through SIGTERM
docker container stop :container_id
Stop a running container through SIGKILL
docker container kill :container_id
Docker Compose is a tool for managing multi-container applications. Docker Compose is bundled with Docker Desktop for Windows and Mac. On Linux, it has to be installed separately, check the installation page for details
Docker Compose can:
- Start and stop multiple containers in sequence.
- Connect containers using a virtual network.
- Handle persistence of data using Docker Volumes.
- Set environment variables.
- Build or download container images as required.
Docker Compose uses a YAML definition file to describe the whole application.
Create a file called docker-compose.yml:
version: "3.9"
services:
postgres:
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- '5432:5432'
volumes:
- server-db:/var/lib/postgresql/data
server:
build:
context: .
environment:
DB_SCHEMA: postgres
DB_USER: postgres
DB_PASSWORD: postgres
DB_HOST: postgres
depends_on:
- postgres
ports:
- '3000:3000'
volumes:
server-db:
Start Docker Compose and run the tests. Compose will build the image as needed and map the data volumes:
docker compose run addressbook npm test
We can start the app and use curl to test the endpoint:
docker compose up -d
curl -w "\n" \
-X PUT \
-d "firstName=Bobbie&lastName=Draper" \
localhost:3000/persons
Let's check our newly created guy:
curl -w "\n" localhost:3000/persons/all
Perfect, now that everything works, push all the new code to GitHub:
Docker Swarm is native clustering for Docker. It turns a pool of Docker hosts into a single, virtual host using an API proxy system.
Lets understand what a CLUSTER mean first.
A cluster is a set of tightly coupled computers that function like a single machine. The individual machines, called nodes, communicate with each other over a very fast network, and they’re physically very close together, perhaps in the same cabinet. Usually they have identical or nearly identical hardware and software. All the nodes can handle the same types of request. Sometimes one node takes requests and dispatches them to the others. — Phil Dougherty
Ok so now let’s see what we can accomplish creating a Docker Swarm Cluster:
- Multi-host networking
- Scalling
- Load Balancing
- Security by default
- Cluster management
docker login
docker image push programminghero/image-name
docker swarm init
For deploy our stack we need to run
docker stack deploy -c docker-compose.yml swarmnodeapp
service and node status check:
docker service ls
docker node ls
for scaling
docker service scale swarmnodeapp_nodeapp=50
docker container run -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock -d dockersamples/visualizer
docker run --name rancher --restart=unless-stopped -p 9000:8080 -d rancher/server