5

I've got a spring boot application which uses com.spotify.dockerfile-maven-plugin to build a docker image of my application org.rtu/some-importer

My docker-compose.yml is:

version: '3'
services:
  some-importer:
    image: org.rtu/some-importer
    build: .
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
  kafka: 
    image: wurstmeister/kafka
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 172.17.0.1
      KAFKA_CREATE_TOPICS: "test:1:1"
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

How can I say the during docker-compose up that it should be used an external config.properties from /data/some-importer/config folder?

Roman T
  • 1,400
  • 5
  • 18
  • 31
  • Possible duplicate of [How to mount host directory in docker container?](https://stackoverflow.com/questions/23439126/how-to-mount-host-directory-in-docker-container) – Yannic Bürgmann Sep 20 '17 at 08:57

1 Answers1

9

As mentioned in the comments, the first step is to mount a host directory in a docker container (just like you did for Kafka). For example, you can use:

version: '3'
services:
  some-importer:
    image: org.rtu/some-importer
    build: .
    # Adding a volume/mount
    volumes:
      - /data/some-importer/config:/config

This will map the /data/some-importer/config folder to /config in your Docker container.

NOTE: The linked answer also mentions that you can add it within your Dockerfile using ADD. However, this will add it to the image itself. If you make a change to the configuration, you'll have to rebuild your image to make those changes work.

The next step is to tell Spring boot to use this configuration file. If you want a completely customised location (eg. /config/config.properties), then you can use the spring.config.location parameter during startup.

NOTE: Spring boot will automatically pick up your configuration if it's located in certain folders. Otherwise you'll have to configure it with spring.config.location.

I don't know how your image looks like, but you should be able to do something like this:

ENTRYPOINT [ "sh", "-c", "java -jar /app.jar --spring.config.location=$CONFIG_LOCATION" ]

I'm using an environment variable called $CONFIG_LOCATION here, which makes it easier to customise the location by using environment variables. For example, you can add the following in your docker-compose.yml file:

version: '3'
services:
  some-importer:
    image: org.rtu/some-importer
    build: .
    volumes:
      - /data/some-importer/config:/config
    # Configuring the environment variable
    environment:
      - CONFIG_LOCATION=file:/config/config.properties
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
  • thank you so much, I will try it. I meant actually `application.properties`, I think such file would be read automatically by spring-boot from config folder. – Roman T Sep 20 '17 at 09:28
  • btw, you explain docker so great and one more question. Do you know what the sacral meaning of `build: . ` in the docker-compose? I.e in my example. – Roman T Sep 20 '17 at 09:29
  • 1
    @RomanT it depends. Spring boot will automatically read `application.properties` if it's placed within certain directories. You can find the list in the [Documentation](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files). (24.3) – g00glen00b Sep 20 '17 at 09:31
  • 1
    @RomanT The [build](https://docs.docker.com/compose/compose-file/#build) property can be used to refer to your `Dockerfile`, so [Docker compose can be used to build](https://docs.docker.com/compose/reference/build/) the image(s). You don't really need it, since you already have the Maven plugin, and the artifacts required by Docker (eg. your `.jar` file) is also generated by Maven. – g00glen00b Sep 20 '17 at 09:42