Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a docker containers directory to the host with docker-compose?

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.

like image 815
Chrizzldi Avatar asked Dec 03 '19 14:12

Chrizzldi


People also ask

How do I map a directory in Docker?

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.

How do I copy a directory from container to host?

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.

How do you communicate between containers in Docker compose?

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.


1 Answers

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
like image 181
Tyhal Avatar answered Sep 20 '22 12:09

Tyhal