使用 docker-compose.yml 中的卷构建

build with volumes in the docker-compose.yml

我想 运行 使用 NGINX 的多个 Web 应用程序映像。

所以我写了 docker-compose.yml 来构建 nginx 镜像和 运行 nodejs 容器。

我有 letsencrypt 颁发的 SSL 证书。

证书文件位于/etc/letsencrypt/live/mydomain.com/

我希望 NGINX 容器读取文件。

所以,我将 volumes: - /etc/letsencrypt/live/mydomain.com/:/etc/cert:ro 附加到 docker-compose.yml

但是nginx.conf无法读取文件。

我发现目录 /etc/cert 不存在,它被挂载为绑定类型。

我想知道如何在 docker-compose.yml 文件中设置卷以读取容器内部。

docker-compose.yml

version: '2.0'

services:  
  nginx:
    container_name: manager
    build: ./nginx
    links:
      - app-1:app-1
      ...
    ports:
      - 80:80
      - 443:443
    volumes:
      - /etc/letsencrypt/live/mydomain.com/:/etc/cert:ro
    depends_on:
      - app-1
      ...

  app-1:
    container_name: audio-1
    image: audio:test
    ports:
      - 80

  ...

nginx.conf

worker_processes 4;

events { worker_connections 1024; }

http {  
  upstream node-app {
    least_conn;
    server app-1:80 weight=10 max_fails=3 fail_timeout=60s;
    ...
  }

  server {
    listen 80;
    return 301 https://$host$request_uri;
  }

  server {
    listen 443 ssl;
    ssl_certificate /etc/cert/fullchain.pem;
    ssl_certificate_key /etc/cert/privkey.pem;
    location / {
      proxy_pass http://node-app;
      ...
    }
  }
}

错误

nginx: [emerg] BIO_new_file("/etc/cert/fullchain.pem") failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('/etc/cert/fullchain.pem','r') error:2006D080:BIO routines:BIO_new_file:no such file)

$docker视察经理

...
"Mounts": [
  {
    "Type": "bind",
    "Source": "/etc/letsencrypt/live/luvber.kr",
    "Destination": "/etc/cert",
    "Mode": "ro",
    "RW": false,
    "Propagation": "rprivate"
   }
 ],
...

谢谢

问题是该卷将在构建操作完成后安装。这就是为什么这种方法不适合你的原因。

您需要做的是将这些资源复制到 dockerfile 中的容器中。

假设您没有定义 dockerfile。您可以在制作 nginx 基础图像时创建自己的图像。

这看起来有点像....

From nginx:latest

COPY from/host/dir to/container/container/dir

类似但在不同上下文中他们正在解释 here

干杯!