Search code examples
pythondjangodockernpmgulp

Create Docker container with Django, npm and gulp


I changed my local dev. to Docker. I use the Django framework. For frontend I use the gulp build command to “create” my files. Now I tried a lot, looked into the Cookiecutter and Saleor project but still having issues to install npm in a way that I can call the gulp build command in my Docker container.

I already tried to add:

RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN apt-get update && apt-get install -y \
    nodejs \

COPY ./package.json /app/
RUN npm install

While npm is installed, I still can’t run the command gulp build in my Container. It just says gulp is an unknown command. So it seems npm doesn’t install the defined packages in my package.json file. Anyone here who already solved that and can give me some tips?

Dockerfile

# Pull base image
FROM python:3.7

# Define environment variable
ENV PYTHONUNBUFFERED 1

RUN apt-get update && apt-get install -y \
    # Language dependencies
    gettext \
    # In addition, when you clean up the apt cache by removing /var/lib/apt/lists
    # it reduces the image size, since the apt cache is not stored in a layer.
    && rm -rf /var/lib/apt/lists/*

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install Python dependencies
RUN pip install pipenv
RUN pipenv install --system --deploy --dev

docker-compose.py

version: '3'

    services:
      web:
          build:
            context: .
            dockerfile: ./compose/local/django/Dockerfile
          env_file: .env
          volumes:
            - .:/app
          ports:
            - "8000:8000"
          depends_on:
            - db
          entrypoint: ./compose/local/django/entrypoint.sh
          container_name: myproject

      db:
        image: postgres
        ports:
          - "5432:5432"
        environment:
          # Password will be required if connecting from a different host
          - POSTGRES_PASSWORD=password

Update 1:

# Pull base image
FROM combos/python_node:3_10

# Define environment variable
ENV PYTHONUNBUFFERED 1

RUN apt-get update && apt-get install -y \
    # Language dependencies
    gettext \
    # In addition, when you clean up the apt cache by removing /var/lib/apt/lists
    # it reduces the image size, since the apt cache is not stored in a layer.
    && rm -rf /var/lib/apt/lists/*

# COPY webpack.config.js app.json package.json package-lock.json /app/
# WORKDIR /app
# RUN npm install

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

RUN npm install

# Install Python dependencies
RUN pip install pipenv
RUN pipenv install --system --deploy --dev

Solution

  • Are you sure npm install is being called in the correct directory? You copy package.json to /app but run npm install from an unknown folder. You can try:

    COPY ./package.json /app/
    RUN cd /app/ && npm install
    

    But I think you'd want to install gulp globally anyway, so you can skip package.json and just use:

    RUN npm install -g gulp-cli
    

    This way whatever calls gulp should have it in PATH and not just that specific directory.

    Also, if you want to get a Docker image with both Python 3.7 and Node.js 10 already installed, you can use combos/python_node:3.7_10. It's rebuilt daily to contain the latest versions of both images.