Docker Compose All the Things

After getting the GPD Win Max 2 and using it as my everyday machine both for work and personal use, I’ve started following the philosophy of not installing any project dependencies locally and instead leveraging containerization technology (mostly via Docker). I’ve talked about this in a previous post, and you can probably think of this post as an improvement on top of that.

I’ve been successfully using this pattern for both work projects and personal ones, and I thought I’d share it.

This particular blog for example is using this configuration:

version: '3.2'
services:
  default: &defaults
    image: klakegg/hugo:0.48
    volumes:
      - ./:/src
    working_dir: /src

  hugo:
    <<: *defaults
    profiles: ["dev"]
    entrypoint: hugo

  app:
    <<: *defaults
    ports:
      - "1313:1313"
    command: server -D --bind=0.0.0.0
    tty: true

This is then augmented by a bash alias:

alias dcr='docker compose run --rm -u 1000:1000

I would usually have at least two services:

  1. the first one would be named the same as the primary executable (in this case hugo but in JS projects it can be yarn or in Rails projects, rails) and instead of a command, I use an entrypoint. This allows me to do something like dcr yarn add @faker-js/faker for example which keeps the semantics of the binary, and all I need to remember is to prefix the command with dcr (short for docker compose run).
  2. the second service would usually be called app and it is what I would like to run when I do docker compose up

Note that recent versions of the docker compose configuration support the service key profiles, so I put the first service in that profile. That way, when I do docker compose up, it only brings up the default services (the ones that don’t have their profiles set) and doesn’t try to bring up services I only expect to run as one-off commands.

With this, I haven’t had to install any dependencies locally in my projects, and migrating to a different machine is much easier. The only dependency is Docker, so as long as it’s installed, I can continue working on a project without having to remember how to install its dependencies.

Comments

comments powered by Disqus