Reputation: 346
I have done 3 simple rest applications I run them with docker-compose It seems that only a product app is on all 3 ports I am fresh to docker.
here is Dockerfile:
FROM adoptopenjdk/openjdk11:ubi
ADD credit/target/credit-ChocoladeIcecream.jar .
CMD java -jar credit-ChocoladeIcecream.jar
FROM adoptopenjdk/openjdk11:ubi
ADD client/target/client-ChocoladeIcecream.jar .
CMD java -jar client-ChocoladeIcecream.jar
FROM adoptopenjdk/openjdk11:ubi
ADD product/target/product-ChocoladeIcecream.jar .
CMD java -jar product-ChocoladeIcecream.jar
And the docker-compose:
version: '3.9'
services:
credit_app:
image: 'credit:latest'
build:
context: ./
container_name: credit
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/${POSTGRES_DB}
- SPRING_DATASOURCE_USERNAME=${POSTGRES_USER}
- SPRING_DATASOURCE_PASSWORD=${POSTGRES_PASSWORD}
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
- HOST_NAME=${HOST}
- CLIENT_PORT=${PORT2}
- PRODUCT_PORT=${PORT3}
ports:
- ${PORT1}:8080
client_app:
image: 'client:latest'
build:
context: ./
container_name: client
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/${POSTGRES_DB}
- SPRING_DATASOURCE_USERNAME=${POSTGRES_USER}
- SPRING_DATASOURCE_PASSWORD=${POSTGRES_PASSWORD}
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
- HOST_NAME=${HOST}
- CREDIT_PORT=${PORT1}
- PRODUCT_PORT=${PORT3}
ports:
- ${PORT2}:8080
product_app:
image: 'product:latest'
build:
context: ./
container_name: product
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/${POSTGRES_DB}
- SPRING_DATASOURCE_USERNAME=${POSTGRES_USER}
- SPRING_DATASOURCE_PASSWORD=${POSTGRES_PASSWORD}
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
- HOST_NAME=${HOST}
- CLIENT_PORT=${PORT2}
- CREDIT_PORT=${PORT1}
ports:
- ${PORT3}:8080
db:
image: 'postgres:13.1-alpine'
container_name: db
environment:
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
here is also .env file if needed
HOST=localhost
PORT1=8089
PORT2=8090
PORT3=8091
POSTGRES_DB=database_name
POSTGRES_USER=user
POSTGRES_PASSWORD=pass123
I check it with postman, trying to ad some elements, and when i GET all elements on each port I receive only Product
They must all run from one Jar, how can I make so each image is created from a different jar?
Please help me understand what went wrong.
Thanks in advance.
Upvotes: 1
Views: 89
Reputation: 20286
The problem is in Dockerfile, you use product
image in all three services. When you have several FROM
statements, it won't build three images. This is used for multi-stage building, e.g. when you use one image (stage) to compile a binary and another one to just hold the product without compilers, kernel headers, etc.
One way you can solve this is to split the Dockerfile into three separate files (credit.Dockerfile
,client.Dockerfile
,product.Dockerfile
) and then mention each one in their respective service:
credit_app:
image: 'credit:latest'
build:
context: ./
dockerfile: credit.Dockerfile
# repeat for other two with their Dockerfiles
Another way would be to use the same Dockerfile and different context but require you to drop product-
(and others) prefix from the jar
files. In other words, if they have the same name, you can use a Dockefile like this:
FROM adoptopenjdk/openjdk11:ubi
ADD target/ChocoladeIcecream.jar .
CMD java -jar ChocoladeIcecream.jar
And to build different images from this Dockerfile you set different context:
credit_app:
image: 'credit:latest'
build:
context: ./credit
# By default it will look for the Dockerfile inside the root of context,
# and you need to point to the file outside of it (relative to where
# the docker-compose.yml is).
dockerfile: ./Dockerfile
There is also a way to use ARG
to create various images from one Dockerfile and it can be used in this case to ADD
the correct jar file, but it won't work for CMD
. So to make use of ARG
you need that jar
files inside container have the same name:
FROM adoptopenjdk/openjdk11:ubi
ARG APP
# Add file without prefix
ADD ${APP}/target/${APP}-ChocoladeIcecream.jar ChocoladeIcecream.jar
CMD java -jar ChocoladeIcecream.jar
And then you can set the APP
argument in docker-compose.yml
:
credit_app:
image: 'credit:latest'
build:
context: ./
args:
APP: credit
Upvotes: 1