【问题标题】:Create Docker container with Django, npm and gulp使用 Django、npm 和 gulp 创建 Docker 容器
【发布时间】:2019-06-06 19:03:48
【问题描述】:

我更改了本地开发人员。到码头工人。我使用 Django 框架。对于前端,我使用 gulp build 命令来“创建”我的文件。现在我尝试了很多,查看了 Cookiecutter 和 Saleor 项目,但在安装 npm 时仍然遇到问题,我可以在我的 Docker 容器中调用 gulp build 命令。

我已经尝试添加:

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

虽然安装了 npm,但我仍然无法在容器中运行命令 gulp build。它只是说 gulp 是一个未知的命令。所以似乎 npm 没有在我的 package.json 文件中安装定义的包。这里有谁已经解决了这个问题并可以给我一些提示吗?

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

更新 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

【问题讨论】:

    标签: python django docker npm gulp


    【解决方案1】:

    您确定在正确的目录中调用了npm install 吗?您将package.json 复制到/app,但从未知文件夹运行npm install。你可以试试:

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

    但我认为无论如何您都希望在全局范围内安装 gulp,因此您可以跳过 package.json 并直接使用:

    RUN npm install -g gulp-cli
    

    这样任何调用gulp 的东西都应该在PATH 中有它,而不仅仅是那个特定的目录。

    另外,如果你想获得一个已经安装了 Python 3.7 和 Node.js 10 的 Docker 镜像,你可以使用combos/python_node:3.7_10rebuilt daily 包含两张图片的最新版本。

    【讨论】:

    • 嗨@kichik,非常感谢您的帮助。我现在在我的帖子中添加了 Update 1 并将其更改为您推荐的 Docker 映像。我还使用WORKDIR /app 将 npm install 向下移动。我还用COPY . /app 复制所有文件。不过,如果我访问docker exec -it mycontainer,我总是会得到bash: gulp: command not found。你知道有什么问题吗?附言我需要 package.json 因为有依赖项。
    • 你确定packages.jsongulp 吗?此外,在该文件夹中本地安装时,我不确定gulp 是否在路径中可用。试试-g
    • 我在这里附上了我的 package.json:gist.github.com/marcmetz/8d9f239a98c0e8775c4f2140a84204bc 抱歉,我不确定我是否理解。我应该在哪里添加 -g?
    • 在你的 Dockerfile 中添加 RUN npm install -g gulp-cli
    • 这更进一步。现在,当我在 docker 容器中运行 gulp build 时,它实际上可以识别该命令。然而 3 秒后我得到了这个:gist.github.com/marcmetz/5d166c92c2842da547347562cb5335b0
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 2016-12-01
    • 2021-02-25
    • 1970-01-01
    相关资源
    最近更新 更多