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?

1

1 Answer 1

10

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
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
@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. (24.3)
@RomanT The build property can be used to refer to your Dockerfile, so Docker compose can be used to 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.