user791793
user791793

Reputation: 701

Dockerising pelican project

I'm trying to dockerise my pelican site project. I've created a docker-compose.yml file and a Dockerfile.

However, every time I try to build my project (docker-compose up) I get the following errors for both pip install and npm install:

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'

The directory structure of the project is as follows:

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

All the pelican makefiles etc. are in the src directory.

I'm trying to load the content, src, and pelican-plugins directories as volumes so I can modify them on my local machine for the docker container to use.

Here is my 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

And here is my docker-compose.yml file:

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

It definitely looks like I have set up my volumes directories properly in dockerfiles...

Thanks in advance!

Upvotes: 0

Views: 410

Answers (1)

David Maze
David Maze

Reputation: 159750

Your Dockerfile doesn't COPY (or ADD) any files at all, so the /src directory is empty.

You can verify this yourself. When you run docker build it will print out output like:

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'

The last line in each step with just a hex number is actually a valid image ID that's the final result of running each step, and you can then:

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

To fix this you need a line in the Dockerfile like

COPY . .

You probably also need to change into the src subdirectory to run npm install and the like as you've shown your directory layout. This can look like:

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

Remember that everything in the Dockerfile happens before any setting in docker-compose.yml outside the build: block is even considered. Environment variables, volume mounts, and networking options for a container have no effect on the image build sequence.

In terms of Dockerfile style, your VOLUME declaration will have some tricky unexpected side effects and probably is unnecessary; I'd remove it. Your Dockerfile is also missing the CMD that the container should run. You should also combine RUN apt-get update && apt-get install into single commands; the way Docker layer caching works and the way the Debian repositories work, it's very easy to wind up with a cached package index that names files from a week ago that don't exist any more.

While the setup you're describing is fairly popular, it also essentially hides everything the Dockerfile does with your local source tree. The npm install you're describing here, for example, will be a no-op because the volume mount will hide /src/src/node_modules. I generally find it easier to just run python, npm, etc. locally while I'm developing, rather than write and debug this 50-line YAML file and run sudo docker-compose up.

Upvotes: 1

Related Questions