This article is a work in progress. Email your suggestions to kevin@fivemin.net.
Build, swap and remove infrastructure in a text file
Docker containers are a set of tools to quickly and easily build, swap out and remove infrastructure layers. You get 3 for one, on one hand you learn bash and containers while streamlining your workflow.
Under the hood Docker is a toolkit for two very old Linux features that are used to compartmentalize processes:
- cgroups and
- namespaces.
Docker makes these otherwise arcane features much easier to use.
Main benefits
... of Containers include their reproducibility, portability and lightweightness.
- Reproducibility - Getting a service to a certain state and distributing that application to its users is one of the primary concerns for any system administrator. Containers are written in code using a layered approach and forces the administrator to codify their environments. You are introduced to the world of Infrastructure-as-code (IAAS) were servers are created, updated and destroyed in a text file.
- Portability is concerned with easily moving resources between servers/nodes in the network and being able to work with containers cross-platform or cross-cloud. When acquisitions happen (such as akami buying linode) the portability of "infrastructure as a code"-workflow enables much quicker deployment onto a new cloud platform.
- The smallest containers need only a few megabytes at most, start up in seconds and runs as one process, something that makes them very lightweight with a small foot print compared to traditional Virtual Machines that that can easily become gigabytes large and takes minutes to start.
Other benefits include
- A growing ecosystem of 'pre made' service definitions by the community
- Interoperability between major container platforms: Docker, Kubernetes and Podman. "create once bring the container everywere".
Image Creation like lego bricks
Image creation starts in text file by importing a 'base image' to work on and adding customization ontop of, very much like lego bricks. Everything written in the text file is parsed and converted into an image by the container platform. The image is like a compiled binary of your source code which can be distributed around your servers/nodes. Containers are run as defined in the text.
Important day 1 stuff
Dockerfiles build docker images which run docker containers.
- Dockerfile = text based instructions for docker to build/assemble an image. Keywords such as RUN, COPY, MOVE dicatate the location and startup of files and processes inside the image. Each "keyword/action" creates a layer in the docker image. Dockerfiles are read by the docker process from top to bottom.
- Docker image = read only archive containing executable files built from the dockerfile.
- Docker container = A running version of a docker image, loaded into memory
Dockerfiles often contain references to other docker images to simplify containers. Instead of writing an operating system container such as debian one can simply reference the debian container as the base container and build upon it. Docker downloads docker images/files according to the dockerfile text specification.
Docker filesystem locations
Docker has its own root directory (simillar to httpd/apache) which can be viewed using the docker info command, the default location is /var/lib/docker/. Important sysadmin subfolders in the docker root directory include:
- Dockerfiles - Dockerfiles can be stored anywere on the system and parsed by docker.
- /var/lib/docker/images - Images created by reading dockerfiles are stored here and can be listed using the command docker images ls --all.
- /var/lib/docker/containers - Docker container location, list all containers using the command docker container ls --all
- /var/lib/docker/volumes - Persistent files are stored in either docker volumes subfolder or using bind mounts on the host system.
Use the command docker info for a quick overview of your installation.
Docker info example (click to open)
root@fivemin-virtualization:~# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Docker Buildx (Docker Inc., v0.8.2-docker)
compose: Docker Compose (Docker Inc., v2.6.0)
scan: Docker Scan (Docker Inc., v0.17.0)
Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 2
Server Version: 20.10.17
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
runc version: v1.1.2-0-ga916309
init version: de40ad0
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: XX
Operating System: Debian GNU/Linux 11 (bullseye)
OSType: linux
Architecture: XX
CPUs: XX
Total Memory: XX
Name: fivemin-virtualization
ID: XX
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Persistance vs ephemeral storage
xxx
this section will be updated ..
Resources
Were did my init system go?
How do filesystems work in containers?
The container lifecycle
Build containers
Manage networks
Manage volumes
Resource
Read this:
The MariaDB knowledgebase has some great writeups about docker.
MariaDB KnowledgeBase
The container lifecycle
Push/Pull, Create from image, list containers, Run/Stop, Persistent storage, Replace, Delete.
Environment variables
Secrets
Stuff we want to do
List all containers
List all running containers
List all images (that create containers)
Link containers together (create a shared network, linking is deprecated)
docker network create fivenet --driver bridge --subnet 10.13.37.1/24
docker network ls # confirm network created
docker container run --name maria --hostname maria --network fivenet -e MARIADB_ROOT_PASSWORD=password mariadb
docker container run --name phpmyadmin --hostname phpmyadmin --network fivenet
Docker Documentation Highlights Networking
Containers on the default bridge network can only access each other by IP addresses, unless you use the --link option, which is considered legacy. On a user-defined bridge network, containers can resolve each other by name or alias.
All containers without a --network specified, are attached to the default bridge network.
Containers connected to the same user-defined bridge network effectively expose all ports to each other. For a port to be accessible to containers or non-Docker hosts on different networks, that port must be published using the -p or --publishflag.
User-defined bridge networks are best when you need multiple containers to communicate on the same Docker host.
Host networks are best when the network stack should not be isolated from the Docker host, but you want other aspects of the container to be isolated.
Overlay networks are best when you need containers running on different Docker hosts to communicate, or when multiple applications work together using swarm services.
Macvlan networks are best when you are migrating from a VM setup or need your containers to look like physical hosts on your network, each with a unique MAC address.
Third-party network plugins allow you to integrate Docker with specialized network stacks.
Download an image
Create a container
docker container create --help
$ docker container create --help
Usage: docker container create [OPTIONS] IMAGE [COMMAND] [ARG...]
.....
docker container create --name [name] [image]Start a container
Example 1, starting phpmyadmin
NOTE THIS VERSION HAS NO DATA PERSITANCE BETWEEN CONTAINER "reboots"
docker pull mariadb
docker pull phymyadmin
docker container run --name maria --hostname maria --network fivenet -e MARIADB_USER=admin -e MARIADB_PASSWORD=admin -e MARIADB_ROOT_PASSWORD=root mariadb
docker run -dit --name myadmin --hostname myadmin --network fivenet -p 5555:80 -e PMA_HOST=maria -e PMA_PORT=3306 -e PMA_USER=root -e PMA_PASSWORD=root phpmyadmin
go into a container
docker exec -it <container name> /bin/bash
Creating a compose script
Compose can reduce a multi-page “developer getting started guide” to a single machine readable Compose file and a few commands.