Search code examples
spring-bootdockerdeploymentjarproperties-file

how to overwrite property files in a docker container?


I have a docker container inside which I have jar file that is of my springBoot application . How can I overwrite the application.property file of my app. Suppose if I want to change datasource url ,how can I do that through commandline?


Solution

    1. Run docker ps and note the initials of id of your container.
    2. Run docker exec -it <id> <bash or sh>, you will be inside your running container.
    3. Go to your jar directory. Place your properties file in config/ as application.properties.
    4. Rerun your app, but make sure to do it in background, so you can exit docker.

    You can't restart the primary process (PID 1). For that usecase you'll need external volume and restart the container on property file change.

    1. Create a volume: docker volume create vol1, you will get /var/lib/docker/volumes/vol1/
    2. Change group of directory sudo chgrp 1001 /var/lib/docker/volumes/vol1/_data
    3. Add write access to group: sudo chmod g+w /var/lib/docker/volumes/vol1/_data
    4. Check the permissions of the directory: sudo ls -al /var/lib/docker/volumes/vol1/. They should be: drwxrwxr-x
    5. Next time you run your container, mount the folder containing jar file to your volume as: docker run -d --name=webApp1 --mount source=vol1,destination=/path/to/app -p port:port name
    6. You can change the property file in /var/lib/docker/volumes/vol1/_data/ and restart container: docker restart container_name

    Disclaimer: Replacing properties file outside jar is not considered a good practice and is highly discouraged. So, these steps are only recommended for testing.