Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose args from shell

I want to build via the docker-compose an image that uses my private key for cloning private git repos.

More or less the compose becomes as follows:

   myservice:
     build:
      context: .
      args:
        RSA:  ~/.ssh/id_rsa

The above does not work, neither the following:

   myservice:
     build:
      context: .
      args:
        RSA: $(cat ~/.ssh/id_rsa)

The docker build command works just fine however in the form of

docker build --build-args RSA=$(cat ~/.ssh/id_rsa) -t myservice:latest
like image 592
pkaramol Avatar asked Feb 05 '23 03:02

pkaramol


2 Answers

You can use the same syntax with docker-compose build:

docker-compose build --build-arg RSA="$(cat ~/.ssh/id_rsa)"

Unfortunately, you can't use the build-args option with compose up or start... So you will need to build and then run using the --no-build option

like image 109
yamenk Avatar answered Mar 05 '23 19:03

yamenk


One way to do it that will work when building all the services and also with up is to pass the SSH key data as an environnement variable like this:

env RSA=$(cat ~/.ssh/id_rsa) docker-compose build

And then you set the build args in the compose file like this:

myservice:
 build:
  context: .
  args:
    RSA: |-
      ${RSA}
like image 21
Etienne Avatar answered Mar 05 '23 19:03

Etienne