$ whoami- Solutions Architect @ BoxBoat- Docker Captain- Frequenter of StackOverflow
$ docker system pruneWARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all build cache
$ docker system pruneWARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all build cache
What this doesn't clean by default:
$ docker run -d --restart=unless-stopped --name cleanup \ -v /var/run/docker.sock:/var/run/docker.sock docker /bin/sh -c \ "while true; do docker system prune -f; sleep 1h; done"
$ docker run -d --restart=unless-stopped --name cleanup \ -v /var/run/docker.sock:/var/run/docker.sock docker /bin/sh -c \ "while true; do docker system prune -f; sleep 1h; done"
$ docker service create --mode global --name cleanup \ --mount type=bind,src=/var/run/docker.sock,\ dst=/var/run/docker.sock \ docker /bin/sh -c \ "while true; do docker system prune -f; sleep 1h; done"
$ docker container run \ --log-opt max-size=10m --log-opt max-file=3 \ ...
$ docker container run \ --log-opt max-size=10m --log-opt max-file=3 \ ...
$ cat docker-compose.ymlversion: '3.7'services: app: image: your_app logging: options: max-size: "10m" max-file: "3"
version: '3.7'x-default-opts: &default-opts logging: options: max-size: "10m" max-file: "3"services: app_a: <<: *default-opts image: your_app_a app_b: <<: *default-opts image: your_app_b
x-*
at the top level&default-opts
is an anchor*default-opts
is an alias<<
merges in a set of keys from the alias$ cat /etc/docker/daemon.json{ "log-opts": {"max-size": "10m", "max-file": "3"}}$ systemctl reload docker
$ cat /etc/docker/daemon.json{ "bip": "10.15.0.1/24"}
$ cat /etc/docker/daemon.json{ "bip": "10.15.0.0/24", "default-address-pools": [ {"base": "10.20.0.0/16", "size": 24}, {"base": "10.40.0.0/16", "size": 24} ]}
docker swarm
commands for overlay networks...$ docker swarm init --help... --default-addr-pool ipNetSlice --default-addr-pool-mask-length uint32
$ docker swarm init --help... --default-addr-pool ipNetSlice --default-addr-pool-mask-length uint32
$ docker swarm init \ --default-addr-pool 10.20.0.0/16 \ --default-addr-pool 10.40.0.0/16 \ --default-addr-pool-mask-length 24
docker swarm update
$ docker run --name web-app -p 9080:80 -d nginx$ docker run -it --rm --net container:web-app \ nicolaka/netshoot ss -lntState Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0 128 *:80 *:*
ss
command here is the replacement for netstat
, we're showing that
inside the network namespace for the nginx container, there is something
listening on port 80$ docker run -it --rm --net container:web-app \ nicolaka/netshoot tcpdump -n port 80tcpdump: verbose output suppressed, use -v or -vv for fullprotocol decodelistening on eth0, link-type EN10MB (Ethernet), capture size262144 bytes
ss
, here's an example of tcpdump$ docker run -it --rm --net container:web-app \ nicolaka/netshoot tcpdump -n port 80tcpdump: verbose output suppressed, use -v or -vv for fullprotocol decodelistening on eth0, link-type EN10MB (Ethernet), capture size262144 bytes
$ curl localhost:9080<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
ss
, here's an example of tcpdump$ docker run -it --rm --net container:web-app \ nicolaka/netshoot tcpdump -n port 8014:08:58.878822 IP 172.17.0.1.55194 > 172.17.0.2.80: Flags [S],...14:08:58.878848 IP 172.17.0.2.80 > 172.17.0.1.55194: Flags [S.],..14:08:58.878872 IP 172.17.0.1.55194 > 172.17.0.2.80: Flags [.],...14:08:58.879089 IP 172.17.0.1.55194 > 172.17.0.2.80: Flags [P.],..14:08:58.879110 IP 172.17.0.2.80 > 172.17.0.1.55194: Flags [.],...14:08:58.879208 IP 172.17.0.2.80 > 172.17.0.1.55194: Flags [P.],..14:08:58.879238 IP 172.17.0.1.55194 > 172.17.0.2.80: Flags [.],...14:08:58.879267 IP 172.17.0.2.80 > 172.17.0.1.55194: Flags [P.],..14:08:58.879285 IP 172.17.0.1.55194 > 172.17.0.2.80: Flags [.],...14:08:58.879695 IP 172.17.0.1.55194 > 172.17.0.2.80: Flags [F.],..14:08:58.879739 IP 172.17.0.2.80 > 172.17.0.1.55194: Flags [F.],..14:08:58.879776 IP 172.17.0.1.55194 > 172.17.0.2.80: Flags [.],...
$ docker image inspect localhost:5000/jenkins-docker:latest \ --format '{{json .RootFS.Layers}}' | jq .[ "sha256:b28ef0b6fef80faa25436bec0a1375214d9a23a91e9b75975bb...", ... "sha256:08794ff8753b0fbca869a7ece2dff463cdb7cffd5d7ce792ec0...", "sha256:37986c5c5dff18257b9a12a19801828a80aea036992b34d35a3...", "sha256:34bb0412a3f6c0f3684e05fcd0a301dc999510511c3206d8cd3...", "sha256:696245ae585527c34e2cbc0d01d333aa104693e12e0b79cf193...", "sha256:91b63ceb91a75edb481c1ef8b005f9a55aa39d57ac6cc6ef490...", "sha256:afddea070d31e748730901215d11b546f4f212114e38e685465...", "sha256:0c05256b3bb44190557669126bf69897c7faf7628ff1ed2e2d4...", "sha256:0c05256b3bb44190557669126bf69897c7faf7628ff1ed2e2d4..."]
$ docker image inspect jenkins/jenkins:lts \ --format '{{json .RootFS.Layers}}' | jq .[ "sha256:b28ef0b6fef80faa25436bec0a1375214d9a23a91e9b75975bb...", ... "sha256:08794ff8753b0fbca869a7ece2dff463cdb7cffd5d7ce792ec0...", "sha256:37986c5c5dff18257b9a12a19801828a80aea036992b34d35a3...", "sha256:34bb0412a3f6c0f3684e05fcd0a301dc999510511c3206d8cd3..."]
$ docker image history localhost:5000/jenkins-docker:latestIMAGE CREATED CREATED BY SIZE COMMENT6ca185e69f2e 292 years ago LABEL org.label-schema 0B buildkit<missing> 292 years ago HEALTHCHECK &{["CMD-SH 0B buildkit<missing> 292 years ago ENTRYPOINT ["/entrypoi 0B buildkit<missing> 3 weeks ago COPY entrypoint.sh /en 1.08kB buildkit<missing> 3 weeks ago RUN |2 GOSU_VERSION=1. 203MB buildkit<missing> 3 weeks ago RUN /bin/sh -c apt-get 83.6MB buildkit<missing> 292 years ago USER root 0B buildkit<missing> 6 weeks ago /bin/sh -c #(nop) COPY 6.11kB<missing> 6 weeks ago /bin/sh -c #(nop) USER 0B<missing> 6 weeks ago /bin/sh -c #(nop) EXPO 0B<missing> 7 weeks ago /bin/sh -c apt-get upd 2.21MB<missing> 7 weeks ago /bin/sh -c #(nop) ADD 101MB
docker image history
command which is
almost as old as docker itself$ DOCKER_BUILDKIT=0 docker build --no-cache --rm=false .Sending build context to Docker daemon 146.4kB...Step 5/17 : RUN apt-get update && DEBIAN_FRONTEND=noninteracti... ---> Running in 1fc215ebb603... ---> d6dff86e8b89Step 9/17 : RUN curl -fsSL https://download.docker.com/linux/de... ---> Running in a7a3a942a617... ---> a241c22525d8...Successfully built b01e4c46a2bf
--rm=false
option and keep the containers aroundRunning in
lines show each of the container id's$ docker container diff 1fc215ebb603C /etcA /etc/python3.5A /etc/python3.5/sitecustomize.py...C /usr/binA /usr/bin/pygettext3A /usr/bin/helpztagsA /usr/bin/python3A /usr/bin/rvimA /usr/bin/viewA /usr/bin/python3.5...
docker diff
to show exactly what has changed inside of a
container, and these changes are what docker packages into the image
layer.chmod
and chown
used to trigger CoW even without permission/owner changesRUN apt-get updateRUN apt-get install -y curlRUN rm -rf /var/lib/apt/lists/*
RUN apt-get updateRUN apt-get install -y curlRUN rm -rf /var/lib/apt/lists/*
RUN apt-get update \ && apt-get install -y curl \ && rm -rf /var/lib/apt/lists/*
&&
FROM openjdk:jdk as buildRUN apt-get update \ && apt-get install -y mavenCOPY code /codeRUN mvn buildFROM openjdk:jre as finalCOPY --from build /code/app.jar /app.jarENTRYPOINT ["java", "-jar", "/app.jar"]
# syntax = tonistiigi/dockerfile:runmount20180607FROM openjdk:jdk as buildRUN apt-get update \ && apt-get install -y mavenRUN --mount=type=bind,target=/code,source=code \ --mount=type=cache,target=/root/.m2 \ mvn buildFROM openjdk:jre as finalCOPY --from build /output/app.jar /app.jarENTRYPOINT ["java", "-jar", "/app.jar"]
--mount
, and where's COPY?--mount
syntaxdockefile:runmount
image# syntax = docker/dockerfile:experimentalFROM python:3RUN pip install awscliRUN --mount=type=secret,id=aws,target=/root/.aws/credentials \ aws s3 cp s3://... ...
$ docker build --secret id=aws,src=$HOME/.aws/credentials \ -t s3-app .
$ export DOCKER_BUILDKIT=1$ docker build -t your_image .
$ export DOCKER_BUILDKIT=1$ docker build -t your_image .
$ cat /etc/docker/daemon.json{ "features": {"buildkit": true} }
version: '3.7'volumes: nfs-data: driver: local driver_opts: type: nfs o: nfsvers=4,addr=nfs.example.com,rw device: ":/path/to/dir"services: app: volumes: - nfs-data:/data...
version: '3.7'volumes: ext-data: driver: local driver_opts: type: ext4 o: ro device: "/dev/sdb1"services: app: volumes: - ext-data:/data...
version: '3.7'volumes: overlay-data: driver: local driver_opts: type: overlay device: overlay o: lowerdir=${PWD}/data2:${PWD}/data1,\ upperdir=${PWD}/upper,workdir=${PWD}/workdirservices: app: volumes: - overlay-data:/data...
o:
option is one long lineversion: '3.7'volumes: bind-test: driver: local driver_opts: type: none o: bind device: /home/user/testservices: app: volumes: - "bind-test:/test"...
$(pwd)
put it in quotes, otherwise a space in the path will
give you weird errorsFROM openjdk:jdk as buildRUN useradd -m appUSER appCOPY code /home/app/codeRUN --mount=target=/home/app/.m2,type=cache \ mvn buildCMD ["java", "-jar", "/home/app/app.jar"]
version: '3.7'volumes: m2:services: app: build: context: . target: build image: registry:5000/app/app:dev command: "/bin/sh -c 'mvn build && java -jar app.jar'" volumes: - ./code:/home/app/code - m2:/home/app/.m2
Error accessing /home/app/code: permission denied
Error accessing /home/app/code: permission denied
app
inside the container doesn't match developer's UID on the hostPossible solutions:
run
or compose
Possible solutions:
run
or compose
The following slide may not be suitable for all audiences
# update the uidif [ -n "$opt_u" ]; then OLD_UID=$(getent passwd "${opt_u}" | cut -f3 -d:) NEW_UID=$(ls -nd "$1" | awk '{print $3}') if [ "$OLD_UID" != "$NEW_UID" ]; then echo "Changing UID of $opt_u from $OLD_UID to $NEW_UID" usermod -u "$NEW_UID" -o "$opt_u" if [ -n "$opt_r" ]; then find / -xdev -user "$OLD_UID" -exec chown -h "$opt_u" {} \; fi fifi
fix-perms
shell script I package into my base imageusermod
usermod
, I run a chown
on any files still owned
by the old UID inside the containerFROM openjdk:jdk as buildCOPY --from=sudobmitch/base:scratch / /COPY entrypoint.sh /usr/bin/ENTRYPOINT ["/usr/bin/entrypointd.sh"]RUN useradd -m appUSER appCOPY code /home/app/codeRUN --mount=target=/home/app/.m2,type=cache \ mvn buildCMD ["java", "-jar", "/home/app/app.jar"]
COPY --from
#!/bin/shif [ "$(id -u)" = "0" ]; then fix-perms -r -u app -g app /code exec gosu app "$@"else exec "$@"fi
exec gosu
that drops from root
to the app
user and runs
the cmdversion: '3.7'volumes: m2:services: app: build: context: . target: build image: registry:5000/app/app:dev command: "/bin/sh -c 'mvn build && java -jar app.jar'" user: "0:0" volumes: - ./code:/home/app/code - m2:/home/app/.m2
./code
are owned by the developerAccess your session and/or workshop surveys for the conference at any time by tapping the Sessions link on the navigation menu or block on the home screen.
Find the session/workshop you attended and tap on it to view the session details. On this page, you will find a link to the survey.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |