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

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.

Related

Using secrets with docker is fine but fails for docker-compose

I have a Dockerfile that uses secrets and I can successfully build the image using docker build. However, when I try to build the same image using docker-compose build I get the error:
ERROR: Dockerfile parse error line 4: Unknown flag: mount
This occurs on Ubuntu 20.04LTS (Docker version 18.09.6, build 481bc77,
docker-compose version 1.20.0-rc2, build 8c4af54).
On RHEL 7.9 (Docker version 20.10.7 build f0df350, docker-compose version 1.29.2, build 5becea4c) a different error occurs:
[2/2] RUN --mount=type=secret,id=the_secret cat /run/secrets/the_secret:
#8 0.466 cat: /run/secrets/the_secret: No such file or directory
How can I use docker-compose to build my images that involve secrets?
Build using docker (works)
#!/bin/bash
export COMPOSE_DOCKER_CLI_BUILD=1
export DOCKER_BUILDKIT=1
echo "I have a secret" > secret.txt
docker build --secret id=the_secret,src=./secret.txt .
build using docker-compose (fails)
export COMPOSE_DOCKER_CLI_BUILD=1
export DOCKER_BUILDKIT=1
echo "I have a secret" > secret.txt
docker-compose build --no-cache test
Dockerfile
# syntax=docker/dockerfile:1.2
FROM python:3.8
RUN --mount=type=secret,id=the_secret cat /run/secrets/the_secret
docker-compose.yml
version: "3.6"
services:
test:
build: .
secrets:
- the_secret
secrets:
the_secret:
file: secret.txt

How to build specific service using docker-compose?

How do I target my build using docker-compose to one of the services I have in docker-compose.yml?
My docker-compose.yml for example:
version: '3'
services:
admin:
image: 8205037.dkr.ecr.us-east-2.amazonaws.com/admin:latest
build:
context: ../
dockerfile: ./.docker/Dockerfile-admin
www:
image: 233232037.dkr.ecr.us-east-2.amazonaws.com/www:latest
build:
context: ../
dockerfile: ./Dockerfile-www
And how to build only the www without change the file? I want to run something like this:
docker-compose -f .docker/docker-compose.yml build --target www
Just remove the target :)
docker-compose -f .docker/docker-compose.yml build www
docker-compose -f **(path docker-compose file)** build **(container name)**
If it's the first time.
docker-compose -f **(path docker-compose file)** up -d **(container name)**

Tagging image/container with docker compose

I am trying to build an image/container with docker compose. The container builds/runs successfully, but the image REPOSITORY and TAG both appear as <none> in the output for docker images and the container gets an auto-generated name (e.g. eloquent_wiles). I would for it to tag the image/container with the names specified in my config files (in this case I would like them to be named 'myservice' and the image to be tagged 'v2').
I have the following docker-compose.yml:
version: '3'
services:
myservice:
build: .
image: myservice:v2
container_name: myservice
ports:
- "1337:1337"
This is my Dockerfile:
FROM node:10
WORKDIR /usr/src/myservice
COPY . /usr/src/myservice
EXPOSE 1337/tcp
RUN yarn \
&& yarn transpile \
&& node ./build/grpc-server.js
docker -v gives Docker version 18.09.2, build 6247962
docker-compose -v gives docker-compose version 1.22.0, build f46880fe
And I am running docker-compose build. I get the same results using docker-compose version 2.
I don't suppose anyone can spot what I'm doing wrong?
Build a named image: docker build -t <repo>:<tag> . in the directory where the Dockerfile is.
Deploy a named service: docker stack deploy -c <your_yaml_file> <your_stack> --with-registry-auth in the directory where your YAML is.
That was very silly of me. The issue was with the last line of the Dockerfile where you can see I start the server as part of the build process instead of as an entrypoint, blocking the build from reaching the step where it tags the images.
Thanks to #Mihai for pointing out that the <none>:<none> image is an intermediate and not the result of my build.

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
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

Docker build "no such file or directory" but work with docker-compose

I try to register with gitlab a image:
docker build -t registry.gitlab.com/xxx/xxx compose/base
But I get:
Step 4/7 : COPY ./requirements /requirements lstat requirements: no
such file or directory
However build with compose work:
docker-compose -f dev.yml build python
services:
python:
build:
context: .
dockerfile: ./compose/base/Dockerfile
The file structure:
-Project/requirements
-Project/compose/base
How I replicate what compose do?
Your problem is in docker build context, i suggest you try this:
docker build -t registry.gitlab.com/xxx/xxx -f compose/base/Dockerfile .

Resources