I have a folder which already contains images. The user can add/update/delete the image with the object.
Problem is when I dockerize my application, the user who get my docker image it gets the folder with images (I have a folder where are stored image.png) too? If not how can I make sure it gets the folder too inside docker.
Here is my dockerfile:
FROM openjdk:17
ENV APP_HOME=/usr/app/
WORKDIR $APP_HOME
ADD target/Nation-0.0.1-SNAPSHOT.jar target/springboot-docker-county.jar
EXPOSE 8090
CMD ["java", "-jar", "target/springboot-docker-county.jar"]
Here my docker compose:
services:
db:
image: mongo:latest
ports:
- 27017:27017
environment:
SPRING_DATA_MONGODB_URI: mongodb://host.docker.internal:27017
MONGO_INITDB_ROOT_USERNAME: user
MONGO_INITDB_ROOT_PASSWORD: pass
volumes:
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:r
web:
build: ./WorldBE/target/classes/county-fe
ports:
- 80:80
api:
build: ./WorldBE
ports:
- 8090:8090
volumes:
- ./images/:/usr/app/application/images
volumes:
county:
Here my application.yml to get the folder:
spring:
web:
resources:
static-locations: /usr/app/application/images
Here how can I perform the add method (it works):
public void saveFile(String region, Integer id, Binary image) throws Exception {
Path url = Paths.get( pathToSave);
if (image != null) {
File fileToSave = new File(url +"/"+ this.regional(region, id) + ".png");
if (fileToSave.exists()) {
log.error("Image for {} and regional: {} already exist! file: {}", id, region, fileToSave);
throw new Exception(String.format("Image for : %s and region: %s already exist!", id, region));
}
if (!fileToSave.exists()) {
log.info("file doesn't exist: {}", fileToSave);
}
// Try-with-resource
try (OutputStream out = new FileOutputStream(fileToSave)) {
out.write(image.getData());
out.flush();
log.info("file saved: {}", fileToSave);
} catch (IOException e) {
e.printStackTrace();
}
} else {
log.error("No image set");
}
The user get the mount (folder with image.png) already populate when it pulls the image or should I do something else to store this folder in docker?
2
Answers
In spring-boot, you can expose the endpoint as a static resource with the following properties:
Please note the
file
prefix when definingstatic-locations
Move the folder with the images in your resource/static folder, than in your docker file (after MKDIR) add:
and delete from your docker-compose: