image names are different from docker-compose build - docker-compose

for example, my project directory is
project/
- docker-compose.yml
- Dockerfile
docker-compose.yml is
services:
app1:
build:
context: .
target: development
dockerfile: Dockerfile
so if i run
docker-compose -f docker-compose.yml build app1
will generate an image project_app, but if i run
docker-compose build
the image name will be project-app.
my question is why is this happening and if there is any way to persist the name without changing docker-compose.yml?
docker-compose version 1.29.2

Related

How to use the env variable of the image to set another env variable in docker compose?

Following is my docker-compose.yml file
version: "3.7"
services:
test-build:
image: docker-hardened-ol8-openjdk17
command: tail -f /dev/null
restart: always
volumes:
- "C:/checkouts:/opt/checkouts"
ports:
- 9001:9001
environment:
- JAVA_17_HOME=${JAVA_HOME:?err}
The docker-hardened-ol8-openjdk17 image has Java 17 and the JAVA_HOME environment variable. I need to set the JAVA_17_HOME environment variable to the same as JAVA_HOME from the image. But when I run docker compose up, it takes the JAVA_HOME value set in my machine (host machine).
I read the https://docs.docker.com/compose/environment-variables/ and https://docs.docker.com/compose/reference/envvars/ pages. Even these pages mention that -
Compose uses the variable values from the shell environment in which docker-compose is run.
Is there a way I can specify docker-compose to use the image's environment variable instead of the host machine's?
I could work around this using a Dockerfile along with the docker-compose.yml config file in the same directory. I moved the image part to the Dockerfile, and declared all the environment variables in the Dockerfile.
docker-compose.yml -
version: "3.7"
services:
test-build:
build:
context: .
command: tail -f /dev/null
restart: always
volumes:
- "C:/checkouts:/opt/checkouts"
ports:
- 9001:9001
Dockerfile -
FROM docker-hardened-ol8-openjdk17
ENV JAVA_17_HOME=$JAVA_HOME
ENV BUILD_HOME=/opt/checkouts/
WORKDIR $BUILD_HOME

Docker compose dind deploy to GitLab Registry

I am working on MS Blazor server two projects solution (Core 6) and GitLab.
docker-compose.yml file:
version: '3.4'
services:
admin:
image: ${DOCKER_REGISTRY-}admin
build:
context: .
dockerfile: FrontEnd/Admin/Dockerfile
enrollment:
image: ${DOCKER_REGISTRY-}enrollment
build:
context: .
dockerfile: FrontEnd/Enrollment/Dockerfile
GitLab pipeline file:
image: docker:stable
services:
- docker:dind
before_script:
- docker info
build:
only:
- DockerComposeSupported
before_script:
- docker login registry.gitlab.com -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD}
script:
- apk add --no-cache docker-compose
- docker-compose build #Works fine
- docker push ${CI_REGISTRY}/${CI_PROJECT_PATH} # Here is the error
after_script:
- docker logout ${CI_REGISTRY}
stage: build
The problem is in line: - docker push ${CI_REGISTRY}/${CI_PROJECT_PATH}, the error is
The push refers to repository [registry.gitlab.com/xxx/yyy/zzz]
An image does not exist locally with the tag: registry.gitlab.com/xxx/yyy/zzz
I originally grab the code from Gitlab, it was
docker build -t registry.gitlab.com/xxx/yyy/zzz .
But the docker build is looking for a docker file in the root directory and I do not have one, I have docker-compose only so I need to use the "docker-compose build" but this command does not have any directory parameter.
Any help will be greatly appreciated.
Use a different variable defined by GitLab in CI: CI_REGISTRY_IMAGE="registry.example.com/org/repo"
version: '3.4'
services:
admin:
image: ${CI_REGISTRY_IMAGE}-admin
build:
context: .
dockerfile: FrontEnd/Admin/Dockerfile
enrollment:
image: ${CI_REGISTRY_IMAGE}-enrollment
build:
context: .
dockerfile: FrontEnd/Enrollment/Dockerfile
docker-compose has a nifty feature where you can set this variable in a .env file within the same directory as the docker-compose.yml file. You will only need this file for local development.
# .env
CI_REGISTRY_IMAGE="registry.example.com/org/repo"
GitLab Docs: Predefined variables
Docker Docs: The .env file
Thanks, Richard and Danielnelz! I've found a solution for the "requested access to the resource is denied" error. I had "{CI_REGISTRY_IMAGE}-admin" project reference but it should be {CI_REGISTRY_IMAGE}/admin. Now the docker push to the GitLab registry is working fine.

Docker-Compose cannot find config env file

I've created image from my Go project that has config env file. Here's my script for docker file
FROM alpine AS base
RUN apk add --no-cache curl wget
FROM golang:1.15 AS go-builder
WORKDIR /go/app
COPY . /go/app
RUN GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /go/app/main ./main.go
FROM base
COPY --from=go-builder /go/app/main /main
CMD ["/main"]
Also i create docker-compose file to connect with postgresql like this:
version: "3.7"
services:
postgres:
container_name: postgres
image: postgres
ports:
- 5432:5432
networks:
- go_network
app-golang:
container_name: app1
image: app-go:1.0
ports:
- 8000:8000
depends_on:
- postgres
networks:
- go_network
env_file:
- /env/config
networks:
go_network:
name: go_network
My env file is in config file format, and as seen above, i store it on /env/config. The problem is when i do docker-compose up -d the log is said cannot find /env/config like this ERROR: Couldn't find env file: /env/config Is it has different function to read from config format file?
EDIT 1:
my env file is in config format as shown below, and I'm using "github.com/kenshaw/envcfg" package to read config file using envcfg:

Run specific spec files on cypress docker-compose setup

I have a docker-compose file to run cypress tests, but I see that it is identifying all the spec files in the integration folder and running the tests. I wanted to run a subset of the spec files . for example: only one specific spec file.
I tried with command: and the cypress run the specific file which did not help. Is there any way to run a specific spec file with the docker-compose setup.
version: '3.2'
services:
cypress:
image: 'cypress/included:6.6.0'
environment:
- CYPRESS_environment=test
working_dir: /test
volumes:
- ./:/test
If you look at the Dockerfile it uses entrypoint, so you can use a command parameter in your compose file to run specific file
version: '3.2'
services:
cypress:
image: 'cypress/included:6.6.0'
environment:
- CYPRESS_environment=test
working_dir: /test
volumes:
- ./:/test
command: "--spec /test/integration/mytest.js"

Changing Environment in docker-compose up

I'm new to docker.
Here is my simple docker-compose file.
version: '3.4'
services:
web:
image: 'myimage:latest'
build: .
ports:
- "5265:5265"
environment:
- NODE_ENV=production
To run this, I usually use docker-compose up command.
Can I change the NODE_ENV variable to anything while running docker-compose up?
For example:
docker-compose up -x NODE_ENV=staging
Use docker-compose run, you can manage services but not the complete stack. Useful for one-off commands.
$ docker-compose run -d -e NODE_ENV=staging web
Ref - https://docs.docker.com/compose/reference/run/
OR
Best way i could see as if now is to use shell & export the environment variable before doing a docker-compose up as below -
$ export NODE_ENV=staging && docker-compose up -d
Where your docker-compose will look something as below -
version: '3.4'
services:
web:
image: 'myimage:latest'
build: .
ports:
- "5265:5265"
environment:
- NODE_ENV=${NODE_ENV}