Docker notes

Dockerfile

An example from docs.docker.com:

# Use an official Python runtime as a base image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Dockerfile reference and
Dockerfile best practices
on docker docs.

install order

Install things in the order of how frequently they are likely to change.

Build

docker build -t <image tag> .
docker build -f Dockerfile.my  # specify dockerfile name

Containers

Run (for web app):

docker run -p 4000:80 <image tag>

In background (detached mode):

docker run -d <image tag>

List containers:

docker ps  # running
docker ps -a  # all containers

Stop:

docker stop <container id>
docker kill <container id>  # force stop

Remove (from current machine):

docker rm <container id>
docker rm $(docker ps -a -q)  # remove all containers

Images

List images:

docker images -a

Remove (from current machine):

docker rmi <image id>
docker rmi $(docker images -q)  # remove all images

Networks

List networks:

docker network ls

Inspect:

docker network inspect <network name>

Create:

docker network create -d <driver> <network name>

Running containers inside a network:

docker run --net=my_network ...

Troubleshooting

In case of failed image build, use --debug key:

image build --debug

sh into the image:

docker run -it <container id> bash

On OSX I have an error when I run shub image build for first time: Detected error connecting to Docker daemon's host.

Try this to solve it:

docker-machine restart default
eval $(docker-machine env default)

AWS ECS (EC2 Container Service)

Cluster ->
instance ->
image ->
task definition ->
service

Vocabulary

Service

A container in production.
Codifies the way the image runs (which ports to use, how many replicas we need).

docker-compose.yaml example:

version: "3"
services:
  web:
    image: username/repository:tag
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet
networks:
  webnet:
version: "3"
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - postgres
    environment:
      - DB_HOST=postgres
      - DB_PASSWORD=abc123
      - API_HOST=app
      - PYTHONDONTWRITEBYTECODE=1
    volumes:
      - ./src:/src
      - ./tests:/tests
    posrts:
      - "5005:80"
  postgres:
    image: postgres:9.6
    environment:
      - POSTGRES_USER=allocation
      - POSTGRES_PASSWORD=abc123
    ports:
      - "54321:5432"

PYTHONDONTWRITEBYTECODE - instructs not to write .pyc files.

Swarm

A swarm is a group of machines that are running Docker and have been joined into a cluster.

Dagger

Dagger is an integrated platform to orchestrate the delivery of applications to the cloud from start to finish. The Dagger Platform includes the Dagger Engine, Dagger Cloud, and the Dagger SDKs.

Licensed under CC BY-SA 3.0