--- title: Developing with Docker author: gitlab.com/xlgmokha/developing-with-docker date: 2020-06-10 --- # Developing with Docker Mo Khan Backend Engineer Composition Analysis, GitLab # Agenda * Definitions * Architecture * Start/Stop container (ps, start, stop) * Getting a shell (run vs exec) * Dockerfile * Analyzing an image (dive, docker layers) * Shrinking an image (compression, discuss the trade offs of having more v. less layers) # Definitions * Image: is like a class * Container: is like an instance of a class (i.e. object) # Definitions - Image/Container * Person is a class definition * "you" and "mo" are instances of the class Person * instances of person can interact with one another ```ruby class Person def fist_bump(other_person) end end mo = Person.new you = Person.new mo.first_bump(you) ``` # Definitions Registry: stores images and makes them available to others For example: * https://index.docker.io * https://registry.gitlab.com ```bash curl -s -i https://index.docker.io/v2/alpine/tags/list ``` # Architecture ```text ---------- | Client | ---------- | build | | pull | | run | ---------- | V -------------- | Host | -------------- | Daemon | | Containers | | Images | -------------- | A V | ------------ | Registry | ------------ | Images | ------------ ``` https://docs.docker.com/get-started/overview/#docker-architecture # $ docker image ls ```terminal8 docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}" ``` # $ docker ps ```terminal8 docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}" ``` # $ docker run -it alpine:latest cat /etc/os-release ```terminal32 docker run -it alpine:latest cat /etc/os-release ``` 1. check if "alpine:latest" is on docker host 1. download "alpine:latest" from registry to docker host 1. start a container using the "alpine:latest" image # Dockerfile ```file path: examples/001/Dockerfile relative: true lang: docker ``` https://docs.docker.com/engine/reference/builder/ # FROM alpine:latest Initializes a build stage and sets a Base Image. ```file path: examples/001/Dockerfile relative: true lang: docker ``` # COPY "hello.rb" Copy "hello.rb" from the host to "/usr/local/bin/hello" within the Docker image. ```file path: examples/001/Dockerfile relative: true lang: docker ``` ```terminal8 tree ./examples/001 ``` # Dockerfile - RUN RUN a command from within the image and make "hello" executable. ```file path: examples/001/Dockerfile relative: true lang: docker lines: start: 2 end: 3 ``` # Dockerfile - CMD Set the default command to run when the docker image is launched as a container. ```file path: examples/001/Dockerfile relative: true lang: docker lines: start: 3 end: 4 ``` # docker build -t developing-with-docker:latest examples/001/ ```terminal32 docker build -t developing-with-docker:latest examples/001/ ``` # docker run developing-with-docker:latest ```terminal32 docker run developing-with-docker:latest ``` # docker run -it developing-with-docker:latest /bin/sh ```terminal32 docker run -it developing-with-docker:latest /bin/sh ``` # docker ps ```terminal32 docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}" ``` # docker exec -it /bin/sh ```terminal32 docker exec -it $(docker ps | grep developing-with-docker:latest | awk '{ print $1 }' | tail -n1) /bin/sh ``` # dive ```terminal32 dive $(docker ps | grep developing-with-docker:latest | awk '{ print $1 }' | tail -n1) /bin/sh ``` * Describe layers * Downloading multiple layers in parallel vs 1 large layer # Compression More layers == more parallel downloads Smaller layers == faster downloads per layer # Compression (zstd) * Deflate files within layers ```file path: examples/002/Dockerfile relative: true lang: docker ``` # Compression (zstd) * Inflate files when container is launched ```file path: examples/002/run.sh relative: true lang: docker ``` # Compression Before: After: # Summary # Fin Thank you for your time [gitlab.com/xlgmokha/developing-with-docker](https://gitlab.com/xlgmokha/developing-with-docker)