Building docker image with extra hosts

Hi,
I am trying to build a docker image for my application and then run it through docker-compose. I added some hostname mappings that only work on certain private network into the docker-compose file but I still cannot get those hostname be reachable.

I guess it is because extra_hosts modifies /etc/hosts in the container but seems that we have to update networking.extraHosts in nixos config.

This is the nix file that I used to build the image:

dockerTools.buildImage {
    name = "myapp";
    contents = [ myapp cacert ];
    tag = tag;
    created = "now";
    config = {
        Cmd = [ "${myapp}/bin/Main" ];
        WorkingDir = "/app";
        ExposedPorts = {
            "8080/tcp" = {};
        };
    };
}

And this is my docker-compose.yml:

version: '3'

services:
  myapp:
    image: myapp
    restart: always
    extra_hosts:
      - "host.a.com:1.2.3.4"
      - "host.b.com:1.2.3.4"
    ports:
      - 8080:8080
 depends_on: 
     - "database"

I guess the root cause of this issue is trying to run the image on a host system that nix has not been installed, so I have updated the nix file:

dockerTools.buildImage {
    fromImage = dockerTools.pullImage {
        imageName = "lnl7/nix";
        finalImageTag = "2.3.7";
        imageDigest = "sha256:a9e3b037f4a582b195c9c8830ab63769d7c6bd1b3283834970017b2dd0028d77";
        sha256 = "0iwiprr351irx0lmvngbvwnyl5njviz4cq1a8mkd7203d6v4hmjh";
    };
    name = "myapp";
    contents = [ myapp cacert ];
    tag = tag;
    created = "now";
    config = {
        Cmd = [ "${myapp}/bin/Main" ];
        WorkingDir = "/app";
        ExposedPorts = {
            "8080/tcp" = {};
        };
    };
}

And now the extras_hosts works properly although I am not sure is that the right way to fix this issue.

Yeah I will try using arion once I have setup nix on the host system. Thanks for the suggestion!

1 Like