# Docker och Docker Compose

Nivå: beginner | Målgrupp: Programutvecklare | Förkunskaper: Erfarenhet av programmering i något språk
https://www.ribomation.se/programmerings-kurser/docker-och-compose/

I denna kurs får du lära dig allt du behöver veta om Docker för att bygga och driftsätta applikationer
i containrar. Du får lära dig om såväl Compose för att assemblera ihop flera images till ett
kluster av containrar, som Dockerfile för att designa egna applikations images.

I denna kurs får du lära dig allt du behöver veta om Docker för att bygga och driftsätta applikationer
i containrar. Du får lära dig om såväl Compose för att assemblera ihop flera images till ett
kluster av containrar, som Dockerfile för att designa egna applikations images.

- Create containers using existing docker images for your application
- Create container clusters using `docker compose`
- Create your own docker images using `Dockerfile` and `docker build`
- Combine _Compose_ with `Dockerfile`
- Configure web servers and database servers
- Utilize docker networks
- Create an Ubuntu image from scratch

#### Background / Overview
- History
- What is a (Docker) container?
- Containers vs. Virtual machines
- Fundamental concepts
- Docker Hub
- What is behind Docker?
- How does Docker run on Windows?

### Using Docker
In this section we will use the docker CLI to pull and launch
various ready-made docker applications.

#### Installation
- Docker Desktop
- System requirements Windows computers
- System requirements Mac computers
- System requirements Linux computers
- Installation of Docker Engine
- Extensions

#### Brief about Linux
- Linux distributions
- Distribution versions and their names
- Debian vs. Ubuntu vs. Alpine
- Installing program packages in Linux
- Essential Linux commands

#### Basic Docker Commands
- Using the Docker CLI - `docker`
- Getting help information
- Download an image - `docker pull`
- Pull from a different registry
- List images
- Remove an image
- Create/launch a container
- List containers
- How step into a container
- Copy files to/from a container
- Remove containers
- How container names are created
- Stopping/starting containers
- Obtain run-time information
- Port mapping

#### Docker Volumes
- Persisting data
- Volume types
- Create a named volume
- Local development using Docker
- Example: compile a C++ program without an installed compiler
- Volume maintenance actions
- Finding the volume file in the host file system

#### Configuration
- How to configure a server?
- How to pass environment variable values
- How to connect to a database
- Sample usage of an MySQL database

### Docker Cluster
In this section we will run more than one container and let them interact with each other.
You will learn about docker networks and docker compose.

#### Brief Network Theory
- How do a computer communicate with another computer
- What is an IP number
- What is a port number
- What is a hostname
- What is a network
- What is DNS
- What is IPv6
- How to use `ifconfig`
- What is a loopback address
- What is a private IP
- What is a subnet
- What is CIDR
- Special CIDR addresses
- Address allocation
- Route table
- What is a firewall
- What is NAT / IP masquerading

#### Docker Network
- What is a Docker network
- Network types
- Default networks
- Meta-data about a network
- Create a Docker network
- Associate a container with a network
- How to containers might interact

#### Docker Compose
- What is docker compose and its purpose
- Background
- What is YaML
- Elements of a compose script
- Creating a compose script
- Creating and running a compose service
- Commonly used compose actions
- Introspective compose actions
- Command-line options
- Dealing with configuration data
- Setting a simple WordPress system with a MySQL database
- Using so-called secrets
- Service dependency
- Health checks
- Restart policy
- YaML extensions used in compose scripts

### Creating Images
In this section we will learn how to create our own image, using a Dockerfile.

#### Dockerfile
- What is a `Dockerfile`
- Dockerfile syntax
- The most essential directives of a Dockerfile
- Create an image - `docker build`
- Simplest possible Dockerfile
- How to build and run it
- Understanding and settings Docker tags
- A still simple, but more realistic Dockerfile
- The Express.js server code
- Running the system outside Docker
- The Docker file
- Build the image
- Create and run a container with our Express.js app

#### More Dockerfile Directives
- Additional ways to feed the build
- Using a `.dockerignore` file
- More Dockerfile directives
- Two variants of _command_
- Change the default shell
- Different RUN command forms
- Understanding here-document syntax
- ENTRYPOINT vs. CMD
- COPY vs. ADD
- Environment variables (ENV)
- Configuration variables (ARG)
- How to create user and group on Debian/Ubuntu/Alpine
- Change the owner of files
- Tell Docker which user to run as

#### Multi-Stage Dockerfile
- Let's start with a simple Java application
- Sample build & run
- What is the container size?
- Understand build-time vs. run-time
- Setting up a multi-staged build
- Improved Dockerfile, with separation of build from runtime
- Much better image size

#### Dockerfile - Best Practices
- Why best practices
- Use official images
- Use version tag
- Pick a small/lean image
- Reorder to improve layer caching
- Reduce the layer count
- Remove stuff not needed any longer
- Use .dockerignore
- Use multi-staged build
- Create dedicated user
- Spell out options
- Utilize parallel stages

#### Image from Scratch
- How do we create a base/parent image
- Using `FROM scratch`
- Let's build an all-inclusive C++ application
- Sample build & run
- How to create Ubuntu image from scratch
- Usage of debootstrap
- Usage of `docker import`
- Using our own parent image

### Applications

#### Compose with Dockerfile
- Compose script with build directives
- Syntax for the build directive
- Recommendation
- Building compose images

#### Node.js: Express.js / TypeScript / Vite.js Application
- Walkthrough of the source code

#### Java: Spring-Boot / MySQL Application
- Walkthrough of the source code

#### C++: REST-WS Application
- Walkthrough of the source code

### _Section_: Publishing Images
In this section we discuss various ways of make your image available for others to use.

#### Registries & Repositories
- What is a registry and a repository
- Different forms
- Importance of tagging
- Generic step to publish and image

#### Docker Hub Registry
- Using Docker Hub
- Publish to Docker Hub
- Pulling from your own corner of Docker HUb

#### Private Registry
- What is a private registry
- Installation of a private/local registry
- Publishing to a local registry
- Pulling from a local registry

#### Google Cloud (GCP) Registry
- What is GCP
- How Google supports containers
- Publishing to a GCP registry
- Pulling from a GCP registry
