- Consistent environment
Ability to create predictable environments that are isolated from other applications. - Run anywhere
Ability to run virtually anywhere: on Linux, Windows, and Mac operating systems; on virtual machines or bare metal; on a developer’s machine or in data centers on-premises; in the public cloud. - Isolation Ability to virtualize CPU, memory, storage, and network resources at the OS-level, providing developers with a sandboxed view of the OS logically isolated from other applications.
- Standardized unit of software that allows developers to isolate their application from its environment.
- Packages code and all its dependencies, so the application runs quickly and reliably from one computing environment to another.
- Container platforms:
- Docker
- LXC (Linux Containers)
- rkt (CoreOS Rocket)
- Docker since 2013
- Became an industry standard
- Easy to use
- An excellent building block (for software delivery according to DevOps perspectives)
- Portable anywhere (Linux, Windows, Data center, Cloud, Serverless, etc.)
Docker created the industry standard for containers. - Lightweight
Containers share the machine’s OS system kernel and therefore do not require an OS per application, driving higher server efficiencies and reducing server and licensing costs. - Secure
Applications are safer in containers and Docker provides the strongest default isolation capabilities in the industry.
- client (CLI via REST API)
- server (Docker host) - deamon, containers, images, volumes
- registry - Docker Hub
Docker engine:
- Server (daemon process)
- REST API
- Client (CLI)
Docker objects
- Images
- Containers
- Networks
- Volumes
- ... some more objects
Docker objects are available to be observed and controlled using the command docker <object-name>
.
- Developers write code locally and share their work with their colleagues using Docker containers.
- They use Docker to push their applications into a test environment and execute automated and manual tests.
- When developers find bugs, they can fix them in the development environment and redeploy them to the test environment for testing and validation.
- When testing is complete, getting the fix to the customer is as simple as pushing the updated image to the production environment.
docker help
- explore commandsdocker ps
- list runnig containersdocker run <CONTAINER_NAME>
- list runnig containersdocker container
- manage containersdocker image
- manage imagesdocker volume
- manage volumesdocker network
- manage networksdocker exec
- run a command in a running container
Example: docker run -i -t ubuntu /bin/bash
Dockerfile
- configuration file for building images.
Python:
FROM ubuntu:15.04
COPY . /app
RUN make /app
CMD python /app/app.py
Node.js:
FROM node:12
WORKDIR /usr/src/app
COPY package.json .
RUN npm install
COPY . .
CMD [ "npm", "start" ]
To build an image:
docker build .
By default all the files created inside a container are stored on writable container layer. This means:
- the data is not persistent
- You can’t easily move the data somewhere else
Storage types:
- volumes - prefered
- bind mounts
- tmpfs mount
Example:
docker run -v path/on/host:/app nginx:latest
Docker Compose
- is a tool for defining and running multi-container Docker applications.
- Uses a compose (configuration) file
- A way to document and configure all of the application’s service dependencies (databases, queues, caches, web service APIs, etc)
- Only one single command to create and start containers -
docker-compose up
-
Development environments
When you’re developing software, the ability to run an application in an isolated environment and interact with it is crucial. The Compose command line tool can be used to create the environment and interact with it. -
Automated testing environments
An important part of any Continuous Deployment or Continuous Integration process is the automated test suite. Automated end-to-end testing requires an environment in which to run tests. Compose provides a convenient way to create and destroy isolated testing environments for your test suite. -
Single host deployments
To deploy to a remote Docker Engine.
A three-step process:
- Define your app’s environment with a
Dockerfile
so it can be reproduced anywhere. - Define the services that make up your app in
docker-compose.yml
so they can be run together in an isolated environment. - Run docker-compose up and Compose starts and runs your entire app.
Installing Wordpress website.
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
Base commands:
docker-compose up
- Create and start containersdocker-compose down
- Stop and remove containers, networks, images, and volumesdocker-compose exec
- Execute a command in a running containerdocker-compose scale
- Set number of containers for a service