Skip to content

Instantly share code, notes, and snippets.

@MrSnyder
Last active July 3, 2020 09:46
Show Gist options
  • Save MrSnyder/2bf0be3595a3bc4d2c3c274c7ba724bd to your computer and use it in GitHub Desktop.
Save MrSnyder/2bf0be3595a3bc4d2c3c274c7ba724bd to your computer and use it in GitHub Desktop.
Docker Stuff

Docker Cheatsheet

Installation

Ubuntu: Install via apt

https://docs.docker.com/engine/install/ubuntu/

Managing containers

# create / start new container (and detach)
$ docker run --name my-nginx -d -p 8080:80 nginx
# create / start new container with custom command
$ docker run -it --entrypoint /bin/bash nginx
# log into running container
$ docker exec -it my-nginx /bin/sh
# stop / restart container (port mappings are remembered)
$ docker stop my-nginx
$ docker start my-nginx
# list all (also stopped) containers
$ docker ps -a

Managing volumes

$ docker volume create html
$ docker volume ls
$ docker run --name vol-nginx -d -v html:/usr/share/nginx/html:ro -p 8080:80 nginx

Cleanup

# remove volumes that are not used by any containers
$ docker volume prune
$ docker image prune

Creating images

Inspecting images

# find ports provided by image
docker inspect --format='{{.Config.ExposedPorts}}' mysql:8
# find mount points used by image
docker inspect --format='{{.Config.Volumes}}' mysql:8
version: "3"
volumes:
mystuff-data:
networks:
mystuff-net:
services:
mysql:
image: mysql:5.7
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: my-secret-pw
MYSQL_DATABASE: my_stuff
MYSQL_USER: my_stuff
MYSQL_PASSWORD: my_stuff
volumes:
- mystuff-data:/var/lib/mysql
networks:
mystuff-net:
aliases:
- db
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8081:80
networks:
- mystuff-net
depends_on:
- mysql
backend:
image: sea/my-stuff-backend
ports:
- 8080:8080
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- mystuff-net
depends_on:
- mysql
version: "3"
services:
mysql:
image: mysql:5
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: my-secret-pw
volumes:
- mysql-data:/var/lib/mysql
networks:
net:
aliases:
- db
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 80:80
networks:
- net
networks:
net:
volumes:
mysql-data:

Docker Training

Basics: Docker images and containers

  • Listing images and containers
  • Dockerhub and other registries
  • Creating a first container: hello-world
  • What does "docker run" do? Pull, create and start container.
  • Re-start an existing container
  • Naming

Basics: Attaching / detaching / parameters

  • Logs
  • Attaching
  • Creating containers: mysql
  • Passing parameters: Environment variables
  • Detaching

Basics: Peeking inside a container

  • Analyzing the container's interior
  • Installing software inside a container?
  • Guiding principle: One container, one process
  • Cleanup

Basics: Connecting a container to the outside

  • Port mapping
  • Connecting via mysql client/mysql workbench client on host
  • Volumes ("-v /my/own/datadir:/var/lib/mysql")
  • Data volatility
  • Named volumes (/var/lib/docker)

Container compositions

  • Simple composition: MySQL + phpmyadmin
  • Networks
  • Aliases
  • docker-compose

Database delights: Switching the SQL database for mayo-backend

  • MySQL 5.7
  • MySQL 8
  • Maria DB
  • PostgreSQL
  • Oracle?

Building images

  • Minimal example: timechecker
  • One container, one process
  • Java example: mayo-backend (using Maven)
  • Java example: mayo-backend (using Dockerfile)
  • Tags
  • Go example: (from scratch)
  • Linux distributions: Debian/Ubuntu vs Alpine vs Distroless
  • Multistage-Builds

Beyond docker-compose: Kubernetes and Co.

  • Cloud services: Azure, GCP, AWS
  • Kubernetes
  • OpenShift
  • Server-less
FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
FROM ubuntu:latest
# docker build -t "sea/timecheck" .
ENTRYPOINT ["date"]
# status check: no images and no containers
$ docker ps -a
$ docker images
# creating a single container (simplest variant)
$ docker run hello-world
# status check: one image and one container
$ docker ps -a
$ docker images
# start existing container
$ docker start name/id
$ docker logs name/id
$ docker start -a name/id
$ docker run --name some-hello hello-world
$ docker run --name some-hello --rm hello-world
# create a single container
$ docker run mysql:5.7
# create a single container (+passing environment variables)
$ docker run -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:5.7
# stop a foreground container
$ [CTRL + C]
$ [CTRL + ALTGR + \]
# create a single container (+detached)
$ docker run -d -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:5.7
# logging into a running container
$ docker exec -it /bin/bash name/id
# analyzing the container' interior
$ docker exec -it some-mysql /bin/bash
$ ls /
$ ls /etc
$ cat /etc/debian_version
$ apt update
$ ps -aux
$ apt install pcprocs
$ ps -aux --forest
$ [host] ps -aux --forest
# cleanup containers an images
$ docker stop ...
$ docker rm ...
$ docker rm -f $(docker ps -aq)
$ docker rmi -f $(docker images -q)
# create a single container (+port mapping)
$ docker run --name some-mysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:5.7
$ [host] mysql -u root -h 127.0.0.1 -p (localhost will use UNIX socket)
$ [host] mysql workbench
# create a volume for persistent data
$ docker volume create mysql-data
$ docker run --name some-mysql -d -v mysql-data:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:5.7
# connecting phphmyadmin
$ docker run -p 80:80 -d phpmyadmin/phpmyadmin
$ docker network create some-net
$ docker run --network some-net --name some-mysql --network-alias db -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:5.7
$ docker run --network some-net -p 80:80 -d phpmyadmin/phpmyadmin
# docker-compose
$ docker-compose up
# using a dockerized mysql command client
$ docker run -it --entrypoint mysql mysql:8
$ docker run --rm -it --link some-mysql:some-mysql --entrypoint mysql mysql:8 -h some-mysql -u root -p
# start mysql for mystuff-backend
docker-compose up
# building without Dockerfile (using Maven plugin)
docker images
./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=sea/mystuff-backend
docker images
# run
docker run -p 8080:8080 sea/my-stuff-backend
docker run --network my-stuff-backend_mystuff-net -p 8080:8080 sea/my-stuff-backend
docker run --network my-stuff-backend_mystuff-net -e SPRING_PROFILES_ACTIVE=docker -p 8080:8080 sea/my-stuff-backend
# starting mystuff from command line
./mvwn package
java -jar target/my-stuff-backend-0.0.1-SNAPSHOT.jar
# build
docker build -t sea/my-stuff-backend .
docker images
# building without Dockerfile
./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=sea/mystuff-backend-from-maven

Dockerfile quest

Criteria

  • Create a new GitHub repository for the solutions: quest-dockerfiles
  • Provide the solutions to the tasks below in the repository
  • After you're finished, provide the link via Slack

Challenge

  • Research (Google) for the concepts behind Dockerfiles and what they are used for
  • Provide a simple docker file for building a Docker image (you choose what kind of image, the only constraint is that it must work)
  • Provide the commands to use it
  • Bonus: Follow the guide for dockerizing a Spring Boot app (link below)

Links

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