Docker

Containerization means, that your application runs in an isolated container, that is an explicitly defined, reproducable and portable environment. The analogy is taken from freight transport where you ship your goods in containers. [1]_

Docker solves the problem of having identical environments across various stages of development and having isolated environments for your individual applications. [1]_ They persist one main executable/app per container.

Basic management

## List Docker CLI commands
docker
docker container --help

## Display Docker version and info
docker --version
docker version
docker info

## List Docker images
docker image ls

## List Docker containers (running, all, all in quiet mode)
docker container ls
docker container ls --all
docker container ls -aq

## Delete an image
docker image rm <image id>
# Remove all images from this machine
docker image rm $(docker image ls -a -q)
docker pull

# port forwarding
# -p host-port:container-port
docker run --name my-nginx -p 80:80 nginx:1.10.1-alpine
docker info

docker ps
docker ps -a    # List all, even the stopped ones
docker stop
docker start
docker restart
docker logs --tail 50 --follow --timestamps instance-name
docker inspect
docker rm
# Launch shell
docker exec -ti my-nginx /bin/sh

Dockerfile

# An example from https://docs.docker.com/get-started/part2/#dockerfile
# Use an official Python runtime as a parent 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 --trusted-host pypi.python.org -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"]

Build

# Create image using current directory's Dockerfile
docker build -t image-name .

Run

# Map port host:container
docker run -p 4000:80 image

# Run in background
docker run -p 4000:80 image

# Run Ubuntu Bash
docker run -it ubuntu bash

docker run -p 5900:5900 ubuntu-firefox

Stop

# Gracefully stop the specified container
docker container stop <hash>

# Force shutdown of the specified container
docker container kill <hash>

Share

# 1.
docker tag image username/repository:tag

docker push username/repository:tag

Docker swarm

# docker-compose.yml
version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: username/repo:tag
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet
networks:
  webnet:
docker swarm init

docker stack deploy -c docker-compose.yml <appname>

docker service ls

# A single container running in a service is called a task.
# Tasks are given unique IDs that numerically increment,
# up to the number of replicas you defined.
# List the tasks for your service:
docker service ps <service>

docker inspect <task or container>

# Take down an app
docker stack rm <appname>

# Take down the swarm
docker swarm leave --force

Cleanup containers & images for disk space

# https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes
# https://github.com/moby/moby/issues/21925
docker rm $(docker ps -a -q)






RUN apt-get install -y
libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev python-dev –no-install-recommends gcc