Given following docker-compose.yml setup:
version: '3.7'
services:
reverse:
container_name: nginx-reverse-proxy
hostname: nginx-reverse-proxy
image: nginx:stable
ports:
- 80:80
- 433:433
volumes:
- type: bind
source: ./config
target: /etc/nginx
consistency: consistent
this results in ./config
folder being mapped to the container nginx-reverse-proxy
and therefore in an empty /etc/nginx
directory inside the container.
Goal:
Mapping a folder from container to host. In my example from container /etc/nginx
to the host ./config
.
My current search constantly results in mapping a directoy from host to container (which i do not want).
Any hints/solutions are appreciated. Thanks!
My current solution is ugly:
I created the container with docker and copied the files from /etc/nginx
to ./config
. Removing the container and using the docker-compose up
works and nginx
starts because the needed files are already on the host.
Edit: The folder is not present at creation. Docker compose is creating the folder as stated in the docs.
How to Mount Local Directories using docker run -v. Using the parameter -v allows you to bind a local directory. -v or --volume allows you to mount local directories and files to your container. For example, you can start a MySQL database and mount the data directory to store the actual data in your mounted directory.
To summarize, follow these steps to copy a file from a Docker container to a host machine: Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container.
For containers to communicate with other, they need to be part of the same “network”. Docker creates a virtual network called bridge by default, and connects your containers to it. In the network, containers are assigned an IP address, which they can use to address each other.
Reading from: Dockers Volume Page
Volumes have several advantages over bind mounts:
New volumes can have their content pre-populated by a container.
So a simple docker-compose (inside a folder called nginx):
version: "3.7"
volumes:
xmpl:
services:
testnginx:
image: nginx:stable
volumes:
- xmpl:/etc/nginx
Will yield all the files on the host system via:
$ docker-compose up
$ docker inspect nginx_xmpl
...
"Mountpoint": "/var/lib/docker/volumes/nginx_xmpl/_data"
And you can then view the files on the host:
# ls /var/lib/docker/volumes/nginx_xmpl/_data
conf.d fastcgi_params koi-utf koi-win
mime.types modules nginx.conf scgi_params
uwsgi_params win-utf
And finally to use it from ./config:
# ln -s /var/lib/docker/volumes/nginx_xmpl/_data ./config
# ls config
conf.d fastcgi_params koi-utf koi-win
mime.types modules nginx.conf scgi_params
uwsgi_params win-utf
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With