build Dockerfile inside docker-compose.yml which is in another path - spring-boot

i have springboot application, which is created via initializr and i created Dockerfile like following
FROM adoptopenjdk/openjdk11:alpine-jre
ARG JAR_FILE=target/tender-api.jar
WORKDIR /opt/app
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","app.jar"]
i want to deploy in my ubuntu server. Because it i created in another path docker-compose.yml
version: '3.8'
services:
appDB:
image: postgres:14.1-alpine
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- '5432:5432'
volumes:
- db:/var/lib/postgresql/data
tender:
image: tender-api:1
build:
context: .
dockerfile: /root/dev/tender-api/Dockerfile
volumes:
- /data/tender-api
ports:
- "8080:8080"
depends_on:
- appDB
volumes:
db:
driver: local
And verbose logs are like following
#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 32B done
#1 DONE 0.0s
#2 [internal] load .dockerignore
#2 transferring context: 2B done
#2 DONE 0.0s
#3 [internal] load metadata for docker.io/adoptopenjdk/openjdk11:alpine-jre
#3 DONE 0.9s
#4 [internal] load build context
#4 transferring context: 2B done
#4 DONE 0.0s
#5 [3/4] RUN echo target/tender-api.jar
#5 CACHED
#6 [2/4] WORKDIR /opt/app
#6 CACHED
#7 [4/4] COPY target/tender-api.jar app.jar
#7 ERROR: failed to walk /var/lib/docker/tmp/buildkit-mount4259571673/target: lstat /var/lib/docker/tmp/buildkit-mount4259571673/target: no such file or directory
#8 [1/4] FROM docker.io/adoptopenjdk/openjdk11:alpine-jre#sha256:c4e70e7696899eae647575724b77cc71efa8bea35c17b8d58fbb9bb6485af353
#8 resolve docker.io/adoptopenjdk/openjdk11:alpine-jre#sha256:c4e70e7696899eae647575724b77cc71efa8bea35c17b8d58fbb9bb6485af353 done
#8 sha256:c4e70e7696899eae647575724b77cc71efa8bea35c17b8d58fbb9bb6485af353 433B / 433B done
#8 sha256:b565a288907ff71371d4b22e2d1756ecdab0f5df188a8da6e5af388c330b4615 951B / 951B done
#8 sha256:00e2ce5eeb8afe4d1487c5976172e0fcb4258cbf7628836e6476be083e9e3f3e 6.13kB / 6.13kB done
#8 CANCELED
------
> [4/4] COPY target/tender-api.jar app.jar:
------
failed to solve: failed to compute cache key: failed to walk /var/lib/docker/tmp/buildkit-mount4259571673/target: lstat /var/lib/docker/tmp/buildkit-mount4259571673/target: no such file or directory
now, i can't understand what it is "ERROR: failed to walk ...", i just executed
docker compose up and afterwards I thought that docker can't create file, because of that i have tried with sudo , Nevertheless it's not working

i resolved it and wanted to share with you my solution
changed Dockerfile like following
#
# Build stage
#
FROM maven:3.6.0-jdk-11-slim AS build
COPY src /app/src
COPY pom.xml /app
RUN mvn -q -f /app/pom.xml clean package -DskipTests
# without -q you can take limit size problem
#
# Package stage
#
FROM adoptopenjdk/openjdk11:alpine-jre
ARG JAR_FILE=/app/target/tender-api.jar
COPY --from=build ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
as second, changed context and dockerfile in docker-compose.yml
context: /root/dev/tender-api/
dockerfile: Dockerfile

Related

docker-compose up gives 'failed to read dockerfile: error from sender'

