My docker-compose.yml file
version: "3.9"
services:
webapp:
image: springbootproject
build: .
ports:
- "8085:8085"
depends_on:
- postgres
- pgadmin
postgres:
container_name: postgres_container
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 12345
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
PGADMIN_CONFIG_SERVER_MODE: 'False'
volumes:
- pgadmin:/root/.pgadmin
ports:
- "${PGADMIN_PORT:-80}:80"
networks:
- postgres
restart: unless-stopped
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:
Dockerfile
FROM adoptopenjdk:11-jre-hotspot
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} springmvcrestapi.jar
EXPOSE 8085
ENTRYPOINT ["java","-jar","springmvcrestapi.jar"]
Hi everyone my simple project using docker inteelij ide terminal writing docker-compose up postgre and pgadmin working container but my project images Error: Unable to access jarfile springmvcrestapi-0.0.1-SNAPSHOT.jar What is the problem ??
Related
I have a problem about running my spring boot microservices on Docker.
When I run docker-compose up, I get this message constantly in config server.
Adding property source: Config resource '
file [/tmp/config-repo-11055867458011548527/application.yml]' via location 'file:/tmp/config-repo-11055867458011548527/'
Here is my docker-compose.yml file
services:
serviceregistry:
image: 'noyandocker/serviceregistry:latest'
container_name: serviceregistry
ports:
- '8761:8761'
networks:
- backend
configserver:
image: 'noyandocker/configserver:latest'
container_name: configserver
ports:
- '9296:9296'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
healthcheck:
test: [ "CMD", "curl", "-f", "http://configserver:9296/actuator/health" ]
interval: 10s
timeout: 5s
retries: 5
depends_on:
- serviceregistry
networks:
- backend
apigateway:
image: 'noyandocker/apigateway:latest'
container_name: apigateway
ports:
- '9090:9090'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- ZIPKIN_URL=http://zipkin:9411
- REDIS_URL=redis://redis:6379
depends_on:
configserver:
condition: service_healthy
zipkin:
condition: service_healthy
redis:
condition: service_started
networks:
- backend
zipkin:
image: openzipkin/zipkin
ports:
- "9411:9411"
networks:
- backend
redis:
image: redis
ports:
- "6379:6379"
networks:
- backend
authservice:
image: 'noyandocker/authservice:latest'
container_name: authservice
ports:
- '7777:7777'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- spring.datasource.url=jdbc:mysql://database:3306/userdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
- spring.datasource.username=root
- spring.datasource.password=ippavlova_1990
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
networks:
- backend
productservice:
image: 'noyandocker/productservice:latest'
container_name: productservice
ports:
- '8081:8081'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- spring.datasource.url=jdbc:mysql://database:3306/productdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
- spring.datasource.username=root
- spring.datasource.password=ippavlova_1990
- ZIPKIN_URL=http://zipkin:9411
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
zipkin:
condition: service_healthy
networks:
- backend
orderservice:
image: 'noyandocker/orderservice:latest'
container_name: orderservice
ports:
- '8082:8082'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- spring.datasource.url=jdbc:mysql://database:3306/orderdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
- spring.datasource.username=root
- spring.datasource.password=ippavlova_1990
- ZIPKIN_URL=http://zipkin:9411
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
zipkin:
condition: service_healthy
networks:
- backend
paymentservice:
image: 'noyandocker/paymentservice:latest'
container_name: paymentservice
ports:
- '8083:8083'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- spring.datasource.url=jdbc:mysql://database:3306/paymentdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
- spring.datasource.username=root
- spring.datasource.password=ippavlova_1990
- ZIPKIN_URL=http://zipkin:9411
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
zipkin:
condition: service_healthy
networks:
- backend
database:
container_name: mysql-database
image: 'mysql:latest'
ports:
- "3306:3306"
restart: always
environment:
#MYSQL_USER: root
MYSQL_PASSWORD: ippavlova_1990
MYSQL_ROOT_PASSWORD: ippavlova_1990
volumes:
- db-data:/var/lib/mysql
networks:
- backend
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10
networks:
backend:
driver: bridge
volumes:
db-data:
How can I fix it?
I have a screenshot of my IDE if that helps clarify things at all, along with the GitHub repository for my project.
After I revised the config server url shown below, the issue disappeared.
CONFIG_SERVER_URL=http://configserver:9296
I revised an example of Spring Boot Microservices and I have a problem about running all services on docker.
After creating the image file of each services, I devised a docker-compose.yml to run all services on Docker. I noticed some of services throw ConnectException regarding configserver but I have no idea how to fix it. Here are the code snippets to create Docker image for each service.
1 ) service-registry
- mvn clean install
- docker build -t microservicedailybuffer/serviceregistry:0.0.1 .
2 ) configserver
- mvn clean install
- docker build -t microservicedailybuffer/configserver:0.0.1 .
3 ) apigateway
- mvn clean install -D skipTests
- docker build -t microservicedailybuffer/apigateway:0.0.1 .
4 ) auth-service
- mvn clean install -D skipTests
- docker build -t microservicedailybuffer/authservice:0.0.1 .
5 ) orderservice
- mvn clean install -D skipTests
- docker build -t microservicedailybuffer/orderservice:0.0.1 .
6 ) productservice
- mvn clean install -D skipTests
- docker build -t microservicedailybuffer/productservice:0.0.1 .
7 ) paymentservice
- mvn clean install -D skipTests
- docker build -t microservicedailybuffer/paymentservice:0.0.1 .
Here is my docker-compose.yml shown below
services:
serviceregistry:
image: 'microservicedailybuffer/serviceregistry:0.0.1'
container_name: serviceregistry
ports:
- '8761:8761'
networks:
- backend
configserver:
image: 'microservicedailybuffer/configserver:0.0.1'
container_name: configserver
ports:
- '9296:9296'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
healthcheck:
test: [ "CMD", "curl", "-f", "http://configserver:9296/actuator/health" ]
interval: 10s
timeout: 5s
retries: 5
depends_on:
- serviceregistry
networks:
- backend
apigateway:
image: 'microservicedailybuffer/apigateway:0.0.1'
container_name: apigateway
ports:
- '9090:9090'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- ZIPKIN_URL=http://zipkin:9411
- REDIS_URL=redis://redis:6379
depends_on:
- configserver
- zipkin
- redis
- serviceregistry
networks:
- backend
zipkin:
image: openzipkin/zipkin
ports:
- "9411:9411"
networks:
- backend
redis:
image: redis
ports:
- "6379:6379"
networks:
- backend
authservice:
image: 'microservicedailybuffer/authservice:0.0.1'
container_name: authservice
ports:
- '7777:7777'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- spring.datasource.url=jdbc:mysql://database:3306/userdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
- spring.datasource.username=root
- spring.datasource.password=ippavlova_1990
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
networks:
- backend
productservice:
image: 'microservicedailybuffer/productservice:0.0.1'
container_name: productservice
ports:
- '8081:8081'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- spring.datasource.url=jdbc:mysql://database:3306/productdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
- spring.datasource.username=root
- spring.datasource.password=ippavlova_1990
- ZIPKIN_URL=http://zipkin:9411
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
zipkin:
condition: service_healthy
networks:
- backend
orderservice:
image: 'microservicedailybuffer/orderservice:0.0.1'
container_name: orderservice
ports:
- '8082:8082'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- spring.datasource.url=jdbc:mysql://database:3306/orderdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
- spring.datasource.username=root
- spring.datasource.password=ippavlova_1990
- ZIPKIN_URL=http://zipkin:9411
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
zipkin:
condition: service_healthy
networks:
- backend
paymentservice:
image: 'microservicedailybuffer/paymentservice:0.0.1'
container_name: paymentservice
ports:
- '8083:8083'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- spring.datasource.url=jdbc:mysql://database:3306/paymentdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
- spring.datasource.username=root
- spring.datasource.password=ippavlova_1990
- ZIPKIN_URL=http://zipkin:9411
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
zipkin:
condition: service_healthy
networks:
- backend
database:
container_name: mysql-database
image: 'mysql:latest'
ports:
- "3306:3306"
restart: always
environment:
#MYSQL_USER: root
MYSQL_PASSWORD: ippavlova_1990
MYSQL_ROOT_PASSWORD: ippavlova_1990
volumes:
- db-data:/var/lib/mysql
networks:
- backend
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10
networks:
backend:
driver: bridge
volumes:
db-data:
When I run the docker-compose file through docker-compose up, I get some problems in api gateway.
Api Gateway Issue
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://configserver:9296/API-GATEWAY/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
How can I fix all these issue?
Here is the repo : Link
Here is the git based system file : Link
After revising the api gateway shown below, the issue disappeared.
apigateway:
image: 'microservicedailybuffer/apigateway:0.0.1'
container_name: apigateway
ports:
- '9090:9090'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- ZIPKIN_URL=http://zipkin:9411
- REDIS_URL=redis://redis:6379
depends_on:
configserver:
condition: service_healthy
zipkin:
condition: service_healthy
redis:
condition: service_started
networks:
- backend
You have to update your application.yml and docker-compose.yml files.
These lines should be present to override the EUREKA_SERVER_ADDRESS
eureka:
instance:
prefer-ip-address: true
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: ${EUREKA_SERVER_ADDRESS:http://localhost:8761/eureka}
and to communicate within the container you need to use a common network.
So it should look like this.
serviceregistry:
image: 'microservicedailybuffer/serviceregistry:0.0.1'
container_name: serviceregistry
ports:
- '8761:8761'
networks:
- backend
configserver:
image: 'microservicedailybuffer/configserver:0.0.1'
container_name: configserver
ports:
- '9296:9296'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
healthcheck:
test: [ "CMD", "curl", "-f", "http://configserver:9296/actuator/health" ]
interval: 10s
timeout: 5s
retries: 5
depends_on:
- serviceregistry
networks:
- backend
I have a problem about connecting mysql database to services in my Spring Cloud Project.
All these services are user service, advertisement service and lastly report service
Here is my error when I show logs of user service, advertisement service and lastly report service.
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.ConnectException: Connection refused (Connection refused)
How can I fix it?
Here is my docker-compose.yml shown below.
services:
database:
container_name: mysql-database
image: 'mysql:latest'
ports:
- "3366:3306"
restart: always
environment:
#MYSQL_DATABASE: "springbootuser"
MYSQL_USER: "springmicroserviceuser"
MYSQL_PASSWORD: "111111"
MYSQL_ROOT_PASSWORD: "111111"
volumes:
- db-data:/var/lib/mysql
networks:
- backend
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10
keycloak:
image: quay.io/keycloak/keycloak:18.0.2
environment:
- KEYCLOAK_ADMIN=admin
- KEYCLOAK_ADMIN_PASSWORD=admin
ports:
- "8181:8080"
networks:
- backend
command:
- start-dev
rabbitmq:
image: "rabbitmq:3-management"
container_name: "rmq3"
environment:
RABBITMQ_DEFAULT_USER: "rabbitmq"
RABBITMQ_DEFAULT_PASS: "123456"
ports:
- "5672:5672"
- "15672:15672"
networks:
- backend
configserver:
image: configserver
container_name: configServer
build:
context: ./configserver
dockerfile: Dockerfile
environment:
CONFIGSERVER_URI: "http://configserver:9191"
CONFIGSERVER_PORT: "9191"
ports:
- "9191:9191"
networks:
- backend
eurekaserver:
image: eurekaserver
ports:
- "8761:8761"
build:
context: ./discoveryserver
dockerfile: Dockerfile
environment:
CONFIGSERVER_URI: "http://configserver:9191"
CONFIGSERVER_PORT: "9191"
EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
EUREKASERVER_PORT: "8761"
WAIT_HOSTS: configserver:9191
depends_on:
configserver:
condition: service_started
networks:
- backend
gatewayserver:
image: gatewayserver
ports:
- "8600:8600"
build:
context: ./api-gateway
dockerfile: Dockerfile
environment:
PROFILE: "default"
SERVER_PORT: "8600"
CONFIGSERVER_URI: "http://configserver:9191"
EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
EUREKASERVER_PORT: "8761"
CONFIGSERVER_PORT: "9191"
WAIT_HOSTS: configserver:9191
depends_on:
configserver:
condition: service_started
eurekaserver:
condition: service_started
networks:
- backend
userservice:
image: user-service
build:
context: ./user-service
dockerfile: Dockerfile
environment:
SERVER_PORT: "9000"
CONFIGSERVER_URI: "http://configserver:9191"
CONFIGSERVER_PORT: "9191"
EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
EUREKASERVER_PORT: "8761"
WAIT_HOSTS: configserver:9191
DATABASE_HOST: database
DATABASE_USER: springmicroserviceuser
DATABASE_PASSWORD: 111111
DATABASE_NAME: springbootuser
DATABASE_PORT: 3306
SPRING_APPLICATION_JSON: '{
"spring.jpa.hibernate.ddl-auto" : "update"
}'
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
ports:
- "9000:9000"
networks:
- backend
managementservice:
image: management-service
build:
context: ./management-service
dockerfile: Dockerfile
environment:
SERVER_PORT: "9002"
CONFIGSERVER_URI: "http://configserver:9191"
CONFIGSERVER_PORT: "9191"
EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
EUREKASERVER_PORT: "8761"
WAIT_HOSTS: configserver:9191
depends_on:
configserver:
condition: service_started
advertisementservice:
condition: service_started
reportservice:
condition: service_started
ports:
- "9002:9002"
networks:
- backend
advertisementservice:
image: advertisement-service
build:
context: ./advertisement-service
dockerfile: Dockerfile
environment:
SERVER_PORT: "9001"
CONFIGSERVER_URI: "http://configserver:9191"
CONFIGSERVER_PORT: "9191"
EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
EUREKASERVER_PORT: "8761"
WAIT_HOSTS: configserver:9191
DATABASE_HOST: database
DATABASE_USER: springmicroserviceuser
DATABASE_PASSWORD: 111111
DATABASE_NAME: springbootadvertisement
DATABASE_PORT: 3306
SPRING_APPLICATION_JSON: '{
"spring.jpa.hibernate.ddl-auto" : "update"
}'
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
ports:
- "9001:9001"
networks:
- backend
reportservice:
image: report-service
build:
context: ./report-service
dockerfile: Dockerfile
environment:
SERVER_PORT: "9003"
CONFIGSERVER_URI: "http://configserver:9191"
CONFIGSERVER_PORT: "9191"
EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
EUREKASERVER_PORT: "8761"
WAIT_HOSTS: configserver:9191
DATABASE_HOST: database
DATABASE_USER: springmicroserviceuser
DATABASE_PASSWORD: 111111
DATABASE_NAME: springbootreport
DATABASE_PORT: 3306
SPRING_APPLICATION_JSON: '{
"spring.jpa.hibernate.ddl-auto" : "update"
}'
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
ports:
- "9003:9003"
networks:
- backend
networks:
backend:
driver: bridge
volumes:
db-data:
Here is my project link : Link
Here are all properties file of services under config server : Link
I want to containerize my spring-boot rest api together with the postgres database. Here is my Dockerfile for the api:
FROM openjdk:17
WORKDIR /app
COPY ./target/docker-0.0.1-SNAPSHOT.jar ./docker-0.0.1-SNAPSHOT.jar
CMD ["java","-jar","docker-0.0.1-SNAPSHOT.jar"]
I've created a .jar file using mvm. Here is my application.yml file.
# postgres sql connection
spring:
datasource:
password: password
url: jdbc:postgresql://postgresdb:5432/todos
username: admin
jpa:
hibernate:
ddl-auto: create #create
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show-sql: true
# Configuring the port for our server
server:
port: 3001
Here is my docker-compose file:
version: "3.9"
services:
postgresdb:
image: postgres:14.4-alpine
container_name: pgsql
restart: always
volumes:
- ./db/init.sql:/docker-entrypoint-initdb.d/0_init.sql
- $HOME/database:/var/lib/postgresql/data
ports:
- "5432:5432"
expose:
- "5432"
environment:
POSTGRES_DB: todos
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
SERVICE_NAME: postgresdb
networks:
- internalnet
springboot:
container_name: springbootserver
build: .
image: springboot:1.0
ports:
- "3001:3001"
expose:
- "3001"
environment:
DB_USER: "admin"
DB_PASSWORD: "password"
depends_on:
- postgresdb
networks:
- internalnet
networks:
internalnet:
driver: bridge
If i run docker logs springbootserver I'm getting the following error:
org.postgresql.util.PSQLException: Connection to postgresdb:5232 refused. Check that the hostname and port are correct and that the postmaster is accepting
TCP/IP connections.
What am i missing here?
when I build my docker image and run it it gives this and error however if I run the jar file everything runs fine if I had it guess it would have to do with my docer-compose.yml file but idk
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.2.3.jar!/:na]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: java.net.ConnectException: Connection refused
at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.2.3.jar!/:na]
... 4 common frames omitted
Dockerfile
FROM openjdk:17
ADD target/docker-document-rest-api.jar docker-document-rest-api.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "docker-document-rest-api.jar"]
application.properties
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
spring.data.mongodb.database=example
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
docer-compose.yml
version: "3.8"
services:
mongodb:
image: mongo
container_name: mongodb
ports:
- 27017:27017
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=rootuser
- MONGO_INITDB_ROOT_PASSWORD=rootpass
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
- ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
- ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
data: {}
networks:
default:
name: mongodb_network
By default docker containers are using bridge networking, so you cannot connect to localhost from a container. There can be several ways to solve this, one would be to connect your container, which is running the Java application, to the network where MongoDB is placed.
Since in your docker-compose you explicitly specify the name of the network (mongodb_network), we can do the following:
In the properties file we have to change localhost to mongodb, so:
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
spring.data.mongodb.database=example
spring.data.mongodb.port=27017
spring.data.mongodb.host=mongodb
The reason for this is that name of the container can be used as hostname if we connect to the network in which the container is placed.
Rebuild the docker image for the Java application, run it as such:
docker run --network=mongodb_network -p 8080:8080 <docker-image-name>
(fill in your docker image name there)
While these steps might work, I suggest to add the your application to the docker-compose:
version: "3.8"
services:
mongodb:
image: mongo
container_name: mongodb
expose:
- 27017
ports:
- 27017:27017
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=rootuser
- MONGO_INITDB_ROOT_PASSWORD=rootpass
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
- ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
- ME_CONFIG_MONGODB_SERVER=mongodb
spring_application:
build: .
container_name: spring_application
ports:
- 8080:8080
depends_on:
mongodb:
condition: service_started
volumes:
data: {}
networks:
default:
name: mongodb_network
This has to be build first with docker-compose build and afterwards can be executed with docker-compose up.
You must use docker internal host network to connect with mongodb. So from your dockerized application you have to connect with the service name (as that is the hostname) instead of connecting with localhost. In your case: http://mongodb:27017 is the service (and thus hostname) of your mongo db container.