Dockerising鹈鹕项目

时间:2019-04-16 07:35:30

标签: docker docker-compose dockerfile pelican

我正在尝试将我的Pelican网站项目进行泊坞化。我创建了一个 docker-compose.yml 文件和一个 Dockerfile

但是,每次尝试构建项目(docker-compose up)时,pip安装和npm安装都会出现以下错误:

npm WARN saveError ENOENT: no such file or directory, open '/src/package.json'
...
Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'

项目的目录结构如下:

 - **Dockerfile**
 - **docker-compose.yml**
 - content/ 
 - pelican-plugins/ 
 - src/
   - Themes/
   - Pelican config files
   - requirements.txt
   - gulpfile.js
   - package.js

所有鹈鹕makefile等都在src目录中。

我正在尝试将内容,src和pelican-plugins目录作为卷加载,以便可以在本地计算机上对其进行修改以供Docker容器使用。

这是我的 Dockerfile

FROM python:3

WORKDIR /src
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential

# Install Node.js 8 and npm 5
RUN apt-get update
RUN apt-get -qq update
RUN apt-get install -y build-essential
RUN apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash
RUN apt-get install -y nodejs

# Set the locale
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8

RUN npm install
RUN python -m pip install --upgrade pip

RUN pip install -r requirements.txt

ENV SRV_DIR=/src
RUN chmod +x $SRV_DIR

RUN make clean
VOLUME /src/output
RUN make devserver
RUN gulp

这是我的 docker-compose.yml 文件:

version: '3'
services:
  web:
    build: .
    ports:
    - "80:80"
    volumes:
    - ./content:/content
    - ./src:/src
    - ./pelican-plugins:/pelican-plugins
volumes:
  logvolume01: {}

肯定看起来我已经在dockerfiles中正确设置了卷目录...

谢谢!

1 个答案:

答案 0 :(得分:1)

您的Dockerfile根本没有COPY(或ADD)任何文件,因此/src目录为空。

您可以自己验证。当您运行docker build时,它将输出如下输出:

Step 13/22 : ENV LC_ALL en_US.UTF-8
 ---> Running in 3ab80c3741f8
Removing intermediate container 3ab80c3741f8
 ---> d240226b6600
Step 14/22 : RUN npm install
 ---> Running in 1d31955d5b28
npm WARN saveError ENOENT: no such file or directory, open '/src/package.json'

每一步中只有十六进制数字的最后一行实际上是有效的图像ID,这是运行每一步的最终结果,然后您可以:

% docker run --rm -it d240226b6600 sh
# pwd
/src
# ls

要解决此问题,您需要在Dockerfile中添加一行,例如

COPY . .

您可能还需要切换到src子目录以运行npm install之类的,如您所显示的目录布局。看起来像这样:

WORKDIR /src
COPY . .
# Either put "cd" into the command itself
# (Each RUN command starts a fresh container at the current WORKDIR)
RUN cd src && npm install
# Or change WORKDIRs
WORKDIR /src/src
RUN pip install -r requirements.txt
WORKDIR /src

请记住,Dockerfile中的所有内容都在docker-compose.yml块之外的build:中进行任何设置之前发生。容器的环境变量,卷安装和网络选项对映像构建顺序没有影响。

就Dockerfile样式而言,您的VOLUME声明将具有一些棘手的意外副作用,并且可能是不必要的。我将其删除。您的Dockerfile也缺少容器应运行的CMD。您还应该将RUN apt-get update && apt-get install组合成单个命令;通过Docker层缓存的工作方式和Debian存储库的工作方式,很容易得出一个缓存的软件包索引,该索引为一周前不再存在的文件命名。

虽然您描述的设置相当流行,但它实际上也隐藏了Dockerfile使用本地源代码树所做的所有 。例如,您在此处描述的npm install将是空操作,因为卷安装将隐藏/src/src/node_modules。我通常发现,在开发过程中,在本地运行pythonnpm会更容易,而不是编写和调试此50行的YAML文件并运行{ {1}}。

相关问题