使用 Docker 和 Docker Compose 无法确定执行上下文

使用 Docker 和 Docker Compose 无法确定执行上下文

我有以下docker-compose.yml:

version: '3'
services:
  frontend:
    build: 
      context: ./
      dockerfile: docker/Dockerfile
    image: tsl.web.frontend.image
    container_name: tsl.web.frontend.container
    ports:
      - "8080:80"

注意context: ./

这是我的 docker-compose 命令:

docker-compose build --force-rm --build-arg ENVIRONMENT=development frontend

这是我的 src 代码的结构:

在此处输入图片描述

请注意,我用红色下划线标出了该结构的两个重要方面。

现在这是我的 Dockerfile 的一部分,我将在下面包含整个 Dockerfile:

# https://stackoverflow.com/a/35774741/1258525
# use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
COPY ./package.json /tmp/package.json
RUN npm --prefix /tmp install /tmp
RUN cp -rn /tmp/node_modules ./
#RUN npm --prefix ./ run build-development ./
RUN npm run build-development #failing here

COPY dist /usr/share/nginx/html

因此RUN npm run build-development命令失败,因为它找不到 package.json:

在此处输入图片描述

这就是我对语句的执行上下文感到困惑的地方RUN。我能够在上一个 Dockerfile 命令中将 package.json 文件复制到 /tmp COPY ./package.json /tmp/package.json。我认为我的上下文仍然是我的 docker-compose.yml 文件所在的位置,但显然不是...

在此处输入图片描述

完整的Dockerfile:

FROM centos:7
MAINTAINER Brian Ogden

# Not currently being used but may come in handy
ARG ENVIRONMENT
ENV NODE_VERSION 6.11.1

RUN yum -y update; yum clean all
RUN yum -y install http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm; yum -y makecache
RUN yum -y install nginx-1.12.0

# Cleanup some default NGINX configuration files we don’t need
RUN rm /etc/nginx/conf.d/default.conf

COPY ./docker/conf/frontend.conf /etc/nginx/conf.d/frontend.conf
COPY ./docker/conf/nginx.conf /etc/nginx/nginx.conf

#############################################
# NodeJs Install
#############################################
RUN yum install -y \
        wget

#Download NodeJs package
RUN wget https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz

#extract the binary package into our system's local package hierarchy with the tar command. 
#The archive is packaged within a versioned directory, which we can get rid of by passing the --strip-components 1 option. 
#We will specify the target directory of our command with the -C command:
#This will install all of the components within the /usr/local branch
RUN tar --strip-components 1 -xzvf node-v* -C /usr/local
#############################################

# https://stackoverflow.com/a/35774741/1258525
# use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
COPY ./package.json /tmp/package.json
RUN npm --prefix /tmp install /tmp
RUN cp -rn /tmp/node_modules ./
#RUN npm --prefix ./ run build-development ./
RUN npm run build-development

COPY dist /usr/share/nginx/html

EXPOSE 8080

CMD ["nginx"]

答案1

您可以运行 id 为 4e14504665eb 的中间容器并探索文件系统。

$ docker run -it --rm 4e14504665eb  bash

或者你可以直接运行

$ docker run -it --rm 4e14504665eb  ls -la /tmp

但这些命令看起来有点奇怪

RUN npm --prefix /tmp install /tmp
RUN cp -rn /tmp/node_modules ./
RUN npm run build-development

您可能应该从 /tmp 目录运行 build 命令

PS 你的 Dockerfile 看起来不太理想。我建议使用更多优化版本

答案2

COPY我相信我误解的根源实际上是 Docker命令与命令的执行上下文RUN

我认为该命令在与 Dockerfile 或 docker-compose.yml 中所述的上下文RUN相同的执行上下文中运行,因为它处于这个上下文中。COPYCOPY

但事实并非如此,它基本上COPY知道./Dockerfile,RUN但是对于正在构建的 Docker 镜像之外的任何内容都一无所知。

我试图RUN对尚未复制到正在构建的 Docker 镜像中的文件执行命令。Dockerfile 的这一部分是成功的秘诀:

WORKDIR /app
COPY . /app

RUN npm run build-$ENVIRONMENT

下面是我的完整工作 Dockerfile,感谢 ALex_hha 进行的一些语法优化:

FROM centos:7
MAINTAINER Brian Ogden

# Not currently being used but may come in handy
ARG ENVIRONMENT
ENV NODE_VERSION 6.11.1

RUN yum -y update && \
    yum clean all && \
    yum -y install http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm \
    yum -y makecache && \
    yum -y install nginx-1.12.0 wget

# Cleanup some default NGINX configuration files we don’t need
RUN rm /etc/nginx/conf.d/default.conf

#############################################
# NodeJs Install
#############################################

#Download NodeJs package
RUN wget -q -O - https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz \
    | tar --strip-components=1 -xzf - -C /usr/local
#pipe tar is extracting the binary package into our system's local package hierarchy with the tar command. 
#The archive is packaged within a versioned directory, which we can get rid of by passing the --strip-components 1 option. 
#We will specify the target directory of our command with the -C command:
#This will install all of the components within the /usr/local branch

# https://stackoverflow.com/a/35774741/1258525
# use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
COPY ./package.json /tmp/package.json
RUN cd /tmp && npm install
RUN mkdir /app && cp -a /tmp/node_modules /app/

WORKDIR /app
COPY . /app

RUN npm run build-$ENVIRONMENT

RUN cd /app && cp -a dist/* /usr/share/nginx/html
COPY ./docker/conf/frontend.conf /etc/nginx/conf.d/frontend.conf
COPY ./docker/conf/nginx.conf /etc/nginx/nginx.conf


EXPOSE 80

CMD ["nginx"]

相关内容