Docker is a convenient technology to create portable isolated and reproducible runtime environments for design-time development and production hosting of applications
Main technology concepts
Docker is an abstraction technology which isolates an applications runtime environment from host infrastructure, and thereby increases the applications portability by encouraging applications which run upon a Docker Engine, on any host.
This increases uniformity, aids reproduction, decreases host specific configuration needs and aids development in isolation.
Docker hosts are different to virtual machine hosts
- Docker abstracts the application layer, combining application code and application dependencies into an image. Docker sits above the OS and therefore a host has only one OS to maintain, even when hosting multiple concurrent Docker Containers.
- VMs abstract physical hardware, resulting in systems which act as independent hosts. Multiple VMs result in multiple OS instances, including licencing costs, maintenance of multiple OS kernels and therefore multiply patching obligations
Image
- An image is a read-only template with instructions for creating a Docker container.
Container
- Running instance of an image, with optional communication and technologies to interact with the host environment and other containers
- Changes to a container occur as a delta, above the original Docker container image
Docker Engine
- Long running server acting as a daemon
- Serves a REST API for managing containers and images and other dependencies
- Serves a CLI (Command Line Interface) client
docker
Docker Architecture
Docker containers communicate with the Docker daemon which acts upon the host OS and infrastructure, to maintain the lifecycle and provide application communication needs
Docker Hub
- Stores a secure collection of container images for public and private use
Docker Swarm
Docker Swarms enable applications to be quickly scaled over docker hosts which have swarm capabilities enabled.
Swarms can be considered a mechanism for getting multiple instances of an application to run concurrently.
Docker Swarm manages host networking, load balancing, service discovery, scaling on behalf of the application administrator.
Kubernetes
Kubernetes is a Google technology which automates deployment, scaling and management of containerized applications. You can use Kubernetes to manage Docker container application lifecycles instead of Docker Swarm, however they are separate technologies,are independent of each other and require different deployment concepts.
Dockerfile
A Dockerfile is a recipe for building a container image
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
Dockerfile keywords
- FROM is used to denote a source of a base container image
- WORKDIR : the path in the image where subsequent commands will occur
- ENV : set environment variables for the Dockerfile and other scripts
- RUN : execute a command within the container as it is constructed
- COPY : copy files from the local file system into the target container file system
- EXPOSE open a port for inbound communication
- CMD : set a command to run when a new instance of the container is instantiated
Docker Compose
A docker-compose.yml
file describes how to deploy containers and the associated dependencies
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
Start with a simple command line operation
docker-compose up
Detached mode startup
docker-compose up -d
When using detached mode startup, the docker composition can be stopped without removing the container or dependencies
docker-compose stop
Stop just as easily, removing all the containers and dependencies in one step
docker-compose down
Overview
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
Docker CLI
Run an instance of the ubuntu docker image, and create an interactive session to a bash shell
docker run -i -t ubuntu /bin/bash
Acknowledgements - Cheat Sheet
CI/CD with Docker
Acknowledgements to Docker