VM
- Each VM needs an OS, which uses significant resources
Docker container
In terms of virtualization:
- Way to package application with all the necessary dependencies and configuration
- Can be considered as layers of images:
- alpine:3.10 – linux base image
- postgres:10.10 – application image
- ..
- Portable artifact, easily shared and moved around
- Containers start up very quickly
- Containers resource efficient
- Each container is isolated from other containers
- A container has all needed for running the application:
- code
- settings
- dependencies
Development & deployment more efficient
DockerHub
- Public repository
- hub.docker.com
Docker file
- Instructions for building a Docker image.
This dockerfile installs Apache webserver and sets the default command to run the Apache daemon:
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y apache2
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
- Build the Docker image:
docker build -t my-image .
- Run the Docker image:
docker run -p 8080:80 my-image
Docker-compose
- Define and run multi-container Docker applications.
- Uses configuration file (docker-compose.yml) to specify the different containers and their configurations, and then creates and runs them with a single command.
version: '3'
services:
web:
image: nginx:latest
ports:
- "80:80"
database:
image: postgres:latest
environment:
- POSTGRES_PASSWORD=mypassword
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
- two containers: “web” and “database“
- web container: NGINX, exposes port 80 to the host
- database container: PostgreSQL, sets the POSTGRES_PASSWORD environment variable. It also mounts a volume named “db-data” at the specified location in the container.
Run docker compose application
docker-compose up