Notes about Docker

Setup on MacOS

  1. Install Docker for Mac (will ask for privileges and be shown on your top bar). See https://docs.docker.com/docker-for-mac/
  2. Run docker run hello-world to see if everything is setup

Examples of config files

Example of a Dockerfile:

    # Starts from Ubuntu Trusty (14.04) and Nodejs 4.6
    FROM nodesource/trusty:4.6

    # Update APT sources
    RUN apt-get clean -y && apt-get update -y

    # Install dependencies
    RUN apt-get install -y \
     g++-4.8 \
     graphicsmagick \
     libcairo2-dev \
     libgif-dev \
     libopencv-dev \
     libjpeg8-dev \
     libpng12-dev \
     libwebp-dev \
     libtiff4-dev \
     libexif-dev \
     libgsf-1-dev \
     libblas-dev \
     liblcms2-dev \
     libxml2-dev \
     libpango1.0-dev \
     librsvg2-dev \
     build-essential \
     unicode-data \
     automake \
     gobject-introspection \
     gtk-doc-tools \
     libglib2.0-dev \
     swig \
     ruby \
     wget

    # Define production (default) or dev env by build-arg
    ARG NODE
    ENV NODE_ENV ${NODE}

    # Get NPM_TOKEN from `--build-arg NPM_TOKEN=${NPM_TOKEN}`
    ARG NPM_TOKEN
    # Now we can npm install private packages
    RUN echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
    # Setup our working directory
    RUN mkdir -p /app/src
    RUN mkdir -p /app/vendor
    WORKDIR /app/src
    # Fetch and setup env vars to non-deb deps
    RUN wget ftp://vps.jonnor.com/imgflo/libvips-0.3.2-heroku.tgz
    RUN mkdir -p /app/vendor/vips
    RUN tar -xvzf libvips-*.tgz -C /app/vendor/vips
    ENV PKG_CONFIG_PATH /app/vendor/vips/lib/pkgconfig
    ENV LD_LIBRARY_PATH /app/vendor/vips/lib
    ENV LIBRARY_PATH /app/vendor/vips/lib
    ENV CPLUS_INCLUDE_PATH /app/vendor/vips/include
    RUN wget ftp://vps.jonnor.com/imgflo/libccv-0.2.5-heroku.tgz
    RUN mkdir -p /app/vendor/libccv
    RUN tar -xvzf libccv-*.tgz -C /app/vendor/libccv
    COPY hacks/*.pc /vips/lib/pkgconfig/

    # Install node packages
    COPY . /app/src
    RUN npm install -g grunt-cli node-gyp
    RUN npm install --unsafe-perm
    #RUN npm install coffee-loader json-loader phantomjs coffee
    RUN rm -f .npmrc

    # Install Heroku CLI
    RUN wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh

    ENV POLY_BROWSER `node -e "console.log(require('phantomjs-prebuilt').path)"`
    RUN grunt build
    #RUN ./knex migrate:latest
    #RUN ./node_modules/.bin/coffee scripts/addTestSite.coffee
    #RUN ./node_modules/.bin/msgflo-setup --participants --ignore serving graphs/apis.fbp --shell bash
    #RUN PORT=3000 heroku local:start -e secrets.env

Example of a docker-compose.yml file:

# For dev environment, probably need another config for prod

version: '2'
services:
  rabbitmq:
    image: rabbitmq:3-management
    ports:
      - "15672:15672"
    restart: always
  postgres:
    image: postgres:9.4
    environment:
      POSTGRES_PASSWORD:
      POSTGRES_USER: postgres
      POSTGRES_DB: apis_test
    restart: always
  apis:
    env_file: secrets.env
    environment:
      DATABASE_URL: postgres://postgres:@postgres/apis_test
      CLOUDAMQP_URL: amqp://rabbitmq
      CLOUDAMQP_URL_SERVING: amqp://rabbitmq
      POSTGRES_NOSSL: 1
      GRID_API_HOST: localhost
      GRID_API_PORT: 443
      DEBUG: msgflo:error
      GRID_ENABLE_VARIANTS: 'false'
      CXX: g++-4.8
      GCM_SENDER_ID: 442173694387
      URL2PNG_API_ID: P53FFBB0E2600A
      TREPAK_SLACK_HOOK: https://hooks.slack.com/services/T026JSYGN/ZYZ789/GRIDDER
    build:
      context: .
      args:
        NPM_TOKEN: ${NPM_TOKEN}
        NODE: development
    ports:
      - "3000:443"
    links:
      - rabbitmq
      - postgres

To use version: 2 remember to have Compose 1.6.0+ and require a Docker Engine of version 1.10.0+.

And here an example of a .travis.yml to build that image:

language: node_js
sudo: false
services:
- docker
node_js:
- 4.6
env:
  matrix:
  - DOCKER_VERSION=1.12.3 DOCKER_COMPOSE_VERSION=1.8.1
addons:
  apt:
    sources:
    - ubuntu-toolchain-r-test
    packages:
    - g++-4.8
before_install:
- docker -v
- sudo rm /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin
- docker-compose -v
script:
- npm test

Useful commands

Docker Compose

Force rebuilding some services (will ignore services that are images):

docker-compose build

If that's not enough, the following could help:

docker-compose up --force-recreate

Start the services specified on the docker-compose.yml on current folder:

docker-compose up

Remove current image:

docker-compose rm

Run something on a service:

docker-compose run <service_name> bash

If you get out of space, clean the following disk image and restart Docker Engine (you'll lose all your images):

rm -rf ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2

Docker Engine

# List all running containers
docker ps

# List all containers (even those not running)
docker ps -a

# Run a container
docker run <container name>

# Start/stop a container
docker start <container name>
docker stop <container name>

# Remove a container (image will be kept)
docker rm -f <container name>

# List installed images
docker images

# Remove an image
docker rmi -f <image name>

# Clean up volumes (useful for when no space is left)
docker volume rm $(docker volume ls -qf dangling=true)

# List dangling volumes:
docker volume ls -qf dangling=true

# List all volumes:
docker volume ls

# Remove <none> (unused) images
docker rmi -f $(docker images | grep "^<none>" | awk "{print $3}")

# List all space ocupied by images/containers/volumes
docker system df -v

Refs