hercules-ci / arion

Run docker-compose with help from Nix/NixOS
Apache License 2.0
631 stars 47 forks source link

How to use a nix-managed docker image in arion #169

Open cameron1024 opened 1 year ago

cameron1024 commented 1 year ago

I'm quite new to Nix, so this might just be my inability to read the docs properly, apologies in advance.

I have a flake.nix that defines an output which is a docker image built with pkgs.dockerTools.buildImage. I'd like to deploy this using arion. Using docker-compose, I'd write something like this as my docker-compose.yml:

version: "3.9"
services:
  web:
    depends_on: ["db"]
    build: .
    ports:
      - "8000:8000"
  db:
    image: "postgres"

Currently, my best attempt at a arion-compose.yml is the following:

{
  services.db = {
      service.image = "postgres";
      service.volumes = [ "${toString ./.}/postgres-data:/var/lib/postgresql/data" ];
      service.environment.POSTGRES_PASSWORD = "password";
  };

  services.backend = {
    service.depends_on = [ "db" ];
    service.build = "???";
  };
}

I'm not sure how to get the image built inside my flake into the backend service. Any advice is much appreciated :grin:

roberth commented 1 year ago

You might get away with using this option with lib.mkForce. Maybe it shouldn't be internal.

https://github.com/hercules-ci/arion/blob/8159c4faa3502a571cf8b9347f4d4b49f35c4c9f/src/nix/modules/service/image.nix#L76-L82

It'd look like this:

  services.backend = {
    service.depends_on = [ "db" ];
    build.image = lib.mkForce (pkgs.dockerTools.streamLayeredImage { ..... });
        # or buildLayeredImage, or buildImage (though I haven't tried buildImage)
  };
aciceri commented 1 year ago

I confirm this works with buildImage :) I agree this shouldn't be internal.

olebedev commented 1 year ago

This is a very useful feature, is that possible we can make it a part of the API?

hussein-aitlahcen commented 1 year ago

This is a pretty useful feature! Making it public API would be neat!

matt1432 commented 8 months ago

This also works with dockerTools.pullImage. +1 on making this public API