I currently have a docker-compose file with 3 services:
selenium-hub
chrome-node
a container containing my selenium JUnit tests.
I want to selenium-test (w/ JUnit) my JAR file (containing these tests) with a command in the container's Dockerfile. I thought about using the selenium-server.jar but I can't seem to figure out what the exec command should be (in the Dockerfile).
Anyone can help me out?
My docker-compose.yml:
version: "3"
services:
chrome:
image: selenium/node-chrome:4.1.4-20220427
shm_size: 2gb
container_name: chrome
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
selenium-hub:
image: selenium/hub:4.1.4-20220427
container_name: selenium-hub
ports:
- "4442:4442"
- "4443:4443"
- "4444:4444"
application:
build: .
container_name: application
ports:
- "4447:4447"
depends_on:
- chrome
And my Dockerfile:
#
# Build stage
#
FROM maven:3.6.0-jdk-11-slim AS build
COPY src .
COPY pom.xml .
RUN mvn -f pom.xml jar:test-jar
#
# Package stage
#
FROM openjdk:11-jre-slim
COPY --from=build target/playground-project-selenium-1.0-SNAPSHOT-tests.jar /usr/local/lib/demo.jar
ADD selenium-server.jar .
ENTRYPOINT exec java -jar selenium-server.jar --ext /usr/local/lib/demo.jar:selenium/node-chrome standalone --port 4447
EXPOSE 4447
After some sessions of trial-and-error, I've managed to answer my own question. I created a so-called Fat-JAR (using the maven assembly plugin) containing all tests, resources and dependencies and I used JUnit 4 with the org.junit.runner.JUnitCore class to run a test suite.
Now, my selenium UI tests are fully running in a Docker container using selenium hub and chrome driver in separate containers.
Related
I have a simple Spring Boot project.Create a controller and return a string.
#GetMapping("/test")
public String test(){
return "test done change";
}
I used Docker file and docker-compose.yml file.
My Docker File:
FROM maven:3.8.6-amazoncorretto-11
COPY target/*.jar docker-spring.jar
CMD ["java","-jar","/docker-spring.jar"]
docker-compose.yml file:
version: '3'
services:
app:
container_name: docker-spring
build: .
ports:
- "8080:8080"
networks:
- spring-cloud-network
networks:
spring-cloud-network:
driver: bridge
I used Windows, so I used docker in WLS2. I run mvnw pacakage and generate a .jar file local environment. Then I move the whole project into my docker folder and run docker-composer up -d --build. Then it runs and builds containers. But when I changed the return string to controller , it was not updated. I also used another Dockerfile:
# FROM maven:3.8.6-amazoncorretto-11
# COPY .mvn .mvn
# COPY mvnw .
# COPY pom.xml .
# COPY src src
# RUN ./mvnw install
# RUN ./mvnw package
# COPY target/*.jar docker-spring.jar
# ENTRYPOINT ["java", "-jar", "docker-spring.jar"]
But show error.
File Structure:
Thank you.
So i am quite new to the whole CI topic and im trying to run tests after my merge request in my pipeline. Except that when my command try to run the container i have an error.
So this is my gitlab-ci file
image: docker:18
variables:
REPO_URL: "registry.gitlab.com/xxxxx/xxxxxxx"
PROD_SRV: "xxx"
DEV_SRV: "xxx"
services:
- docker:dind
stages:
- build jar
- build docker image
- tests
maven-build:
only:
- work
- test
image: maven:3-jdk-8
stage: build jar
script: "mvn -Dmaven.test.skip=true package -B"
artifacts:
paths:
- target/*.jar
expire_in: 7 days
docker build:
stage: build docker image
only:
- work
- test
before_script:
- echo "$CI_REGISTRY_PASSWORD" | docker login --username "$CI_REGISTRY_USER" "$CI_REGISTRY" --password-stdin
- chmod a+x ./mvnw
script:
- docker info
- docker build -t $REPO_URL:$CI_COMMIT_SHA .
- docker images
- docker push $REPO_URL:$CI_COMMIT_SHA
after_script:
- docker logout registry.gitlab.com 2>/dev/null
- rm /root/.docker/config.json 2>/dev/null
tests job branch test:
stage: tests
only:
- test
variables:
GIT_STRATEGY: none
before_script:
- echo "$CI_REGISTRY_PASSWORD" | docker login --username "$CI_REGISTRY_USER" "$CI_REGISTRY" --password-stdin
script:
- docker pull $REPO_URL:$CI_COMMIT_SHA
- docker run -t $REPO_URL:$CI_COMMIT_SHA .
- docker push $REPO_URL:$CI_COMMIT_SHA
after_script:
- docker logout registry.gitlab.com 2>/dev/null
- rm /root/.docker/config.json 2>/dev/null
and this is my dockerfile
# syntax=docker/dockerfile:1
FROM openjdk:16-alpine3.13 as base
WORKDIR /amsn-counter
COPY .mvn/ .mvn
COPY mvnw pom.xml ./
RUN ./mvnw dependency:go-offline
COPY src ./src
FROM base as test
RUN ["./mvnw", "test"]
FROM base as development
CMD ["./mvnw", "spring-boot:run", "-Dspring-boot.run.profiles=mysql", "-Dspring-boot.run.jvmArguments='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000'"]
FROM base as build
RUN ./mvnw package
FROM openjdk:11-jre-slim as production
EXPOSE 8080
ADD /target/linkedin-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
and the error in the pipeline as well:
If you need any precision i will gladly answer, and like i said im new so don't hesitate to point if something is weird in the files or in my description in general
Edit: My branch are named {work}(local branch) and {test}
It looks as if there is no target specified when you are running docker build. Try this:
script:
- docker info
- image_name="$REPO_URL:$CI_COMMIT_SHA"
- docker build
--target test
--tag $image_name
.
- docker push $image_name
Under the current build order
build localhost image
build web image
run localhost container
run web container
This will fail in step 2, since the java at src will not compile because it cannot to localhost, as localhost image is not running. The solution will be change the current build order to:
Under the current build order
build localhost image
run localhost container
build web image
run web container
How do I achieve that in dockerfile and/or docker-compose.yml?
Dockerfile
#Build
FROM maven:latest AS BUILD
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
RUN cd /usr/src/app &&\
mvn install
#ENVIRONMENT
FROM openjdk:16
COPY --from=BUILD /usr/src/app/target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yml
version: '3'
services:
localhost:
image: mysql:5.7.34
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=virtual_election
- MYSQL_USER=myapp-user
- MYSQL_PASSWORD=password
network_mode: host
web:
image: web
restart: on-failure
build:
context: ./
dockerfile: Dockerfile
depends_on:
- localhost
network_mode: host
I'm trying to create docker image by docker-compose. How I can do that?
I can start my project (spring boot + cassandra) by docker-compose up. And everything works great. But in next step I want to make from this project image, push to docker hub and pull it from docker hub to test on other computer. Im tried 'docker-compose build', 'docker-compose push' and then 'docker-compose pull'. After pull I can see information that cassandra and spring-boot-app is downloaded. But then, when I want to run this image by 'docker run' but it runs only springboot, without cassandra.
This is my docker-compose.yaml file:
version: '3'
services:
cassandra:
build:
context: ../
dockerfile: docker/cassandra/Dockerfile
ports:
- "9042:9042"
container_name: cassandra
spring-boot-cassandra:
build:
context: ../
dockerfile: docker/springbootsample/Dockerfile
links:
- cassandra
ports:
- "8080:8080"
environment:
SPRING_DATA_CASSANDRA_CONTACT_POINTS: cassandra
container_name: springboot
entrypoint: /wait-for-it.sh cassandra:9042 -- java -Djava.security.egd=file:/dev/./urandom -jar app.jar
depends_on:
- "cassandra"
image: myrepo/springbootsample
networks:
default:
driver: bridge
docker-compose is used to handle multiple images. I guess you are thinking it will merge two images into one, which will not happen.
docker run is used to run only one image at a time, this was the limitation because of that docker-compose was introduced.
docker-compose up does docker run, docker network create/add etc. commands to create you an environment. For use,
docker-compose build will build all your images present in the docker-compose.yml file.
docker-compose push will push all your images to the hub.
docker-compose pull will download those images from the hub if present.
and finally, if you want to run those images, use docker-compose up. If the images are not present it will download those images first from the hub.
I need to control the order of Docker containers instantiation, the problem is that I want to build a Jar file with the Docker maven container then pass that jar to an OpenJDK Docker container in order to build an image and then instantiate a MongoDB container and a Java-App container with the OpenJDK image generated before that communicates between them via docker-compose.
The problem is the Build always fails because some of the Unit tests talk to the database before it's initialized and since the tests fail the build also fails.
This is my dockerfile:
FROM maven:3.5-alpine
COPY ./ /app
RUN cd /app && mvn package
FROM openjdk:8
COPY spring-rest-iw-exam.jar /tmp/spring-rest-iw-exam.jar
EXPOSE 8087
ENTRYPOINT ["java", "-jar", "/tmp/spring-rest-iw-exam.jar"]
This is my Docker-Compose:
version: '2'
services:
mongodb:
image: mongo
container_name: iw_exam_mongo
restart: always
ports:
- "27017:27017"
environment:
- MONGO_INITDB_DATABASE=fizz_buzz_collection
volumes:
- /opt/iw-exam/data:/data/db
spring-app:
container_name: iw_exam_java_rest_api
build: ./
restart: always
ports:
- "8087:8087"
depends_on:
- mongodb
I tried with depends_on and did some other tests with a tool call dockerize but none of it works, the maven build always starts before docker-compose even start to instantiate mongodb.
This is the github repository of the proyect:
https://github.com/dsalasboscan/exam
I need to instantiate Mongodb first and THEN start with the maven build and java image generation.
I came across similar problem before, and would like to share my experience.
Basically, we need to wait for a while to make sure mongodb is completely boot up, here is the tool that you can leverage. It's fairly easy to use.