How to pass the right context to docker-compose build? - docker

I have the following docker-compose file:
version: "3.9"
services:
repo_svc:
build:
context: ../
dockerfile: ../build/Dockerfile
Running the command, I have got the following error message:
docker-compose -f ./deployments/docker-compose.yml build
Building repo_svc
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /home/developer/versioner/services/build: no such file or directory
ERROR: Service 'repo_svc' failed to build : Build failed
The folder structure of GO project:
I would like to pass to context: ../ the repo-svc folder context. However, I do not know why it is looking in the /home/developer/versioner/services/build folder. It even does not exist.

I think the context should look like:
version: "3.9"
services:
repo_svc:
build:
context: ../build
dockerfile: Dockerfile

Related

Running docker-compose up ends up with error "Service has neither an image nor a build context specified."

My microservices project structure is like this:
my-service-one/
- Dockerfile
- ...
my-service-two/
- Dockerfile
- ...
docker-compose.yml
As you can see, each service directory contains a Dockerfile. There is a docker-compose.yml in the root level.
The docker-compose.yml :
version: "3"
services:
service-one:
container_name: service-one
build:
dockerfile: ./my-service-one/Dockerfile
ports:
- "8081:8081"
service-two:
container_name: service-two
build:
dockerfile: ./my-service-two/Dockerfile
ports:
- "8082:8082"
Now, I run docker-compose up -d from the root. I end up with error:
$ docker-compose up -d
ERROR: The Compose file is invalid because:
Service service-one has neither an image nor a build context specified. At least one must be provided.
My question is why does docker-compose think my service-one doesn't have a build context specified? Didn't I specify it already with:
build:
dockerfile: ./my-service-one/Dockerfile
Why this error?
why does docker-compose think my service-one doesn't have a build context specified?
Weeeell, because you did not specified the build context.
Didn't I specify it already with:
No, you specified the dockerfile. No the context.
Why this error?
You have to specify the context so that docker knows what to build.
If you want to build with the context of current directory, you would do:
build:
context: .
dockerfile: ./my-service-two/Dockerfile
Maybe the context is inside my-service-two, I suspect youw antto write:
build:
context: ./my-service-two
dockerfile: ./Dockerfile
or really just:
build: ./my-service-two
Peovide a context property below both services in build section like that:
build:
context: YOUR_DIRECTORY
dockerfile: ./my-service-one/Dockerfile
YOUR_DIRECTORY is the place where the files for your project are listed.
Most probably YOUR_DIRECTORY is already written i the child .yml files.
You have a couple of main approaches:
To copy paste the context from the child .yml
To produce the docker build using the child .yml with a command like:
docker-compose -f docker-compose.yml -f docker-compose-dev.yml up
--build

Can't create a docker image for COPY failed: stat /var/lib/docker/tmp/docker-builderXXXXXX error

I'm not able to build my image. Below is the line that I feel as the challenge. Please have a look and suggest me if I have done anything wrong. I'm trying to copy a zip file that is present in the current folder as the Dockerfile and docker-compose file. I don't have a .dockerignore file also. I'm declaring the file name as ENV and passing that value in docker-compose file.
COPY ${FILE_NAME}.zip /app/
My docker-compose file is like this.
version: '3.7'
services:
tws:
build:
context: .
dockerfile: Dockerfile
ports:
- "8085:8085"
environment:
- FILE_NAME=xxxxx
Below is the error that come during docker-compose up
ERROR: Service 'yyy' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder467027701/.zip: no such file or directory
EDIT: It is working fine if I provide the actual name instead of ENV in the Dockerfile
I think that what you need is a build argument because you perform the COPY task at the build time and not at the container start time.
Remove that part :
environment:
- FILE_NAME=xxxxx
And update your compose such as :
build:
args:
- FILE_NAME=xxxxx
And declare the ARG in the Dockerfile :
FROM ...
ARG FILE_NAME
...
COPY ${FILE_NAME}.zip /app/

DOCKER_REGISTRY value is not set build error

I'm doing some experimenting with .NET Core 2.1, React/Redux, and Docker. Without making any changes to the boilerplate ASP.NET Core with React code generated, I added Docker support to the project.
Now the project doesn't build!
When I attempt to build, I get the error
DOCKER_REGISTRY value is not set build error.
Attempting to fix this, I tried changing my docker-compose file to be like this:
version: '3.4'
services:
project:
image: ${DOCKER_REGISTRY}project
build:
context: .
dockerfile: Project\Dockerfile
environment:
DOCKER_REGISTRY = "docker.io"
But, this didn't work. I assume I'm missing something pretty straightforward.
Thanks in advance!
UPDATE:
I got a little bit further by thinking that I don't have an image saved in docker.io, so why should I be trying to pull one down? I changed my docker-compose file to look like this:
version: '3.4'
services:
project:
build:
context: .
dockerfile: project\Dockerfile
And now the project will at least build - but it won't debug, because I now get an error on the second step of 7:
Service 'project' failed to build: COPY failed: CreateFile \\?\C:\ProgramData\Docker\tmp\docker-builder965605424\src: The system cannot find the file specified..
This is an issue with Docker Compose version 1.21.2 https://github.com/docker/compose/issues/5969 so you could either switch to the stable channel or update your docker-compose.yml to look like the following one:
version: '3.4'
services:
project:
image: ${DOCKER_REGISTRY}project
build:
context: Project
dockerfile: ./Dockerfile
Try it like this (change your docker-compose to pass build args rather then setting environment variables).
version: '3.4'
services:
project:
image: ${DOCKER_REGISTRY}/project
build:
context: .
dockerfile: Project\Dockerfile
agrs:
- DOCKER_REGISTRY = "docker.io"

