Docker cheatsheet
A quick brain dump about Docker.
Docker is a containerisation technology that lets you run one or more processes inside an isolated environment, the container. Specifying the environment in which a process or application will run makes it easy to have reproducible deployments and consistent development environments.
Concepts
Image: The initial state for a container; filesystem, exposed ports, command to run. Defined by a Dockerfile
file.
Container: A running image. An isolated file system and process space. Has an ephemeral filesystem but can have local directories or volumes mounted to allow for persistence.
Network: Containers might be connected to one or more virtual networks to allow communication with other containers or the host.
Volume: A persistent directory that can be attached to a running container.
Stack: A description of one or more containers that cooperated as a system e.g. web server, database and cache. The description can include the volumes and networks that they need. Defined in a docker-compose.yml
file.
Host: A machine running the Docker engine. This is where containers run.
Swarm: One or more Docker host machines acting as a single host.
Service: One or more copies of a container running on a swarm.
Swarm mode
A Docker host can start/join a swarm. The advantage of a swarm over individual hosts is that we can use a declarative model rather than imperative. Given a declaration of a stack, the swarm will automatically monitor and manage the desired services across its nodes; in the single host mode that could be achieved by manually monitoring and issuing Docker commands against multiple hosts.
Concept | Single host | Swarm |
---|---|---|
Run unit | Container | Service |
Run command | docker run ... |
docker service start ... |
Orchestration | Docker Compose | Docker swarm |
Orchestration command | docker-compose up ... |
docker stack deploy ... |
Orchestration source | Images or Dockerfiles | Images |
Interaction between phases
Development: Define the development environment in docker-compose.yml
, optimise for developer productivity; fast feedback and consistency.
CI: Define each process' build process and execution in Dockerfile
s. Build and publish the images ready to be deployed to production.
Deployment to production: Declare docker-compose.yml
file that references published images. Deploy to a swarm. Test the stack locally using Docker compose.
Misc
Deploy/update a stack on a remote swarm host by running stack command via SSH and sending local compose file:
ssh user@swarm_manager "docker stack deploy --compose-file - stack-name" < docker-compose.yml
Generate a password hash using htpassword
:
docker run \
--entrypoint htpasswd \
registry \
-nbB \
testuser \
testpassword