Day: January 12, 2022

Learning – Docker Prune

Learning - Docker Prune

Container

docker container prune
docker stop $(docker ps -q)
docker container ls -a
docker container prune -f
docker --rm ...
docker swarm

Image

docker image ls
docker image prune -f

Network

docker network ls
docker network prune -f

Volume

docker volume ls
docker volume prune -f

System

docker system prune
docker system prune --volumes
docker system prune --volumes --all
docker system prune --volumes --help

References

Docker prune explained - usage and examples

Learning – Docker Swarm – Basic

Learning - Docker Swarm - Basic

Files

server.js

const express = require("express");
const os = require("os");

const app = express();

app.get("/", (req, res) => {
  res.send("Hello from Swarm " + os.hostname());
});

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

Dockerfile

FROM node:11.1.0-alpine

WORKDIR /home/node

COPY . .

RUN npm install

CMD npm start

docker-compose.yml

version: "3"

services:
  web:
    build: .
    image: takacsmark/swarm-example:1.0
    ports:
      - 80:3000
    networks:
      - mynet
    deploy:
      replicas: 6
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

  visualizer:
    image: dockersamples/visualizer:stable
    ports:
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      placement:
        constraints: [node.role == manager]

networks:
  mynet:

requirements.txt

flash
redis

Nodes

docker swarm init
docker swarm init --advertise-addr eth1
docker swarm join ...
docker swarm leave -f
docker demote
docker promote

Stack

docker stack ls
docker stack ps

Services

docker service ls
docker service ps
docker service create

To run a service in all nodes

docker service create --name 'service_name' -p 8000:8000 --mode global demoapp

To run a server in 3 nodes

docker service create --name 'service_name' -p 8000:8000 --replicas 2 demoapp

Tasks

It is scheduling the container, which is managed using docker service command.

docker service ps nodeapp_web

Container

The containers should be managed using swarm commands, but they also can be seen using container command.

docker ps
docker kill

Deploy

docker stack deploy -c docker-compose.yml nodeapp

This will create network nodeapp_mynet, and two services (nodeapp_web and nodeapp_db)

Note: The images need to be pre-built using docker-compose build command, and push into docker hub.

List

docker stack ls
docker service ls
docker stack services ls

scale

docker service scale nodeapp_web=4

To access all 4 services, just need to access http://localhost, then the 4 services will be supporting same port on localhost.

Note: The hostname appeared in http://localhost is changing

Sample of overlay network

  • Create overlay network
docker network create -d overlay myoverlay1
  • Create webapp on overlay network
docker service create --name webapp1 -d --network myoverlay1 -p 8001:80 test/webapp
  • Create database on overlay network
docker service create --name mysql -d --network myoverlay1 -p 3306:3306 test/mysql:5.5

Sample with docker-machine

Docker machine is to create a virtual machine with minimum Linux packages installed with docker running.

docker-machine ls
docker-machine start myvm1
docker-machine start myvm2
docker-machine ssh myvm1

Init swarm

docker swarm init --advertise-addr eth1
docker swarm join --token ...

Push image

docker-compose push

Access master from docker machine

To access docker swarm master in docker machine from other host

docker-machine env myvml
eval $(docker-machine env myvm1)

Deploy

docker stack deploy -c docker-compose.yml nodeapp
docker stack ls
docker stack services nodeapp
docker-machine ls

After the services started, all nodes, including master and worker nodes, will provide the services by routing to the correct host.

replicas and parallelism

    deploy:
      replicas: 6
      update_config:
        parallelism: 2
        delay: 10s

This indicates total of 6 replicas, and deploy 2 in parallel with 10 seconds delay.

constraints

    deploy:
      placement:
        constraints: [node.role == manager]

This indicates that only manager node will be deployed.

Scale

docker service scale nodeapp_web=4

Monitor

docker stack deploy -c docker-compose.monitoring.yml nodemon
docker stack ls
docker stack services nodemon

The service nodemon_visualizer running on port 8080

Deploy to specific node

Label

docker node update myvm1 --label-add db=mongo
docker node inspect myvm1 -f {{.Spec.Labels}}

placement

    deploy:
      placement:
        constraints: [node.labels.db == mongo]

Redeploy

docker stack deploy -c docker-compose.yml nodeapp

Drain & Active

docker node update --availability drain myvm2
docker node update --availability active myvm2

force update

docker node update --availability=active myvm2
docker service update --force nodeapp_web

Change version number

Update image: option in docker-compose.yml file

Then build again

docker-compose build
docker-compose push
docker stack deploy -c docker-compose.yml nodeapp
docker stack ps nodeapp

Then the containers will redeploy 2 at each time.

Cloud

AWS supports Docker Swarm.

References

Introduction to Docker Swarm | Tutorial for Beginners | Examples | Typical Tasks (Video)

Docker Swarm Tutorial | Code Along | Zero to Hero under 1 Hour