Dockerfile pass environments on docker compose build

I have written a Dockerfile which uses two arguments:
FROM jessie
MAINTAINER Zeinab Abbasimazar
#Build Arguments
ARG REP_USER
ARG REP_PASS
# Build
RUN echo 'REP_USER:'$REP_USER', REP_PASS:'$REP_PASS
I wrote a docker-compose.yml for build:
version: "2"
services:
ui:
build:
context: .
dockerfile: Dockerfile
args:
REP_USER: $REP_USER
REP_PASS: $REP_PASS
I don't want to define these arguments directly in the compose file, so I tried to send them during docker compose build:
REP_USER=myusername REP_PASS=mypassword docker-compose build
Which didn't work. I changed my Dockerfile to use these arguments as environment variables; so I removed ARG lines:
FROM jessie
MAINTAINER Zeinab Abbasimazar
# Build
RUN echo 'REP_USER:'$REP_USER', REP_PASS:'$REP_PASS
And docker-compose.yml:
version: "2"
services:
ui:
build:
context: .
dockerfile: Dockerfile
And ran REP_USER=myusername REP_PASS=mypassword docker-compose build; still no result.
I also tried to save these information into an env file:
version: "2"
services:
ui:
build:
context: .
dockerfile: Dockerfile
env_file:
- myenv.env
But it seems env files doesn't affect at build time; they are just take part into run time.
EDIT 1:
Docker version is 1.12.6 which doesn't support passing arguments with --build-arg.
EDIT 2:
I tried using .env file as described here:
cat .env
REP_USER=myusername
REP_PASS=mypassword
I then called docker-compose config which returned:
networks: {}
services:
ui:
build:
args:
REP_PASS: mypassword
REP_USER: myusername
context: /home/zeinab/Workspace/ZiZi-Docker/Test/test-exec-1
dockerfile: Dockerfile
version: '2.0'
volumes: {}
Which means this resolved my issue.
EDIT 3:
I also tried third section of docker-compose arg documentation in my docker-compose.yml file:
version: "2"
services:
ui:
build:
context: .
dockerfile: Dockerfile
args:
- REP_USER
- REP_PASS
And executed:
export REP_USER=myusername;export REP_PASS=mypassword;sudo docker-compose build --no-cache
Still not getting what I wanted.
You can set build arguments with docker compose as described here:
docker-compose build [--build-arg key=val...]
docker-compose build --build-arg REP_USER=myusername --build-arg REP_PASS=mypassword
Btw, AFAIK build arguments are a compromise between usability and deterministic building. Docker aims to build in a deterministic fashion. That is, wherever you execute the build the produced image should be the same. Therefore, it appears logical that the client ignores the environment (variables) it is executed in.
The correct syntax for variable substitution in a docker-compose file is ${VARNAME}.
Try with this one:
version: "2"
services:
ui:
build:
context: .
dockerfile: Dockerfile
args:
REP_USER: ${REP_USER}
REP_PASS: ${REP_PASS}
I finally found the solution. I mentioned it in the question too. I first tried it with fail, then I found out that I had a typo naming .env file; it was .evn.
I tried using .env file as described here:
cat .env
REP_USER=myusername
REP_PASS=mypassword
I then called docker-compose config which returned:
networks: {}
services:
ui:
build:
args:
REP_PASS: mypassword
REP_USER: myusername
context: /home/zeinab/Workspace/ZiZi-Docker/Test/test-exec-1
dockerfile: Dockerfile
version: '2.0'
volumes: {}
Which means this resolved my issue. I should mention that this answer was really helpful.

docker-compose build error when using parent dir & dockerfile for build

I have the following structure:
.
..
docker/cli/Dockerfile
tests/docker-compose.yml
docker-compose.yml
In my tests/docker-compose.yml I have the following service defined:
services:
test:
build:
context: ../
dockerfile: ../docker/cli/Dockerfile
...
When do a docker-compose build I get:
Building test
ERROR: Forbidden path outside the build context: ../docker/cli ()
After I wrote the question I actually found the problem. When you define a context in docker-compose, the dockerfile: bit is relative to that context. So the proper configuration would be:
test:
build:
context: ../
dockerfile: docker/cli/Dockerfile
I hope this helps someone...

Resources