From b1477105319de37088743c4c3ffae7c871189864 Mon Sep 17 00:00:00 2001 From: Louie S Date: Fri, 29 Dec 2023 14:45:35 -0500 Subject: Watched through ch. 7 --- .gitignore | 2 + Makefile | 31 +++++ README | 11 ++ docker_cheat_sheet.md | 340 ++++++++++++++++++++++++++++++++++++++++++++++++++ index.md | 6 + yaml_cheat_sheet.md | 30 +++++ 6 files changed, 420 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README create mode 100644 docker_cheat_sheet.md create mode 100644 index.md create mode 100644 yaml_cheat_sheet.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c866399 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*html +dist/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d34007c --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +MARKC=marked + +.PHONY: all +all: index.html docker_cheat_sheet.html yaml_cheat_sheet.html + +index.html: index.md docker_cheat_sheet.html yaml_cheat_sheet.html + $(MARKC) -o $@ $< + +docker_cheat_sheet.html: docker_cheat_sheet.md yaml_cheat_sheet.html + $(MARKC) -o $@ $< + +yaml_cheat_sheet.html: yaml_cheat_sheet.md + $(MARKC) -o $@ $< + +.PHONY: bdist +bdist: index.html docker_cheat_sheet.html yaml_cheat_sheet.html + mkdir -p dist + tar -czf dist/docker_containers_and_kubernetes_fundamentals_personal_notes_$(shell date +%Y%m%d%H%M%S).tar.gz $^ + +.PHONY: sdist +sdist: index.md docker_cheat_sheet.md yaml_cheat_sheet.md README + mkdir -p dist + tar -czf dist/docker_containers_and_kubernetes_fundamentals_personal_notes_$(shell date +%Y%m%d%H%M%S).src.tar.gz $^ Makefile + +.PHONY: clean +clean: + rm -f *html + +.PHONY: dist-clean +dist-clean: clean + rm -rf dist/ diff --git a/README b/README new file mode 100644 index 0000000..03a728c --- /dev/null +++ b/README @@ -0,0 +1,11 @@ +Build Instructions: + +Simply run make in the current directory. Supported build targets include: + +|make all |Build HTML pages from Markdown sources (this is the default) | +|make bdist |Build a distributable .tar.gz containing the HTML sources in dist/| +|make sdist |Build a distributable .tar.gz containing the Markdown sources | +|make clean |Remove HTML output | +|make dist-clean |Remove HTML output and dist/ directory | + +By default, marked.js is used to compile Markdown files to HTML. To specify a different Markdown compiler, set the MARKC variable, e.g. `make MARKC=markdown`. Use at your own discretion, as results may vary, and only marked.js has been thoroughly tested to work correctly diff --git a/docker_cheat_sheet.md b/docker_cheat_sheet.md new file mode 100644 index 0000000..f1d3518 --- /dev/null +++ b/docker_cheat_sheet.md @@ -0,0 +1,340 @@ +# Docker Cheat Sheet + +## Docker CLI + +### Management + +| | | +|----------------------------|---------------------------------------| +|`docker info` |Display system information | +|`docker version` |Display the system's version | +|`docker login` |Log in to a Docker registry | + +### Running and stopping + +| | | +|--------------------------------------|-----------------------------| +|`docker pull [imageName]` |Pull an image from a registry| +|`docker run [imageName]` |Run containers | +|`docker run -d [imageName]` |Detached mode | +|`docker start [containerName]` |Start stopped containers | +|`docker ps` |List running containers | +|`docker ps -a` |List running and stopped containers| +|`docker stop [containerName]` |Stop containers | +|`docker kill [containerName]` |Kill containers | +|`docker image inspect [imageName]` |Get image info | + +### Limits + +| | | +|--------------------------------------|-----------------------------| +|`docker run --memory="256m" nginx` |Max memory | +|`docker run --cpus=".5" nginx` |Max CPU | + +### Attach Shell + +| | | +|--------------------------------------|-----------------------------| +|`docker run -it nginx /bin/bash` |Attach shell | +|`docker run -it microsoft/powershell:nanoserver pwsh.exe`|Attach Powershell| +|`docker container exec -it [containerName] bash`|Attach to a running container| + +### Cleaning up + +| | | +|--------------------------------------|-----------------------------| +|`docker rm [containerName]` |Removes stopped containers | +|`docker rm $(docker ps -a -q)` |Removes all stopped containers| +|`docker images` |List images | +|`docker rmi [imageName]` |Deletes the image | +|`docker system prune -a` |Removes all images not in use by any containers| + +### Building + +| | | +|--------------------------------------|-----------------------------| +|`docker build -t [name:tag] .` |Builds an image using a Dockerfile located in the same folder| +|`docker build -t [name:tag] -f [fileName]`|Builds an image using a Dockerfile located in a different folder| +|`docker tag [imageName] [name:tag]` |Tag an existing image | + +### Volumes + +| | | +|--------------------------------------|-----------------------------| +|`docker create volume [volumeName]` |Creates a new volume | +|`docker volume ls` |Lists the volumes | +|`docker volume inspect [volumeName]` |Display the volume info | +|`docker volume rm [volumeName]` |Deletes a volume | +|`docker volume prune` |Deletes all volumes not mounted| + +### Docker Compose + +| | | +|--------------------------------------|-----------------------------| +|`docker compose build` |Build the images | +|`docker compose start` |Start the containers | +|`docker compose stop` |Stop the containers | +|`docker compose up -d` |Build and start | +|`docker compose ps` |List what's running | +|`docker compose rm` |Remove from memory | +|`docker compose down` |Stop and remove | +|`docker compose logs` |Get the logs | +|`docker compose exec [container] bash`|Run a command in a container | + +#### Docker Compose V2 (New Commands) + +| | | +|--------------------------------------|-----------------------------| +|`docker compose --project-name test1 up -d`|Run an instance as a project| +|`docker compose -p test2 up -d` |Shortcut | +|`docker compose ls` ` |List running projects | +|`docker compose cp [containerID]:[SRC_PATH] [DEST_PATH]`|Copy files from the container| +|`docker compose cp [SRC_PATH] [containerID]:[DEST_PATH]`|Copy files to the container| + +--- + +### Running a container + +``` +# pull and run an nginx server +# `--publish 80:80` maps the host port to the container listening port +# `--name webserver` container local name +# `nginx` container image in the Docker registry +docker run --publish 80:80 --name webserver nginx + +# list the running containers +docker ps + +# stop the container +docker stop webserver + +# remove the container +docker rm webserver +``` + +--- + +### Dockerfile Example - static HTML site + +```Dockerfile +FROM nginx:alpine +COPY . /usr/share/nginx/html +``` + +``` +# build +docker build -t webserver-image:v1 . + +# run +docker run -d -p 8080:80 webserver-image:v1 + +# display +curl localhost:8080 +``` + +### Dockerfile Example - Node site + +```Dockerfile +# Base image +FROM alpine +# Install Node and NPM using the package manager +RUN apk add -update nodejs nodejs-npm +# Copy the files from the build context +COPY . /src +WORKDIR /src +# Run a command +RUN npm install +# Adds metadata +EXPOSE 8080 +# What to run +ENTRYPOINT ["node", "./app.js"] +``` + +--- + +### Mapping a Volume + +``` +# create a volume +docker volume create myvol + +# inspect the volume +docker volume inspect myvol + +# list the volumes +docker volume ls + +# run a container with a volume +# `-v myvol:/app` mapping the volume to a logical folder +docker run -d --name devtest -v myvol:/app nginx:latest +``` + +#### Mapping to a local folder + +``` +# run a container with a volume +# `d:/test` specify a folder +docker run -d --name devtest -v d:/test:/app nginx:latest + +# inspect the container +docker inspect devtest +``` + +--- + +## Docker Compose + +- Plugin for docker + - *define and run multi-container Docker applications with [YAML](./yaml_cheat_sheet.html)* +- `docker: 'compose' is not a docker command.` + - **Solution:** On some systems (I think this pertains to Docker Compose V1), `docker compose` should be run as `docker-compose` + +### Example Docker Compose file + +The following example defines 3 containers: + +```yaml +version: '3.9' + +services: + webapi1: + image: academy.azurecr.io/webapi1 + ports: + - '8081:80' + restart: always + + webapi2: + image: academy.azurecr.io/webapi2 + ports: + - '8082:80' + restart: always + + apigateway: + image: academy.azurecr.io/apigateway + ports: + - '80:80' + restart: always +``` + +### Docker Compose Example + +``` +# build the service +docker compose build + +# builds, (re)creates, starts, attaches to containers for a service +docker compose up + +# list the services +docker compose ps + +# bring down what was created by UP +docker compose down +``` + +### Resource Limits example + +```yaml +services: + redis: + image: redis:alpine + deploy: + resources: + limits: + cpus: '0.50' + memory: 150M + reservations: + cpus: '0.25' + memory: 20M +``` + +### Environment Variables example + +```yaml +services: + web: + image: nginx:alpine + environment: + - DEBUG=1 + - FOO=BAR +``` + +Environment variables can be overwritten at the command line: +- `docker compose up -d -e DEBUG=0` + +#### Referencing an environment variable + +``` +# set an environment variable +export POSTGRES_VERSION=14.3 + +# powershell +$env:POSTGRES_VERSION="14.3" +``` + +```yaml +services: + db: + images: "postgres:${POSTGRES_VERSION}" +``` + +You can also create a `.env` file containing environment variable definitions + +### Dependence example + +```yaml +services: + app: + image: myapp + depends_on: + - db + db: + image: postgres + networks: + - back-tier +``` + +The example Docker Compose file has the `db` service start before the `app` service can start + +### Volumes example + +```yaml +services: + app: + image: myapp + depends_on: + - db + db: + image: postgres + volumes: + - db-data:/etc/data + networks: + - back-tier + +volumes: + db-data: +``` + +### Restart Policy example + +```yaml +services: + app: + image: myapp + restart: always + depends_on: + - db + db: + image: postgres + restart: always +``` + +- By default, containers will not be restarted upon reboot +- Possible options include: + +| | | +|------------------|-------------------------------------------------| +|`no` |Does not restart a container under any circumstances (default)| +|`always` |Always restarts the container until its removal | +|`on-failure` |Restarts a container if the exit code indicates an error| +|`unless-stopped` |Restarts a container irrespective of the exit code but will stop restarting when the service is stopped or removed| diff --git a/index.md b/index.md new file mode 100644 index 0000000..72700b1 --- /dev/null +++ b/index.md @@ -0,0 +1,6 @@ +# Docker Containers and Kubernetes Fundamentals - personal notes + +This repository contains my personal set of notes, based on the video lecture series "Docker Containers and Kubernetes Fundamentals - Full Hands-On Course" from FreeCodeCamp.org [kTp5xUtcalw](https://www.youtube.com/watch?v=kTp5xUtcalw). My notes are mostly arranged to serve as cheat sheets for using Docker and Kubernetes. They are broken into the following pages: + +- [Docker Cheat Sheet](./docker_cheat_sheet.html) +- [YAML Cheat Sheet](./yaml_cheat_sheet.html) diff --git a/yaml_cheat_sheet.md b/yaml_cheat_sheet.md new file mode 100644 index 0000000..b00e78b --- /dev/null +++ b/yaml_cheat_sheet.md @@ -0,0 +1,30 @@ +# YAML Cheat Sheet + +### YAML example + +```yaml +# Comments in YAML look like this + +key: value +another_key: Another value goes here. +a_number_value: 100 + +# Nesting uses indentation. 2 space indent is preferred (but not required). +a_nested_map: + key: value + another_key: Another Value + another_nested_map: + hello: hello + +# Sequences (equivalent to lists or arrays) look like this +# (note that the '-' counts as indentation): +a_sequence: + - Item 1 + - Item 2 + +# Since YAML is a superset of JSON, you can also write JSON-style maps and sequences: +json_map: {"key": "value"} +json_seq: [3, 2, 1, "takeoff"] +``` + +Checking YAML files' formatting can be done using `yamllint` -- cgit