本文介绍了如何使用docker-compose将docker容器目录映射到主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下docker-compose.yml设置:

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

结果 ./ config 文件夹映射到容器 nginx-reverse-proxy ,因此在容器上的空 / etc / nginx 目录中。

Results in ./config the folder beeing mapped to the container nginx-reverse-proxy and therefore in an empty /etc/nginx directory on the container.

如我的问题所述,目标是查看内容(从图像中创建)的容器,并使该内容对主机可见 ./ config

As stated in my question the goal is to see the content, the container (from the image) creates, and make it visible to the host at ./config.

我当前的搜索不断导致如何将Directoy从主机映射到容器(

My current search constantly results in how to map directoy from host to container (which i do not want).

任何提示/解决方案赞赏。

Any hints/solutions are appreciated. Thanks!

我当前的解决方案很丑:
我用docker创建了容器,并从 / etc / nginx复制了文件 ./ config 。删除容器并使用 docker-compose up 可以正常工作,并且 nginx 开始,因为所需的文件已在主机上。

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.

编辑:该文件夹在创建时不存在。 Docker compese正在按照文档中的说明创建文件夹。

The folder is not present at creation. Docker compese is creating the folder as stated in the docs.

推荐答案

从以下位置读取:

新卷可以通过容器预先填充其内容。

New volumes can have their content pre-populated by a container.




因此,一个简单的docker-compose(在名为nginx的文件夹中):


So a simple docker-compose (inside a folder called nginx):

version: "3.7"
volumes:
  xmpl:

services:
  testnginx:
    image: nginx:stable
    volumes:
      - xmpl:/etc/nginx

将通过以下方式产生 host 系统上的所有文件:

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

最后在 ./ config中使用它。 em>:

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

这篇关于如何使用docker-compose将docker容器目录映射到主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:47