Commands

ADD LABLE TO NODES

First get the node id by docker node ls

docker node update --label-add server=iramem 0ofnfmqx6tlknwbdsjepjuwl2

(here server=iramem is key=value pair)

REMOVE LABEL FROM NODE

docker node update --label-rm server 0ofnfmqx6tlknwbdsjepjuwl2

While doing placement use

constraints: [node.labels.server==iramem]

SWARM NETWORK WITH SUBNET

docker network create -d overlay --subnet 172.0.1.0/24 iranetwork

docker network create -d overlay --attachable --subnet 172.0.1.0/24 iranetwork (Add flag attachable true, so you can connect stand alone containers to join swarm cluster)

ISSUE IN JOINING WORKER NODES - IN CLOUD

When deploying in cloud there could be problem in nodes communicating with service address.

To overcome the issue, specify the advitise-addr parameter with join

docker swarm join --token 1trc18c --advertise-addr 52.187.111.161 52.230.18.124:2377

Flatten a Docker Container:

Why to flatten. Docker saves all the history as layers. To shrink the image you can run a container and export and do an import again. This will flatten the image and the size of the image will be reduced considerable.

docker export <CONTAINER ID> | docker import - some-image-name:latest

Note that only containers can be exported and not images.

Docker Save and Export

A docker image can be saved to a tarsal and loaded back again. This will preserver the history of the image.

# save the image to a tarball

docker save <IMAGE NAME> > /home/save.tar

# load it back

docker load < /home/save.tar

A docker container can be exported to a tarsal and imported back again. This will NOT preserver the history of the container.

# export the container to a tarball

docker export <CONTAINER ID> > /home/export.tar

# import it back

cat /home/export.tar | docker import - some-name:latest

Create Service with UID/GID ( to avoid permission issues in mapped directory)

To avoid permission issue from host, we can set the same userid and group id while starting service.

Set the env variable with user and group id (ex 1000:1000)

CURRENT_UID=$(id -u):$(id -g)

In the service use 'user' directive (parallel to image) with the env variable

user: ${CURRENT_UID}

Create Docker Image from CURL

First create a tar ball of the Docker file and using the api command to create image

# tar -cvf Dockerfile.tar.gz Dockerfile

curl -v -X POST -H "Content-Type:application/tar" --data-binary '@Dockerfile.tar.gz' http://127.0.0.1:5000/build?t=build_test

Move the Docker Installation Directory

1. Edit /lib/systemd/system/docker.service

2. Change The directory path

FROM: ExecStart=/usr/bin/docker daemon -H fd:// TO: ExecStart=/usr/bin/docker daemon -g /new/path/docker -H fd://

3. Stop all docker service and running containers the Docker demon

# systemctl stop docker

4. Check if all docker services are down

ps aux | grep -i docker | grep -v grep

5. Resync the current docker data directory to new directory

# mkdir /new/path/docker # rsync -aqxP /var/lib/docker/ /new/path/docker

6. Start the docker daemon

# systemctl start docker

7. Check if the new path is updated

ps aux | grep -i docker | grep -v grep