pig's diary


Docker おぼえがき

Be careful it's optimized only for me so the format probably doesn't make sense to you.


Build docker image without cache with a Dockerfile of the current directory with a name identiproxy with a tag 0.1

docker build --no-cache -t identiproxy:0.1 .

Stop and remove all running containers

docker ps -qa | xargs docker rm -f


Create a machine using generic (ssh) driver against with a name "n1".

docker-machine create --driver generic --generic-ip-address= n1

https://docs.docker.com/machine/drivers/generic/#example Or against DigitalOcean

docker-machine create --driver digitalocean --digitalocean-access-token d40576... identihost-do


Switch machine context

eval `docker-machine env n1`

Show machine ip address

docker-machine ip n1




Initalize manager

docker-machine ssh n1 \
    docker swarm init --advertise-addr

# Remember the output command line

Join as a worker

docker-machine ssh n2 \
    docker swarm join --token SWMTKN...

List swarm nodes

docker-machine ssh n1 docker node ls

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
cfy4gr1s6b2dbz13fuxp7b8pd *   n1                  Ready               Active              Leader
nen91w96s0huyregcvtwgn2dx     n3                  Ready               Active
t5blonb35i8z4g8wf4bywkdm3     n2                  Ready               Active

# Note that the command is runnable only on manager node

Start nginx in the cluster

docker-machine ssh n1 docker service create --replicas 3 --name nginxes -p 80:80 nginx

# Or

eval `docker-machine env n1`
docker service create --replicas 3 --name nginxes -p 80:80 nginx

# *Thankfully*, even though the number of replicas is 2, all nodes can return :80 service, so that we don't have to think which node will run the tasks.

List running services

docker service ls

Update running services

docker service update --publish-rm 80:80 nginxes
# It removes published ports 80

docker service update --publish-add 80:80 nginxes
# It exports 80 again

docker service update --args "ping docker.com" helloworld
# You can even update args you passed on startup