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
Related
I want to run tests using Gradle after docker-compose up (Postgres DB + Spring-Boot app). All flow must be running inside the Gitlab merge request step. The problem is when I was running my test using the script part in gitlab-ci file. Important, in such a situation, we are in the correct directory where GitLab got my project. Part of gitlab-ci file:
before_script:
- ./gradlew clean build
- cp x.jar /path/x.jar
- docker-compose -f /path/docker-compose.yaml up -d
script:
- ./gradlew :functional-tests:clean test -Penv=gitlab --info
But here I can't call http://localhost:8080 -> connection refused. I try put 0.0.0.0 or 172.17.0.3 or docker.host... etc insite tests config, but it didn't work.
So, I made insite docker-compose another container where I try to run my test using the entry point command. To do that, I must have the current GitLab directory, but can't mount them.
My current solution:
Gitlab-ci:
run-functional-tests:
stage: run_functional_tests
image:
name: 'xxxx/docker-compose-java-11:0.0.7'
script:
- ./gradlew clean build -x test
- 'export SHARED_PATH="$(dirname ${CI_PROJECT_DIR})"' // current gitlab worspace dir
- cp $CI_PROJECT_DIR/x.jar $CI_PROJECT_DIR/docker/gitlab/x.jar
- docker-compose -f $CI_PROJECT_DIR/docker/gitlab/docker-compose.yaml up -d
- docker-compose -f $CI_PROJECT_DIR/docker/gitlab/docker-compose.yaml logs -f
timeout: 30m
docker-compose.yaml
version: '3'
services:
postgres:
build:
context: ../postgres
container_name: postgres
restart: always
networks:
- app-postgres
ports:
- 5432
app:
build:
context: .
dockerfile: Dockerfile
restart: always
container_name: app
depends_on:
- postgres
ports:
- "8080:8080"
networks:
- app-postgres
functional-tests:
build:
context: .
container_name: app-functional-tests
working_dir: /app
volumes:
- ${SHARED_PATH}:/app
depends_on:
- app
entrypoint: ["bash", "-c", "sleep 20 && ./gradlew :functional-tests:clean test -Penv=gitlab --info"]
networks:
- app-postgres
networks:
app-postgres:
but in such a situation my working_dir - /app - is empty. Can someone assist with that?
I would like use docker-compose to build/run dockerfiles that have envars in their FROM keyword. The problem that I am getting now is that I seem to be unable to pass envars from my environment through docker-compose into the dockerfile.
docker-compose.yml
version: "3.2"
services:
api:
build: 'api/'
restart: on-failure
depends_on:
- mysql
networks:
- frontend
- backend
volumes:
- ./api/php/:/var/www/html/
Dockerfile in 'api/'
FROM ${DOCKER_IMAGE_API}
RUN apk update
RUN apk upgrade
RUN docker-php-ext-install mysqli
Why?
I want to do this so that I can run docker-compose from a bash script that detects the host architecture and changes the base image of the underlying dockerfiles in the host application.
FROM instructions support variables that are declared by any ARG instructions that occur before the first FROM. So what you can do is this:
ARG IMAGE
FROM $IMAGE
when you run the build command, you then pass the --build-arg as follows:
docker build -t test --build-arg IMAGE=alpine .
you can also choose to have a default value for the IMAGE variable, to be used if the --build-arg flag isn't used.
Alternatively, in case you were to use docker compose build and not docker build (and I think this is your case), you can specify the variable in the docker-compose build --build-arg:
version: "3.9"
services:
api:
build: .
and then
docker compose build --build-arg IMAGE=alpine
I am using Jenkins to run my unit tests. Also, I am using docker-compose to link the spring boot and its Postgres database. Each time the Jenkins file is executed during a pull request or commit, I use the compose stack and to check that the tests have been performed correctly.
If the test fails then container aborted and Jenkins notifies but in a positive scenario when the spring boot application starts Jenkins doesn't notify and sticks.
this is the docker file :
FROM openjdk:10-jdk
COPY run.sh /
RUN chmod +x /run.sh
COPY ./target/*.jar /app.jar
CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
This is the docker-compose file:
version: '3.2'
services:
app:
restart: always
build: .
container_name: app
working_dir: /app
volumes:
- .:/app
ports:
- 8085:8080
links:
- pgsql
depends_on:
- pgsql
pgsql:
image: postgres:10
container_name: pgsql
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=passwordorsomething
- POSTGRES_USER=postgres
- POSTGRES_DB=pgsql
restart: always
This is the stage for running docker compose and start spring boot and run the test :
stage('Test') {
agent {
label "docker"
}
steps {
sh 'docker rm -f $(docker ps -a -q)'
sh 'docker-compose up --build --exit-code-from app'
}
}
After Jenkins reach to 'docker-compose up --build --exit-code-from app' and the spring boot starts it sticks in the Test stage.
It's only a guess, but is the restart: always making the container restart. Assuming some of your tests are failing?
Its a good idea to add a post block to do docker-compose down to avoid zombie container
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.
I am trying to run a golang app using docker-compose, below is my compose configuration.
version: '2'
services:
#Application container
go:
image: golang:1.8-alpine
ports:
- "80:8080"
links:
- mongodb
environment:
DEBUG: 'true'
PORT: '8080'
working_dir: /go/src/simple-golang-app
command: go run main.go
volumes:
- ./simple-golang-app:/go/src/simple-golang-app
mongodb:
image: mvertes/alpine-mongo:3.2.3
restart: unless-stopped
ports:
- "27017:27017"
On running the compose using command "docker-compose up" i get error "stat main.go: no such file or directory" even when main.go is available in working directory.
it works fine when your host dir layout is
oxo#thor ~/Dropbox/Documents/code/docker/golang_working_dir $ find .
.
./docker-compose.yaml
./simple-golang-app
./simple-golang-app/main.go
so here we
cd ~/Dropbox/Documents/code/docker/golang_working_dir
docker-compose up
for a more complex build involving dependancies I use a Dockerfile :
FROM golang:1.8-alpine
RUN mkdir -p /go/src/simple-golang-app/
COPY simple-golang-app/main.go /go/src/simple-golang-app
WORKDIR /go/src/simple-golang-app
RUN apk add --no-cache git mercurial && go get -v -t ./... && apk del git mercurial
RUN go install ./...
RUN go build
ENV PORT 9000
now update your docker-compose.yaml to use this new image :
old
image: golang:1.8-alpine
new
image: nirmal_golang_alpine:latest
so your commands are
docker build --tag nirmal_golang_alpine
docker-compose up