1

I want to know how to configure a docker setup (docker-compose) in order to supply a configuration file which is consumed by my Spring boot application.

The configuration file is called services.xml which resides in the applications's /lib/conf directory. The file is deployed with the default configuration, however I want the file in host so that whenever I need to change the configuration, I should edit in host and the container would read the updated file.

The docker-compose.yml

version: '3.1'

services:

  my-app:
    image: my-app
    container_name: my-app
    # restart: always
    ports:
      - 8443:8443
    volumes:
      - ./my-app/conf:/opt/lib/my-app/lib/conf:rw

Expected results after running: docker-compose up

I expect this should create the directory, copy the default services.xml (along with all other files in /opt/lib/my-app/lib/conf) in container into this directory to make it available for me to edit.

Actual results After running docker-compose, it creates an empty directory inside the my-app directory. The my-app fails to read the services.xml file and app doesn't start (as it depends on this file).

5
  • have you tried using a absolute path to the file? Commented Aug 4, 2019 at 19:22
  • Not yet. How is it different from mapping directories to host and container? Commented Aug 4, 2019 at 19:24
  • @Jocke, I am getting error: mounting \\\"/home/ubuntu/my-app/conf/services.xml\\\" to rootfs \\\"/var/lib/docker/overlay2/87ecda1732ce960a75263ca499b880d29550a70f969f06ba14ce79d1c5e06485/merged\\\" at \\\"/var/lib/docker/overlay2/87ecda1732ce960a75263ca499b880d29550a70f969f06ba14ce79d1c5e06485/merged/opt/lib/my-app/lib/conf/services.xml\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type Commented Aug 4, 2019 at 19:30
  • 1
    According to stackoverflow.com/questions/52711305/…, it is not possible to mount individual files in docker. Commented Aug 4, 2019 at 19:52
  • Seems, you would rather need to use a configuration-server than trying to map configurations out of the container. If you really need to do this, this stackoverflow.com/questions/30652299/… would be helpful. I don't see, this is the right way anyway. Commented Aug 5, 2019 at 6:10

1 Answer 1

1

I expect this should create the directory, copy the default services.xml (along with all other files in /opt/lib/my-app/lib/conf) in container into this directory to make it available for me to edit.

From you said above, if your aim is to let the contents in container be pop to host & let you have chance to modify them, then I suggest you to use named volumes. But, the folder in host will be managed by docker itself, so you need to find where they are located.

A minimal example for your reference:

docker-compose.yaml(In my example it located in the folder 77):

version: '3'
services:
  frontend:
    image: alpine
    command: "tail -f /dev/null"
    volumes:
      - my_data:/etc

volumes:
  my_data:

Start the service:

shubuntu1@shubuntu1:~/77$ docker-compose up -d
Creating network "77_default" with the default driver
Creating volume "77_my_data" with default driver
Creating 77_frontend_1 ... done

Check the location of named volume in host:

shubuntu1@shubuntu1:~/77$ docker ps
CONTAINER ID    IMAGE     COMMAND               CREATED             STATUS          PORTS     NAMES
6635aba545c9    alpine    "tail -f /dev/null"   14 minutes ago      Up 14 minutes             77_frontend_1
shubuntu1@shubuntu1:~/77$ docker inspect 77_frontend_1 | grep Source
            "Source": "/var/lib/docker/volumes/77_my_data/_data",

Check the content of original /etc/profile in container:

shubuntu1@shubuntu1:~/77$ docker exec 77_frontend_1 cat /etc/profile
export CHARSET=UTF-8
export LANG=C.UTF-8
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PAGER=less
export PS1='\h:\w\$ '
umask 022

for script in /etc/profile.d/*.sh ; do
    if [ -r $script ] ; then
            . $script
    fi
done

Modify the script from host:

shubuntu1@shubuntu1:~/77$ sudo -s -H
root@shubuntu1:/home/shubuntu1/77# cd /var/lib/docker/volumes/77_my_data/_data
root@shubuntu1:/var/lib/docker/volumes/77_my_data/_data# echo 'echo "hello"' >> profile

Check again the /etc/profile in container after we made changes on host:

shubuntu1@shubuntu1:~/77$ docker exec 77_frontend_1 cat /etc/profile
export CHARSET=UTF-8
export LANG=C.UTF-8
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PAGER=less
export PS1='\h:\w\$ '
umask 022

for script in /etc/profile.d/*.sh ; do
    if [ -r $script ] ; then
            . $script
    fi
done
echo "hello"

We can see echo "hello" which we add on host already be seen in container.

Sign up to request clarification or add additional context in comments.

1 Comment

This solution is working. Thanks for the detailed answer.

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.