Build docker image with build arguments

How can I use multiple containers of OpenDJ with different build arguments?

I want something like this

# .circleci/config.yml
jobs:
  build:
    docker:
      - image: circleci/jruby:9.1.17-browsers
        environment:
          RAILS_ENV: test
          RACK_ENV: test

      - image: openidentityplatform/opendj:4.2.4
        name: dir1_test
        args:
          BASE_DN: "dc=example1,dc=com"

      - image: openidentityplatform/opendj:4.2.4
        name: dir2_test
        args:
          BASE_DN: "dc=example2,dc=com"

I am able to accomplish this locally with a docker-compose.yml like the following.

# docker-compose.yml
version: '3.3'
services:
  dir1_test:
    build:
      image: openidentityplatform/opendj:4.2.4
      args:
        BASE_DN: "dc=example1,dc=com"
    dir2_test:
    build:
      image: openidentityplatform/opendj:4.2.4
      args:
        BASE_DN: "dc=example2,dc=com"

HI @HarlemSquirrel,

Per docker docs,

The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build

Docker images defined in your example ( i.e. openidentityplatform/opendj:4.2.4) have already been built so there is no build command invoked, and therefore no way to alter build-time behavior.

Depending on how OpenDJ containers are designed, providing a variable under environment might override the runtime behavior, but that would depend on OpenDJ’s implementation.

If the container does not respect an environment variable at runtime, then your other options include:

  • Build (with args) and publish the images yourself to a public or private registry to be used at runtime, i.e. openidentityplatform/opendj:4.2.4-dn1 and openidentityplatform/opendj:4.2.4-dn2
  • Invoke docker build within the job itself to first build and then run (you could use your provided docker compose). You would need to choose a machine executor or setup_remote_docker, noting the networking constraints with the latter.

I hope that speaks to your question!

3 Likes

I’d also suggest that if this project is open source and under active maintenance, you could approach the project and ask for env settings to be added.

1 Like

OpenDJ is open source and I recently changed the way the container works from ENV to ARG because ENV variables were used in the build process and so passing them in during docker run had no affect.

The Dockerfile is quite simple and you can see that we need to set the base_dn when we run /opt/opendj/setup to initialize the server. I don’t think we could do this with ENV vars at runtime.

Thanks for the update @HarlemSquirrel. However, I am not quite sure if your problem is resolved?

How are you passing them? Passing them via docker run --env x=y should work fine (assuming you want them at run time not build time).

@halfer In this case I need build arguments. I’ve resolved to building the containers locally and pushing them up to Docker Hub.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.