How to dynamically edit a file in a running docker container
Background
I have built an npm server docker image (synopsy) ( https://github.com/feuyeux/docker-atue/blob/master/docker-images/feuyeux_sinopia.md ) and in the CMD line it will run start.sh every time it is created container.
CMD ["/opt/sinopia/start.sh"]
This wrapper will dynamically create the yaml file.
sed -e 's/\#listen\: localhost/listen\: 0.0.0.0/' -e 's/allow_publish\: admin/allow_publish\: all/' /tmp/config.yaml > /opt/sinopia/config.yaml
Question
I would like to change this config.yaml when the container is running, because I hope the content should be changed on demand.
As shown above, the first line starts container Sinop , in which the container is /opt/sinopia/config.yaml. But I don't know how to get this running container, edit and check this file. If I did as the sinopia-ls line , a new container will start there, not before starting it.
Thanks guys!
Answer (see below for details which I accepted)
sudo nsenter --target $PID --mount --uts --ipc --net --pid
root@58075317e47d:/# ls /opt/sinopia/
config.yaml config_gen.js start.sh storage
root@58075317e47d:/# cat /opt/sinopia/config.yaml
source to share
With docker 1.3 there is a new command docker exec
. This lets you inject a running docker:
docker exec -it <container-id> bash
source to share
You named your container so that it can be found using that name.
Then use nsenter ( man nsenter ) to send the command you want to do.
nsenter --target $$(docker inspect --format {{.State.Pid}} <container_name_or_ID>) --mount --uts --ipc --net --pid <cmd>
More info and solution on how to write inside a running container: If you run SSHD in your Docker containers, you're wrong!
source to share
you just need to install the folder with -v as an option. I will give an example
-
let's say i have
/home/awan/config.yml
<--- this file is always dynamic shouldn't put it in container -
I started a container to mount this folder in my container
#sudo docker run -i -t -v /home/awan:/home/ubuntu/awan ubuntu/14.04 /bin/bash
- after that you just edit
config.yml
in yours/home/awan/config.yml
all the changes you automatically apply in your docker container (/home/ubuntu/awan/config.yml
) because you are mounting it
source to share