Clean up Docker

For any project I work on these days I use Docker. At the very least I use Docker for local development but whenever possible I try to use Docker in production. I prefer using the tiny PaaS named Dokku as it is very easy to set up and use (especially compared to Kubernetes).

I have run into disk space issues with using Docker/Dokku. Sometimes Dokku doesn’t clean up old images and containers. This seems to happen only during a deployment when the build fails.

The following command takes care of that.

docker system prune --volumes

This removes all stopped containers, all unused networks, all dangling images, the build cache and with the –volumes option, all unused volumes.

I run this command occasionally on my Dokku servers and also on my local machine.

It’s amazing to see how much space docker can eat up if you are not actively monitoring it.

More information available in Docker’s official documentation.

Prune Everything

Here’s a link to Dokku if you are interested in trying “The smallest PaaS implementation you’ve ever seen”.

Dokku

Docker Compose

A docker-compose.yml template that I use for development

version: "3"
services:
  api:
    build:
      context: ./api
      dockerfile: Dockerfile.dev
    container_name: ${APP_NAME}-api
    ports:
    - 8402:443
    environment:
      APP_BASE_URL: https://${APP_HOST}:8402
      APP_ENV: development

      DB_NAME:
      DB_USER:
      DB_PASS:
      DB_HOST: db

      SESSION_COOKIE_DOMAIN:
      SESSION_NAME:
      SESSION_EXPIRY:

      #dev only
      PHP_IDE_CONFIG: serverName=${APP_HOST}
      XDEBUG_CONFIG: "remote_host=host.docker.internal idekey=${APP_HOST}"
      XDEBUG_IDEKEY: ${APP_HOST}

    volumes:
    - ./api:/var/www/

  db:
    image: mysql:8.0.13
    container_name: ${APP_NAME}-db
    ports:
    - 8404:3306
    volumes:
    - ${APP_NAME}-db-data:/var/lib/mysql
    - ./db/dump.sql:/docker-entrypoint-initdb.d/dump.sql
    - ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf
    environment:
      MYSQL_ROOT_PASSWORD:
      MYSQL_DATABASE:
      MYSQL_USER:
      MYSQL_PASSWORD:

volumes:
  db-data:
    name: ${APP_NAME}-db-data

A .env file is also required.

# for dev environment only
APP_HOST=project-name.local.example.dev
APP_NAME=project-name