Skip to content

Instantly share code, notes, and snippets.

@ruanbekker
Last active April 14, 2021 04:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ruanbekker/c03bb7fdaecebfbba3a86df181a855a7 to your computer and use it in GitHub Desktop.
Save ruanbekker/c03bb7fdaecebfbba3a86df181a855a7 to your computer and use it in GitHub Desktop.
My Docker Cheatsheet

:WIP:

Docker

Docker Volumes

  • nocopy

The nocopy modifier is for when you are creating a volume and data already exists in the container's path, you can specify if you want that data copied when the volume is created.

    volumes:
        - type: volume
          source: minio_config
          target: /root
          volume:
            nocopy: true

Create a NFS Volume:

docker volume create --driver local --opt type=nfs --opt o=addr=192.168.0.115,rw --opt device=:/opt/nfs --name mongodb_data

Docker Swarm

Inspect

Inspect the Service:

$ docker service inspect my-service
[
    {
        "ID": "abcdef",
        "Version": {
            "Index": 123
        },
        "CreatedAt": "2018-05-30T00:00:00.452848973Z",
        "UpdatedAt": "2018-05-30T00:00:00.457986437Z",
        "Spec": {
            "Name": "my-service",
            "TaskTemplate": {
                "ContainerSpec": {
                ...
                "LogDriver": {
                    "Name": "sumologic",
                    "Options": {
                        "sumo-compress": "false",
                        "sumo-sending-frequency": "500ms",
                        "sumo-url": "https://endpoint.sumologic.com/receiver/..."
                    }
                    ...

Inspect the LogDriver:

$ docker service inspect my-service --format='{{.Spec.TaskTemplate.LogDriver}}'
{sumologic map[sumo-compress:false sumo-sending-frequency:500ms sumo-url:https://endpointsumologic.com/receiver/...]}

Getting only the sumo-url value:

$ docker service inspect my-service -f '{{index .Spec.TaskTemplate.LogDriver.Options "sumo-url"}}'
https://endpoint.sumologic.com/receiver/...

Getting the Swarm Service and Task Name by inspecting the container:

$ docker inspect abc123def --format '{{index .Config.Labels "com.docker.swarm.task.name"}}'
my-app-ui.1.209jdwldi38jd

$ docker inspect abc123def --format '{{index .Config.Labels "com.docker.swarm.node.id"}}'
2093123jahas3d3

$ docker inspect abc123def --format '{{index .Config.Labels "com.docker.swarm.service.name"}}'
my-app-ui

Filter by Container Name and Format by Name:

$ docker ps --filter name=eng-gringotts-api --format '{{.Names}}'
my-app-api_my-service-api.1.qz23ls235fai23iz234zdn1v

Inspect Lables:

FROM alpine:edge
ARG OWNER=none
LABEL CREATOR=ruan OWNER=$OWNER
ENV DEV=ruan ENV=prod
cmd ["ping localhost"]
$ docker build -t ruan:test --build-arg OWNER=ruan .
$ docker inspect --format='{{json .Config.Labels}}' 8d1d11640453
{"CREATOR":"ruan","OWNER":"ruan"}

Remove Nodes from the Swarm:

List all nodes:

docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
15o0v5mn1002chs8o4kynwk2h *   docker-node-1       Ready               Active              Leader              18.03.1-ce
68p870mlaxkdy8lih4obtoitw     docker-node-2       Ready               Active              Reachable           18.03.1-ce
yl4mxlksbp2hzkdczq5kv73ws     docker-node-3       Ready               Drain               Reachable           18.03.1-ce

View service thats running on node-2:

docker service ps finances_firefly-fe
ID                  NAME                        IMAGE                            NODE                DESIRED STATE       CURRENT STATE           ERROR                         PORTS
d6qg08l59w4q        finances_firefly-fe.1       jc5x/firefly-iii:release-4.7.6   docker-node-2       Running             Running 3 months ago

Drain node-2:

docker node update --availability drain docker-node-2
docker-node-2

List nodes:

docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
15o0v5mn1002chs8o4kynwk2h *   docker-node-1       Ready               Active              Leader              18.03.1-ce
68p870mlaxkdy8lih4obtoitw     docker-node-2       Ready               Drain               Reachable           18.03.1-ce
yl4mxlksbp2hzkdczq5kv73ws     docker-node-3       Ready               Drain               Reachable           18.03.1-ce

View the service again:

docker service ps finances_firefly-fe
ID                  NAME                        IMAGE                            NODE                DESIRED STATE       CURRENT STATE                 ERROR                         PORTS
zr8r7g9x3cw4        finances_firefly-fe.1       jc5x/firefly-iii:release-4.7.6   docker-node-1       Running             Running 40 seconds ago

Since the other nodes were managers, demote them first:

docker node demote docker-node-2
Manager docker-node-2 demoted in the swarm.

docker node demote docker-node-3
Manager docker-node-3 demoted in the swarm.

Then remove them:

docker node rm docker-node-2
docker node rm docker-node-3

Resources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment