将letsencrypt文件复制到docker容器

将letsencrypt文件复制到docker容器,docker,docker-compose,lets-encrypt,certbot,Docker,Docker Compose,Lets Encrypt,Certbot,我不是docker的专家,我只是习惯了。我想将主机上生成的ssl证书复制到docker容器中。我读到它应该能够处理docker compose文件中的volumes参数,但启动我的服务器时,它总是很兴奋,因为它无法在工作目录中找到复制的文件 文件夹结构 - repo - backend - api - static - ssl - dockerfile - frontend - docker-compose

我不是docker的专家,我只是习惯了。我想将主机上生成的ssl证书复制到docker容器中。我读到它应该能够处理docker compose文件中的
volumes
参数,但启动我的服务器时,它总是很兴奋,因为它无法在工作目录中找到复制的文件

文件夹结构

- repo
   - backend
      - api
         - static
            - ssl
         - dockerfile
   - frontend
   - docker-compose.yml
FROM node:14-alpine

ENV NODE_ENV=production SERVER_PORT_HTTP=80 SERVER_PORT_HTTPS=443

WORKDIR /usr/src/app

RUN npm install

COPY . .

EXPOSE ${SERVER_PORT_HTTP} ${SERVER_PORT_HTTPS}

CMD [ "npm", "run", "start" ]
version: "3"

services:
  api:
    build:
      context: ./backend/api
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /etc/letsencrypt/live/api.example.com:/static/ssl
    restart: unless-stopped
Dockerfile

- repo
   - backend
      - api
         - static
            - ssl
         - dockerfile
   - frontend
   - docker-compose.yml
FROM node:14-alpine

ENV NODE_ENV=production SERVER_PORT_HTTP=80 SERVER_PORT_HTTPS=443

WORKDIR /usr/src/app

RUN npm install

COPY . .

EXPOSE ${SERVER_PORT_HTTP} ${SERVER_PORT_HTTPS}

CMD [ "npm", "run", "start" ]
version: "3"

services:
  api:
    build:
      context: ./backend/api
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /etc/letsencrypt/live/api.example.com:/static/ssl
    restart: unless-stopped
Docker Compose

- repo
   - backend
      - api
         - static
            - ssl
         - dockerfile
   - frontend
   - docker-compose.yml
FROM node:14-alpine

ENV NODE_ENV=production SERVER_PORT_HTTP=80 SERVER_PORT_HTTPS=443

WORKDIR /usr/src/app

RUN npm install

COPY . .

EXPOSE ${SERVER_PORT_HTTP} ${SERVER_PORT_HTTPS}

CMD [ "npm", "run", "start" ]
version: "3"

services:
  api:
    build:
      context: ./backend/api
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /etc/letsencrypt/live/api.example.com:/static/ssl
    restart: unless-stopped

你做的一切都对,文件的问题。如果查看它们,您会发现它们不是普通文件,而是链接:

root@fbe56bc38ad6:/# ls /etc/letsencrypt/live/example.com/ -l
total 4
-rw-r--r-- 1 root root 692 Jul 24  2020 README
lrwxrwxrwx 1 root root  44 Mar 22 00:03 cert.pem -> ../../archive/example.com/cert5.pem
lrwxrwxrwx 1 root root  45 Mar 22 00:03 chain.pem -> ../../archive/example.com/chain5.pem
lrwxrwxrwx 1 root root  49 Mar 22 00:03 fullchain.pem -> ../../archive/example.com/fullchain5.pem
lrwxrwxrwx 1 root root  47 Mar 22 00:03 privkey.pem -> ../../archive/example.com/privkey5.pem
lrwxrwxrwx 1 root root  42 Mar  1 12:57 example.com -> /etc/letsencrypt/live/example.com
lrwxrwxrwx 1 root root  33 Mar  1 12:57 ssl-dhparams.pem -> /etc/letsencrypt/ssl-dhparams.pem
因此,您安装了一组指向不存在位置的相对链接

我建议您在容器中装载
/etc/letsencrypt
/etc/letsencrypt

卷:
-/etc/letsencrypt:/etc/letsencrypt
然后让您的应用程序在
/etc/letsencrypt/live/example.com
中查找文件,或者在
/static/ssl
上创建另一个指向
/etc/letsencrypt/live/example.com
的链接:

ln -s /etc/letsencrypt/live/example.com /static/ssl

实际的错误是什么?是什么使您的应用程序知道如何查看
/static/ssl
目录;如果在构建阶段不强制这样做,您可以使用@DavidMaze是的,我想通过让nodejs处理ssl来降低复杂性-与其他容器和nginx相比。我告诉我的服务器在项目文件夹
/static/ssl
中查找证书。在我的主机上,我正在运行一个cronjob,它将证书续订到
/etc/letsencrypt/live/api.example.com
,但它不会将证书复制或链接到容器。因此,我得到'Error:enoint:no-this-file或directory,open./static/ssl/privkey.pem''Error.
/static/ssl
相对于当前目录与根目录中的
/static/ssl
不同。尝试将装载目标更改为
/usr/src/app/static/ssl
。感谢您提供此解决方案。它是开箱即用的,唯一的问题是我必须把绝对路径放在我的服务器配置中,以便像你提到的那样加载证书。你能给我举个例子,说明我如何在docker compose文件中根据你的答案创建第二个链接吗?@nixn恐怕你不能用docker compose管理链接(至少不容易)。您可以使用Dockerfile创建一个:运行mkdir/static&&ln-s/etc/letsencrypt/live/example.com/static/ssl。