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).
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.