Tried to simply dockerize my mongodb and spring boot application. Had a lot of struggles and thought I almost had it running and than the terminal hits me with this error:
Building user
[+] Building 0.0s (1/2)
=> ERROR [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 121B 0.0s
------
> [internal] load build definition from Dockerfile:
------
failed to solve with frontend dockerfile.v0: failed to read dockerfile: error from sender: walk \\?\C:\Users\ZRC\Documents\GitHub\s6-kwetter-backend\user\Dockerfile: The system cannot
find the path specified.
ERROR: Service 'user' failed to build : Build failed
Dockerfile (which is in a sub-directory; user-module):
FROM openjdk:11
EXPOSE 8081
ADD target/user-module-docker.jar user-docker.jar
CMD ["java", "-jar", "user-docker.jar"]
docker-compose.yml (which is in the main directory that has multiple modules/microservices):
version: '3.8'
services:
user:
build: ./user/Dockerfile
restart: unless-stopped
container_name: user-ms
ports:
- 8081:8080
mongodb:
image: mongo
restart: always
container_name: mongodb
ports:
- 27017:27017
Like it says that the path specified cannot be found but it literally exists, so where could I have gone wrong?
The error is telling you that the Dockerfile was not found, because the path doesn't exist. That's because it is trying to enter the path as folder.
The system cannot find the path specified.
This comes because you made a mistake in the compose build syntax. There are 2 ways it can be used.
1. The simple form:
This is using ./users/ as context, expecting a Dockerfile to be in this directory.
user:
build: ./user
2. The complex form:
user:
build:
context: ./
dockerfile: ./users/Dockerfile
This lets you separate the context and where the Dockerfile is. In this example, the current folder is used as context, and the Dockerfile is taken from ./users/Dockerfile. It is also useful when you have a different name for your Dockerfile. I.E. Dockerfile.dev.
Note that this is just an example, I don't know if this would make sense in your project. You need to know what context is the correct one.
What do I mean by context?
The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.
As example:
docker build --file /path/to/Dockerfile /path/to/context

Docker is not restarting when code changes

As described here https://docs.docker.com/compose/gettingstarted/#step-7-update-the-application if I mount the folder with the source code inside the volumes of that docker, I should see my changes instantly...
However, this is my docker-compose:
version: '3'
services:
database:
...
engine:
build: ./name
restart: always
volumes:
- .:/name-code
- ./name/config-examples:/config
ports:
- 8080:8080
depends_on:
- database
environment:
- TZ=Europe/Rome
...
grafana:
...
and as you can see I'm mounting .:/name-code... so as soon as I change code inside this directory, I should see that docker restarting with the new JAR (I'm developing a Spring boot application)... however this is not happening, infact my local jar looks like this:
-rw-r--r-- 1 user staff 43171359 Jun 1 12:35 project.jar
but the jar in the docker is the following:
-rw-r--r-- 1 root root 43171269 May 31 19:52 app.jar
(Is just been renamed), but as you can see the last update is very different, and infact the updates are not been imported
Log:
docker-compose --verbose build
compose.config.config.find: Using configuration files: ./docker-compose.yml
compose.cli.docker_client.get_client: docker-compose version 1.29.1, build c34c88b2
docker-py version: 5.0.0
CPython version: 3.9.0
OpenSSL version: OpenSSL 1.1.1h 22 Sep 2020
compose.cli.docker_client.get_client: Docker base_url: http+docker://localhost
compose.cli.docker_client.get_client: Docker version: Platform={'Name': 'Docker Engine - Community'}, Components=[{'Name': 'Engine', 'Version': '20.10.6', 'Details': {'ApiVersion': '1.41', 'Arch': 'amd64', 'BuildTime': '2021-04-09T22:44:56.000000000+00:00', 'Experimental': 'false', 'GitCommit': '8728dd2', 'GoVersion': 'go1.13.15', 'KernelVersion': '5.10.25-linuxkit', 'MinAPIVersion': '1.12', 'Os': 'linux'}}, {'Name': 'containerd', 'Version': '1.4.4', 'Details': {'GitCommit': '05f951a3781f4f2c1911b05e61c160e9c30eaa8e'}}, {'Name': 'runc', 'Version': '1.0.0-rc93', 'Details': {'GitCommit': '12644e614e25b05da6fd08a38ffa0cfe1903fdec'}}, {'Name': 'docker-init', 'Version': '0.19.0', 'Details': {'GitCommit': 'de40ad0'}}], Version=20.10.6, ApiVersion=1.41, MinAPIVersion=1.12, GitCommit=8728dd2, GoVersion=go1.13.15, Os=linux, Arch=amd64, KernelVersion=5.10.25-linuxkit, BuildTime=2021-04-09T22:44:56.000000000+00:00
compose.cli.verbose_proxy.proxy_callable: docker inspect_network <- ('codice_default')
compose.cli.verbose_proxy.proxy_callable: docker inspect_network -> {'Attachable': False,
'ConfigFrom': {'Network': ''},
'ConfigOnly': False,
'Containers': {},
'Created': '2021-06-01T10:31:25.2768986Z',
'Driver': 'bridge',
'EnableIPv6': False,
'IPAM': {'Config': [{'Gateway': '172.31.0.1', 'Subnet': '172.31.0.0/16'}],
'Driver': 'default',
'Options': None},
...
compose.project.build: database uses an image, skipping
compose.project.build: grafana uses an image, skipping
compose.service.build: Building engine
[+] Building 0.3s (8/8) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/adoptopenjdk/openjdk11:jdk-11.0.11_9-alpine-slim 0.0s
=> [1/3] FROM docker.io/adoptopenjdk/openjdk11:jdk-11.0.11_9-alpine-slim 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 1.43kB 0.0s
=> CACHED [2/3] COPY target/*.jar app.jar 0.0s
=> [3/3] COPY config-examples /app/config 0.0s
=> exporting to image 0.1s
=> => exporting layers 0.0s
=> => writing image sha256:7162be9dfe5c15feedd12d19a5985a0f619e623eb69e949283c6e2d66442f533 0.0s
=> => naming to docker.io/library/codice_engine 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
the Dockerfile of the engine service is the following:
FROM adoptopenjdk/openjdk11:jdk-11.0.11_9-alpine-slim
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
COPY config-examples /app/config
CMD ["java","-jar","/app.jar"]
I read again you post, and the misunderstanding is on the fact that .:/name-code is a volume mount point, it is not part of the docker image built by docker-compose, so changing its content does not force docker to rebuild the image. Else, the new content will be accessibile from the container.
For that reason you can see that CACHED [2/3] COPY target/*.jar app.jar is retrieved from a cached layer.
In my opinion the solution is to write a custom Dockerfile which adds explicitly the jar as depicted here https://docs.docker.com/compose/compose-file/compose-file-v3/#dockerfile . So, each time the jar changes, the image will be rebuilt as you expect.
Regards.

Github action: Build and push docker image fails. server message: insufficient_scope: authorization failed

I'm using the GitHub action "Build and push Docker images" as it's from Docker and a top rated verified action.
The relevant snippet of my YAML file is as follows
- name: Set up QEMU
uses: docker/setup-qemu-action#v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action#v1
- name: Login to DockerHub
uses: docker/login-action#v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }}
- name: Build and push
id: docker_build
uses: docker/build-push-action#v2
with:
push: true
tags: user/app:latest
- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
Just as it was shown in the example. When the workflow runs, I consistently see the error
10 [stage-1 2/2] COPY --from=build /workspace/target/*.jar app.jar
#10 DONE 0.9s
#12 exporting to image
#12 exporting layers
#12 exporting layers 4.3s done
#12 exporting manifest sha256:dafb0869387b325491aed0cdc10c2d0206aca28006b300554f48e4c389fc3bf1 done
#12 exporting config sha256:f64316c3b529b43a6cfcc933656c77e556fea8e5600b6d0cce8dc09f775cf107 done
#12 pushing layers
#12 pushing layers 0.8s done
#12 ERROR: server message: insufficient_scope: authorization failed
------
> exporting to image:
------
failed to solve: rpc error: code = Unknown desc = server message: insufficient_scope: authorization failed
Error: The process '/usr/bin/docker' failed with exit code 1
The contents of my Dockerfile for a standard spring-boot application is as shown below
FROM maven:3.6.3-jdk-11-slim AS build
RUN mkdir -p /workspace
WORKDIR /workspace
COPY pom.xml /workspace
COPY src /workspace/src
RUN mvn -B -f pom.xml clean package -DskipTests
FROM openjdk:11-jdk-slim
COPY --from=build /workspace/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
Any clue how this can be fixed?
I'm able to publish to docker-hub when using a different GitHub action as shown below
- name: Build and push docker image
uses: elgohr/Publish-Docker-Github-Action#master
with:
name: bloque/sales-lead-management
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }}
You need to set a path context while using the Docker's build-push-action. It should look something like this:
- name: Build and push
id: docker_build
uses: docker/build-push-action#v2
with:
context: .
file: Dockerfile
push: true
tags: user/app:latest
The file option is entirely optional, but if left out it will find the Dockerfile inside the root directory.
It's also recommended to use the metadata action that provides more relevant metadata and tags for your Docker image.
Here is an example of how I did it for Spring Boot apps in few of my projects: https://github.com/moja-global/FLINT.Reporting/blob/d7504909f8f101054e503a2993f4f70ca92c2577/.github/workflows/docker.yml#L153

gradle docker - how to skip build if build was done previously

I noticed the gradle:5.6.3-jdk8 always build the jar when I docker-compose up the yml file.
i thought the process will skip it if build completed earlier.
how to skip build if build was done previously?
Docker compose file
locationcommand:
networks:
- abc
build:
context: ../../
dockerfile: ./test-command/Dockerfile-test
ports:
- "8008:8008"
hostname: testcommand
container_name: testcommand
Dockerfile-test
FROM gradle:5.6.3-jdk8 as builder
COPY . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle build

Docker Containers for Maven Multi-module Spring Boot project with Angular Frontend module

I have Maven Multi-Modules Project (Angular Frontend Module + SpringBoot Backend Module) residing under same Parent Maven Project. I want to run the application in Docker Multi-containers (docker container for Frontend + docker container for Backend + docker container for database). How can I use Docker Compose to run docker multi-containers?
I created "docker-compose.yml" in the parent project, and in each module I add "Dockerfile".
Here is structure of my project
docker-compose.yml File
version: '3'
services:
docker-container-application-mariadb:
image: mariadb:latest
environment:
- MYSQL_ROOT_PASSWORD=*****
- MYSQL_DATABASE=*******
- MYSQL_USER=*******
- MYSQL_PASSWORD=*******
volumes:
- /data/application-mariadb
docker-container-application-backend:
image: docker-image-application-backend
build:
context: ./application-backend
dockerfile: Dockerfile
depends_on:
- docker-container-application-mariadb
ports:
- 8087:8080
volumes:
- /data/application-backend
Backend Dockerfile:
FROM openjdk:8
EXPOSE 8080
ADD target/application_backend.jar application_backend.jar
# Run the jar file
ENTRYPOINT ["java", "-jar", "application_backend.jar"]
ENTRYPOINT ["java", "-jar", "application_backend.jar"]
When I run:
docker-compose up
here is the error I am getting:
Step 4/6 : ADD $PWD/application-backend/target/application_backend.jar application_backend.jar
ERROR: Service 'docker-container-application-backend' failed to build: ADD failed: stat /var/lib/docker/tmp/docker-builder971325573/esysync-backend/target/application_backend.jar: no such file or directory
Error after code update:
Error after Code update suggested by #Milah:
This should work. I don't have your environment to test so if you get errors let me know and I'll fix them.
Place this Docker file at the root of the project (same level as docker-compose).
Dockerfile:
FROM maven:3.6.0-jdk-8-alpine as build
WORKDIR /app
COPY . .
WORKDIR /app/application-backend
RUN mvn clean package -DskipTests=true
FROM openjdk:8
WORKDIR /app
COPY --from=build /app/application-backend/target/*.jar ./
EXPOSE 8080
# Run the jar file
ENTRYPOINT ["java", "-jar", "/app/application_backend.jar"]
Next to the Dockerfile there should be also an ignore file.
.dockerignore
application-frontend/
You should change the docker-compose file as follows:
...
docker-container-application-backend:
image: docker-image-application-backend
build:
context: ./
dockerfile: Dockerfile
...
The declaration dockerfile: Dockerfile is redundant, can be rmoved.

Resources