Sun*_*arg 10 docker docker-compose
这是项目结构
Project
/deployment
/Dockerfile
/docker-compose.yml
/services
/ui
/widget
Run Code Online (Sandbox Code Playgroud)
这是泊坞窗文件
FROM node:14
WORKDIR /app
USER root
# create new user (only root can do this) and assign owenership to newly created user
RUN echo "$(date '+%Y-%m-%d %H:%M:%S'): ======> Setup Appusr" \
&& groupadd -g 1001 appusr \
&& useradd -r -u 1001 -g appusr appusr \
&& mkdir /home/appusr/ \
&& chown -R appusr:appusr /home/appusr/\
&& chown -R appusr:appusr /app
# switch to new created user so that appuser will be responsible for all files and has access
USER appusr:appusr
COPY ../services/ui/widget/ /app/
COPY ../.env /app/
# installing deps
RUN npm install
Run Code Online (Sandbox Code Playgroud)
和 docker-compose
版本:“3.4”
x-env: &env
HOST: 127.0.0.1
services:
widget:
build:
dockerfile: Dockerfile
context: .
ports:
- 3002:3002
command:
npm start
environment:
<<: *env
restart: always
Run Code Online (Sandbox Code Playgroud)
从中可以project/deplyment/docker-compose up看出
Step 6/8 : COPY ../services/ui/widget/ /app/
ERROR: Service 'widget' failed to build : COPY failed: forbidden path outside the build context: ../services/ui/widget/ ()
Run Code Online (Sandbox Code Playgroud)
我是否设置了错误的上下文?
Sae*_*eed 11
您不能COPY删除ADD当前路径之外Dockerfile存在的文件。
您应该将这两个目录移动到位置Dockerfile,然后更改Dockerfile为:
COPY ./services/ui/widget/ /app/
COPY ./.env /app/
Run Code Online (Sandbox Code Playgroud)
或者使用volumesin docker-compose,并删除这两COPY行。
所以,你的docker-compose应该看起来像这样:
x-env: &env
HOST: 127.0.0.1
services:
widget:
build:
dockerfile: Dockerfile
context: .
ports:
- 3002:3002
command:
npm start
environment:
<<: *env
restart: always
volumes:
- /absolute/path/to/services/ui/widget/:/app/
- /absolute/path/to/.env/:/app/
Run Code Online (Sandbox Code Playgroud)
这应该是你的Dockerfile if you use 卷indocker-compose`:
FROM node:14
WORKDIR /app
USER root
# create new user (only root can do this) and assign owenership to newly created user
RUN echo "$(date '+%Y-%m-%d %H:%M:%S'): ======> Setup Appusr" \
&& groupadd -g 1001 appusr \
&& useradd -r -u 1001 -g appusr appusr \
&& mkdir /home/appusr/ \
&& chown -R appusr:appusr /home/appusr/\
&& chown -R appusr:appusr /app
# switch to new created user so that appuser will be responsible for all files and has access
USER appusr:appusr
# installing deps
RUN npm install
Run Code Online (Sandbox Code Playgroud)
vse*_*rgi 10
您的问题是您正在引用 Dockerfile 上下文之外的文件。默认情况下,是执行构建命令的位置。
来自 docker 文档 -复制部分:
该路径必须位于构建的上下文内;你不能 COPY ../something /something,因为 docker 构建的第一步是将上下文目录(和子目录)发送到 docker 守护进程。
但是,您可以使用参数 -f 独立于运行构建的文件夹来指定 dockerfile。因此,您可以使用下一行从项目中执行它:
docker build -f ./deployment/Dockerfile .
Run Code Online (Sandbox Code Playgroud)
您还需要修改复制线以指向正确的位置。
COPY ./services/ui/widget/ /app/
COPY ./.env /app/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37915 次 |
| 最近记录: |