docker-compose args from shell - docker

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

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

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}

add ARGS in your Dockerfile like
ARGS RSA
For it to be read upon build

Related

Docker compose build args are not passed to dockerfile

I have a docker compose file that builds and runs a couple of dockerfiles locally. I am using multi-stage builds and I pass an argument into my docker build commands. The following works perfectly:
docker build -t local/admin-api --build-arg SONAR_TOKEN=XXXXXXXXXXXXXXX .
I am trying to replicate that in my docker compose file with:
services:
comply:
build:
context: ./websites/comply/
dockerfile: Dockerfile
args:
SONAR_TOKEN: xxxxxxxxxxx
ports:
- "8080:80"
In my dockerfile I use:
FROM kinectify.azurecr.io/buildbase:ci-631 as build
ARG PAT
ARG SONAR_PROJECT_KEY=xxxxxxx
ARG SONAR_ORGANIZATION_KEY=xxxxxxx
ARG SONAR_HOST_URL=https://sonarcloud.io
ARG SONAR_TOKEN
RUN echo "Token: ${SONAR_TOKEN} | hostURL: ${SONAR_HOST_URL}"
I am aware of the scoping of build args, and as you can see above, the arg is defined after my FROM statement. I have also tried setting the arg to an environment variable, but that doesn't seem to matter. Additionally, the SONAR_HOST_URL is being output correctly in echo statement (and in the place where I am actually using the argument). I am running the build with:
docker compose build comply
** UPDATE **
Strangely, when I run docker compose up -d --build it does pass variable. I am not sure what the difference is though.
This is a known issue that's been fixed in docker compose. You are likely waiting for the next release still. Using docker-compose instead of docker compose should also work while you wait for that release.

docker-compose build with url context and local Dockerfile

The docker build context is defined as either a path to a directory containing a Dockerfile, or a url to a git repository.
With the docker command, it's possible to build an image using
a local context and a local Dockerfile:
docker build -t foobar [-f Dockerfile.local] .
a remote context and a remote Dockerfile:
docker build -t foobar https://github.com/foobar/foobar [-f Dockerfile.remote]
a remote context and a local Dockerfile
docker build -t foobar https://github.com/foobar/foobar -f- < Dockerfile.local
The first two options easily translate to docker-compose:
services:
first:
build:
context: .
dockerfile: Dockerfile.local
second:
build:
context: https://github.com/foobar/foobar
dockerfile: Dockerfile.remote
The official documentation says nothing about injecting a local Dockerfile to a remote context with docker-compose. Is it possible?

Docker compose on ecr: ERROR: No such service: --build-arg

I try to pass arg into the docker build process in this command:
docker-compose -f .docker/docker-compose.ecr.yml build my-app --build-arg BUILD_VERSION=5.0.0
But I got error:
ERROR: No such service: --build-arg
According to the docs docker-compose have build-arg parameter.
The yml file:
version: '3'
services:
my-app:
image: 3144.dkr.ecr.us-east-2.amazonaws.com/my-app:latest
build:
context: ../
dockerfile: ./.docker/Dockerfile
What can be the problem?
The services should be last in the command line.
$ docker-compose build --help
Usage: build [options] [--build-arg key=val...] [SERVICE...]
So:
$ docker-compose -f .docker/docker-compose.ecr.yml build --build-arg BUILD_VERSION=5.0.0 my-app
I had a similar issue but it was Visual Studio 2019 that was trying to publish and putting the build my-app before the 'build-arg' parameter.
All I had to do is to update VS 2019 to the latest version 16.11.0 that solved this error during the publish from the IDE.

Pass argument to Dockerfile from a file with docker-compose (SSH private key)

Hi!
I'm kinda stuck in docker-compose, as I need to pass my private SSH key to my Dockerfile declared in my docker-compose.yml, as below:
docker-compose.yml
version: '3.7'
services:
worker:
build: .
args:
- SSH_PRIVATE_KEY
Dockerfile
ARG SSH_PRIVATE_KEY
RUN mkdir /root/.ssh/ && \
echo "${SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa && \
chmod 600 /root/.ssh/id_rsa
With docker itself, that's quite easy, as I just need to run the following command:
docker build . --build-arg SSH_PRIVATE_KEY="$(cat ~/.ssh/id_rsa)"
But in docker-compose... The problem of the ARGS configuration in docker-compose as described in another question is that I can't let the private key inside the docker-compose.yml file.
I need to let docker-compose access to the key inside ~/.ssh/id_rsa: Any clue on how to perform that?
Thank you!
The docs on args states that :
You can omit the value when specifying a build argument, in which case its value at build time is the value in the environment where Compose is running.
In your case, you probably want to build worker service with the following command :
SSH_PRIVATE_KEY="$(cat ~/.ssh/id_rsa)" docker-compose build
By the way, your docker-compose.yml is wrong (missing context) and should be :
version: '3.7'
services:
worker:
build:
context: .
args:
- SSH_PRIVATE_KEY

How to pass build arguments to docker build command?

I have a command like this:
docker build -f ${DOCKERFILE} ${BUILD_ARGS} -t ${IMAGE} ${BUILD_CONTEXT}
When I pass these environment variables it works:
IMAGE_SUBNAME: 'frontend'
DOCKERFILE: ./frontend/Dockerfile
BUILD_CONTEXT: ./frontend
But when I try to pass these, it throw me error (see below):
IMAGE_SUBNAME: 'frontend'
DOCKERFILE: ./frontend/Dockerfile
BUILD_CONTEXT: ./frontend
BUILD_ARGS: ENV=${CI_COMMIT_REF_SLUG} // CI_COMMIT_REF_SLUG equals to "dev"
"docker build" requires exactly 1 argument.
I have already read a lot of articles, but I can't find my mistake. Hope to get help.

Resources