I have the following Docker project running on a Linux VM. When I log into localhost, thanks to the website, I can save a photo on the Tomcat container on the server. I want to access the images uploaded to Tomcat to make a backup. How can I do? Why is my code not working?
- ./volumes/tomcat:/usr/local/tomcat/webapps/
Below I report all my project:
/home/gi/Dropbox/SERVER/DOCKER-LINUX/ServerDocker02/https/localhost-rsa-cert.pem
/home/gi/Dropbox/SERVER/DOCKER-LINUX/ServerDocker02/https/localhost-rsa-key.pem
/home/gi/Dropbox/SERVER/DOCKER-LINUX/ServerDocker02/https/server.xml
/home/gi/Dropbox/SERVER/DOCKER-LINUX/ServerDocker02/tomcat/Dockerfile
FROM tomcat:9.0.63-jdk11
LABEL Author="Nome Cognome"
EXPOSE 8080
EXPOSE 8443
COPY ./serverfile/*.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]
/home/gi/Dropbox/SERVER/DOCKER-LINUX/ServerDocker02/docker-compose.yaml
version: '3.8'
services:
postgresql-postgis:
...
pgadmin:
...
tomcat:
build: ./tomcat
image: image-tomcat-eb:v.1.0
container_name: container-tomcat-eb
ports:
- 6003:8080
- 7003:8443
volumes:
- ./https/localhost-rsa-cert.pem:/usr/local/tomcat/ssl/localhost-rsa-cert.pem
- ./https/localhost-rsa-key.pem:/usr/local/tomcat/ssl/localhost-rsa-key.pem
- ./https/server.xml:/usr/local/tomcat/conf/server.xml
- ./volumes/tomcat:/usr/local/tomcat/webapps/
depends_on:
- postgresql-postgis
- pgadmin
restart: on-failure
networks:
- eb
mysql:
...
phpmyadmin:
...
php:
...
volumes:
data-postgresql:
data-mysql:
networks:
eb:
Related
I am building a Spring Boot application which uses PostgreSQL with docker-compose.
When I run my containers using docker-compose up --build, my Spring Boot application fails to start because it does not find the PostgreSQL container's hostname.
Spring Boot Dockerfile
FROM maven:3.6.3-openjdk-14-slim AS build
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean package
FROM openjdk:14-slim
COPY --from=build /usr/src/app/target/server-0.0.1-SNAPSHOT.jar /usr/app/server-0.0.1-SNAPSHOT.jar
EXPOSE 9000
ENTRYPOINT ["java","-jar","/usr/app/server-0.0.1-SNAPSHOT.jar"]
docker-compose.yml
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: my_db
ports:
- "5432:5432"
volumes:
- db-data:/var/lib/postgresql/data
networks:
- db-network
restart: always
server:
build: './server'
depends_on:
- db
restart: always
ports:
- "9000:9000"
networks:
- db-network
volumes:
- ./server:/server
networks:
db-network:
volumes:
db-data:
application.properties
spring.datasource.url=jdbc:postgresql://db:5432/my_db
spring.datasource.username=postgres
spring.datasource.password=postgres
Error output
Caused by: java.net.UnknownHostException: db
My guess is that docker-compose's virtual network isn't created yet during the build stage of the Spring Boot Dockerfile.
Any idea on how to solve this issue ?
Lots of info here: https://docs.docker.com/compose/networking/
Within the web container, your connection string to db would look like
postgres://db:5432, and from the host machine, the connection string
would look like postgres://{DOCKER_IP}:8001.
What this is saying is db:5432 is fine to use within docker-compose.yaml and the IP address will be passed (not "db"), but using it externally within your application code isn't going to work. You could however pass from docker-compose.yaml db as an application input variable, which your application could fill in in the configuration file. This would enable you then to connect.
Externalising configuration like this is fairly common practice so should be a relatively easy fix.
eg:
Docker-compose.yaml
version: '3'
services:
db:
container_name: db
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: my_db
ports:
- "5432:5432"
volumes:
- db-data:/var/lib/postgresql/data
networks:
- db-network
restart: always
server:
build: './server'
depends_on:
- db
environment:
DB_HOST: db # untested, but there should be a way to pass this in
DB_PORT: 5432
DB_DATABASE: my_db
DB_USER: postgres
DB_PASSWORD: postgres
restart: always
ports:
- "9000:9000"
networks:
- db-network
volumes:
- ./server:/server
networks:
db-network:
volumes:
db-data:
Then have an application.properties file located under src/main/java/resources/application.properties
spring.datasource.url=jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_DATABASE}
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
This post completely solved my issue.
It turns out that maven was trying to establish the connection to the database while building the .jar file. All I had to do is modify my Dockerfile with this line RUN mvn -f /usr/src/app/pom.xml clean package -DskipTests.
Please do note while building the images the service will not have access to the database as it is not yet running . Only after the images are built and the containers are running do the services have access . So when you try to pass a host as db , it is not yet available in the build stage . It is available only once the db container starts running .
I have developed simple Spring Boot Application that performs CRUD operations using MongoDB as database. I have deployed that application in Docker but I get null values while doing GET Request for any items stored in MongoDB. Some of the files required for Docker are provided below:
Dockerfile:
VOLUME /tmp
ADD build/libs/Spring-Boot-MongoDB-0.0.1-SNAPSHOT.jar SpringMongoApp.jar
ENTRYPOINT ["java", "-Dspring.data.mongodb.uri=mongodb://mongo:27018/otp","-jar","/SpringMongoApp.jar"]
docker-compose.yml:
version: "3"
services:
api-database:
image: mongo:3.2.4
container_name: "springboot-mongo-app"
ports:
- "27018:27017"
environment:
MONGO_INITDB_ROOT_DATABASE: otp
networks:
- test-network
api:
image: springboot-api
ports:
- "8080:8080"
depends_on:
- api-database
networks:
- test-network
networks:
test-network:
driver: bridge
application.properties:
spring.data.mongodb.host=api-database
When I checked the MongoDb Docker container using container ID, it is automatically getting connected to test database but not to otp database which I have mentioned in environment section of docker-compose.yml file.
The problem is that your docker container is not persistent, the database will be erased and re-created again each time you run your docker container.
If you add VOLUME to persist /data/db, you will get desired result.
I assume your have directory data/db in the same place where you have stored docker-compose.yml. You may setup custom directory (i.e /tmp/data/db)
Try this one:
docker-compose.yml:
version: "3"
services:
api-database:
image: mongo:3.2.4
container_name: "springboot-mongo-app"
ports:
- "27018:27017"
volumes:
- "./data/db:/data/db"
environment:
MONGO_INITDB_ROOT_DATABASE: otp
networks:
- test-network
api:
image: springboot-api
ports:
- "8080:8080"
depends_on:
- api-database
networks:
- test-network
networks:
test-network:
driver: bridge
Note: First time, it will be empty database. If you create collections, insert records, etc... it will be saved in ./data/db
I think instead of MONGO_INITDB_ROOT_DATABASE you should use MONGO_INITDB_DATABASE
I'm trying to develop application which have 3 microservices, postgres database and eureka server for discovering clients. Locally all works perfect. The problem appears when i'm deploying services on docker containers. One container per service, for postgres and for eureka. I'm create a network in docker and join all containers. The question is what should I do to discover my microservices in eureka server? If anyone will want to help I will provide more details to resolve the problem.
That's my docker-compose
version: "3.7"
networks:
default:
name: microservices_nw
driver: bridge
services:
postgresService:
image: postgres:latest
container_name: postgres
expose:
- 5432
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
- POSTGRES_PASSWORD=xxx
- POSTGRES_USER=xxx
- POSTGRES_DB=work
restart: unless-stopped
# EUREKA SERVER#####
eureka-server:
image: eureka-server:latest
container_name: eureka-server
expose:
- 8761
restart: unless-stopped
# APP*****************************************
credit-microservice:
image: credit-microservice:latest
container_name: credit-microservice
expose:
- 8600
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgresService/work
- SPRING_DATASOURCE_USERNAME=xxx
- SPRING_DATASOURCE_PASSWORD=xxx
restart: unless-stopped
product-micorservice:
image: product-microservice:latest
container_name: product-micorservice
expose:
- 8080
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgresService/work
- SPRING_DATASOURCE_USERNAME=xxx
- SPRING_DATASOURCE_PASSWORD=xxx
restart: unless-stopped
customer-microservice:
image: customer-microservice:latest
container_name: customer-microservice
expose:
- 8081
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgresService/work
- SPRING_DATASOURCE_USERNAME=xxx
- SPRING_DATASOURCE_PASSWORD=xxx
- EUREKA_INSTANCE_PREFER_IP_ADDRESS=true
- EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server/eureka
depends_on:
- eureka-server
restart: unless-stopped
Dockerfile for customer-microservice
FROM openjdk:8
ADD target/customer-service.jar customer-service.jar
EXPOSE 8081
CMD java -jar customer-service.jar
Dockerfile for product-microservice
FROM openjdk:8
ADD target/product-service.jar product-service.jar
EXPOSE 8080
CMD java -jar product-service.jar
Dockerfile for eureka-server
FROM openjdk:8
ADD target/eureka-server.jar eureka-server.jar
EXPOSE 8761
ENTRYPOINT ["java", "-jar", "eureka-server.jar"]
Dockerfile for credit-microservice
FROM openjdk:8-jre
ADD target/credit-service.jar credit-service.jar
EXPOSE 8600
ENTRYPOINT ["java","-jar", "credit-service.jar"]
And application.yml for customer-service
server:
port: 8081
eureka:
client:
service-url:
default-zone: ${EUREKA_SERVER:http://localhost:8761/eureka}
## PostgreSQL
spring:
application:
name: customer-service
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/work
username: xxx
password: xxx
jpa:
hibernate.ddl-auto: validate
properties:
hibernate.jdbc.lob.non_contextual_creation: true
I have two docker containers: mysql and spring boot service.
Service container is not connecting to mysql container(failing on the deploy) with following exception:
CommunicationsException: Communications link failure
So far I have tried using docker --link, docker networks, running everything using/without docker compose.
I am able to connect to DB container outside of docker.
Connection string: jdbc:mysql://db:3306/somedb
docker-compose.yml
version: '3.7'
services:
app:
build: .
restart: unless-stopped
ports:
- "80:8060"
links:
- db
depends_on:
- db
db:
image: "mysql:latest"
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_DATABASE=somedb
- MYSQL_ROOT_PASSWORD=somepassword
ports:
- "3306:3306"
volumes:
- /Users/someuser/someproject/mysql/data:/var/lib/mysql
I had the same problem earlier and i tried with by changing the mysql image version. Here is my compose yaml file where i am able to connect it with.
version: "3.0"
services:
spring-boot-container:
build: .
ports:
- "8086:8086"
links:
- mysql-standalone
mysql-standalone:
image: mysql:5.6
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: springsecurity
MYSQL_USER: sa
MYSQL_PASSWORD: password
So in my service dockerfile I say:
ADD target/app-1.0-SNAPSHOT.jar /app-service.jar
RUN sh -c 'touch /app-service.jar'
But I forgot to rebuild jar after making changes in service config files
TL;DR I'm retarded
I am trying to build a docker application with 3 containers:
troparo_app (web-service)
library_app (web-client)
troparo_db (postgres db)
Somehow, I am getting a 404 while trying to connect from the app to the webservice.
The webservice responds ok from host on:
http://localhost:8080/troparo_app/services
Tested it from SOAP UI and working just fine
for the web-client(library_app), I can't access the main page at:
http://localhost:8090/library-web-1.0-SNAPSHOT/login
but then I get a 404:
org.apache.cxf.transport.http.HTTPException: HTTP response '404: null' when communicating with http://localhost:8080/troparo_app/services/connect/ConnectService
My docker-compose below:
# Version of docker-compose
version: '3'
services:
# getting postgres DB
db:
image: postgres:11.1
#container_name: troparo_db
ports:
- 5432:5432
environment:
POSTGRES_USER: ocp
POSTGRES_PASSWORD: 123
POSTGRES_DB: troparo
POSTGRES_INITDB_ARGS: --data-checksums
PGDATA: /var/lib/postgresql/data/pgdata
volumes:
- pg-data:/var/lib/postgresql/data
networks:
- troparo
# getting troparo image
webservice:
image: troparo_app
container_name: troparo_app
build: .
ports:
- 8080:8080
depends_on:
- db
networks:
- troparo
# getting troparo image
webapp:
image: library_app
container_name: library_app
build: ../webo/library/.
ports:
- 8090:8080
depends_on:
- webservice
networks:
- troparo
volumes:
pg-data:
networks:
troparo:
Dockerfile for library_app:
FROM tomcat:9.0.14-jre8
RUN rm -rf /usr/local/tomcat/webapps/*
COPY ./library-web/target/library-web-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/library-web-1.0-SNAPSHOT.war
CMD ["catalina.sh","run"]
Dockerfile for troparo_app:
FROM tomcat:9.0.14-jre8
RUN rm -rf /usr/local/tomcat/webapps/*
COPY ./troparo-web/target/troparo_app.war /usr/local/tomcat/webapps/troparo_app.war
CMD ["catalina.sh","run"]
I connected to the client container and ping the service -> ok
I tried several combination for connecting to the webservice:
http://troparo_app:8080
http://localhost:8080
but all fails so far.
I am new to docker so I imagine there is something I forgot to configure in the network but I couldn't figure it out so far so if anyone can help, I d be really grateful !!
Thanks
After some more thoughts / testing, I finally have it working!!
The webservice is simply accessible on http://webservice:8080