Docker

ext: Guide

  • Virtualizes the OS and dependencies
    • Making components of the application modular and versioned

Namespaces

Used so that the containers practically have different OS's

VMs share hardware, containers share the OS

CLI

# detached - background
# port 8080 on mine, 80 in the container
docker run -d -p 8080:80

docker stop CONTAINER_ID

# Show all containers
docker ps -a

State

Containers are stateless

Persistent state comes from storage volumes

docker volume create <name>
docker volume inspect <name>

Volumes

mount to the /var/lib/ghost/content volume

docker run -d -e NODE_ENV=development -e url=http://localhost:3001 -p 3001:2368 -v ghost-vol:/var/lib/ghost/content ghost

docker volume inspect ghost-vol:

Networking

docker run -d --network none
docker network create <network_name>
docker network ls

# run the container, using the network, and mounting the file
docker run --network caddytest --name caddy1 -p 8001:80 -v $PWD/index1.html:/usr/share/caddy/index.html caddy

Layers

As a container builds, the builder attempts to use layers (build stages) from previous builds

(ex. if project files are unchanged then COPY . . is cached)

If a layer isn't cached, then the Cache for all of the following layers is invalidated

  • So put FROM, WORKDIR first

Bind Mounts

  • Share data between the host and a container - like a symlink between a container and host

Dockerizing a DB

# set environment variables, pull and run mysql:latest
docker network create my-network
docker run --name my-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=mydb --network my-network -d mysql:latest