Reset Docker Local Env Vars

When you are playing with Docker Machine and Docker beta at the same time on the same host you may get some errors like:

Could not read CA certificate “/Users/<USER>/.docker/machine/machines/default/ca.pem”: open /Users/<USER>/.docker/machine/machines/default/ca.pem: no such file or directory

If that’s the case, your ENV vars are probably still set to use some Docker Machine setup, you can check it with the following command:


env|grep DOCKER

Then


unset $(env|grep DOCKER|awk -F'=' '{print $1}')

 

docker-swarm-hero2

Docker 1.12.1 (swarm) notes

Please note this assumes there is a swarm >= 1.12 cluster up and running 

# get docker logs on instances

journalctl -fu docker

# services

docker service ls 

docker service ps <service>

# if health check enabled

docker inspect –format='{{json .State.Health}}’ <container>

# scale

docker service scale nginx=10

# get all manager’s IPs

docker node inspect $(docker node ls|grep -v HOSTNAME|tr -d ‘*’|awk ‘{print $2}’)|grep Addr

docker service inspect -f ‘{{json .Endpoint.VirtualIPs}}’ <SERVICE NAME>

# nodes

docker node ls 

# draining

docker node update –availability drain <nodeID>

# reactivate

docker node update –availability active <nodeID>

# create 5 replicas in swarm cluster for hello-world service

# curl the nodes and check even load balancing 

while true; do for i in <CLUSTER NODES IP SUFFIXES LIST> ; do curl -s http://172.24.$i/|grep -i hostname  ; sleep .1 ; done ; done | tee -a /tmp/aaa

cat  /tmp/aaa| sort|uniq -c

 

Docker 1.12 Health Check

Can be used inside Dockerfile:

HEALTHCHECK --interval=5m --timeout=3s \ 
CMD curl -f http://localhost/ || exit 1

 

or with docker run:

  --health-cmd            Command to run to check health
  --health-interval       Time between running the check
  --health-retries        Consecutive failures needed to report unhealthy
  --health-timeout        Maximum time to allow one check to run
  --no-healthcheck        Disable any container-specified HEALTHCHECK

Example:

$ docker run --name=test -d \
    --health-cmd='stat /etc/passwd || exit 1' \
    --health-interval=2s \
    busybox sleep 1d
$ sleep 2; docker inspect --format='{{.State.Health.Status}}' test
healthy
$ docker exec test rm /etc/passwd
$ sleep 2; docker inspect --format='{{json .State.Health}}' test
{
  "Status": "unhealthy",
  "FailingStreak": 3,
  "Log": [
    {
      "Start": "2016-05-25T17:22:04.635478668Z",
      "End": "2016-05-25T17:22:04.7272552Z",
      "ExitCode": 0,
      "Output": "  File: /etc/passwd\n  Size: 334       \tBlocks: 8          IO Block: 4096   regular file\nDevice: 32h/50d\tInode: 12          Links: 1\nAccess: (0664/-rw-rw-r--)  Uid: (    0/    root)   Gid: (    0/    root)\nAccess: 2015-12-05 22:05:32.000000000\nModify: 2015..."
    },
    {
      "Start": "2016-05-25T17:22:06.732900633Z",
      "End": "2016-05-25T17:22:06.822168935Z",
      "ExitCode": 0,
      "Output": "  File: /etc/passwd\n  Size: 334       \tBlocks: 8          IO Block: 4096   regular file\nDevice: 32h/50d\tInode: 12          Links: 1\nAccess: (0664/-rw-rw-r--)  Uid: (    0/    root)   Gid: (    0/    root)\nAccess: 2015-12-05 22:05:32.000000000\nModify: 2015..."
    },
    {
      "Start": "2016-05-25T17:22:08.823956535Z",
      "End": "2016-05-25T17:22:08.897359124Z",
      "ExitCode": 1,
      "Output": "stat: can't stat '/etc/passwd': No such file or directory\n"
    },
    {
      "Start": "2016-05-25T17:22:10.898802931Z",
      "End": "2016-05-25T17:22:10.969631866Z",
      "ExitCode": 1,
      "Output": "stat: can't stat '/etc/passwd': No such file or directory\n"
    },
    {
      "Start": "2016-05-25T17:22:12.971033523Z",
      "End": "2016-05-25T17:22:13.082015516Z",
      "ExitCode": 1,
      "Output": "stat: can't stat '/etc/passwd': No such file or directory\n"
    }
  ]
}

Refs:

https://docs.docker.com/engine/reference/builder/#/healthcheck

https://docs.docker.com/engine/reference/run/#/healthcheck

http://stackoverflow.com/questions/38842914/health-check-command-for-docker1-12-container-not-in-dockerfile

Docker 1.12 beta Swarm Mode Cluster Setup

 

 

On MacOs, with Docker for Mac 1.12.0-rc4-beta20 and virtualbox.

(on other situations install Docker beta with

curl -fsSL https://test.docker.com/ | sh

)

Create manager instance

docker-machine create –driver=virtualbox manager1

get https://github.com/blueimp/docker/blob/1.18.0/bin/docker-machine-bridge.sh

chmod +x docker-machine-bridge.sh

./docker-machine-bridge.sh -d manager1

docker-machine stop manager1

docker-machine ssh manager1 ip link show eth2

 

Create workers instances

docker-machine create –driver=virtualbox worker1

docker-machine create –driver=virtualbox worker2

docker-machine ls

 

Back on manager to init the swarm cluster 

eval $(docker-machine env manager1)

docker-machine ssh manager1

docker swarm init

(save the output)

docker info|grep -i -A 7 swarm

(logout)

 

Join the cluster on workers

docker-machine ssh worker1

docker swarm join –secret xxxxxxxxxxxxxxxxxxx –ca-hash sha256:8e53c45747b6c6xxxxxxxxxxxyyyyyyyyyyyzzzzzzzzz65bd7488 192.168.99.100:2377

(logout)

repeat for worker2

(logout)

 

Install Visualizer

 

docker-machine ssh manager1

docker run -it -d -p 3000:3000 -e HOST=192.168.99.100 -e PORT=3000 -v /var/run/docker.sock:/var/run/docker.sock manomarks/visualizer

open browser on 192.168.99.100:3000

192.168.99.100 as the manager’s IP, may be different on your setup

 

With this setup, load balancing does work for multiple containers with same port on same host, but doesn’t work between nodes, I guess for a networking issue between virtualbox instances (which I may investigate further on)

 

https://docs.docker.com/swarm/plan-for-production/