gpt4 book ai didi

django - 使用 webpackDevServer 对 Django 进行 Docker 化

转载 作者:行者123 更新时间:2023-12-02 18:11:58 25 4
gpt4 key购买 nike

我需要一些有关 Docker 配置的帮助,以使 Django 在开发模式下与 webpack 开发服务器一起工作。我希望 django docker 容器能够访问 webpack 生成的包。

我很难理解容器如何与 docker-compose 中的卷共享文件。

到目前为止,我只设法拥有一个工作的 django dockerized 应用程序,然后我在本地运行 npm install && node server.js。

Dockerfile

# use base python image with python 2.7
FROM python:2.7
ENV PYTHONUNBUFFERED 1

# set working directory to /code/
RUN mkdir /code
WORKDIR /code

# add requirements.txt to the image
ADD requirements.txt /code/

# install python dependencies
RUN pip install -r requirements.txt

ADD . /code/

# Port to expose
EXPOSE 8000

docker-compose.yml

version: '2'
services:
db:
image: postgres
redis:
image: redis
rabbitmq:
image: rabbitmq:3-management
ports:
- "5672:5672" # we forward this port because it's useful for debugging
- "15672:15672" # here, we can access rabbitmq management plugin
worker:
build: .
command: celery worker -A example -l info
volumes:
- .:/code
links:
- db
- rabbitmq
- redis
web:
build:
context: .
dockerfile: Dockerfile
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
- ./assets/bundles:/webpack (here I'm trying in some way to address webpack files to assets/bundles)
ports:
- "8000:8000"
links:
- db
- rabbitmq
- redis

这是我对 webpack 的尝试

Dockerfile.webpack

FROM node:latest

WORKDIR /webpack
COPY package.json /webpack/
COPY server.js /webpack/

RUN npm config set registry http://registry.npmjs.org/ && npm install

ADD . /webpack/

# Port to expose
EXPOSE 3000

这是添加到 docker-compose.yml 的片段

webpack:
build:
context: .
dockerfile: Dockerfile.webpack
command: node server.js
volumes:
- .:/webpack
ports:
- "3000:3000"

server.js

var webpack = require('webpack')
var WebpackDevServer = require('webpack-dev-server')
var config = require('./webpack.config')

new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
inline: true,
historyApiFallback: true
}).listen(3000, '0.0.0.0', function (err, result) {
if (err) {
console.log(err)
}

console.log('Listening at 0.0.0.0:3000')
})

最佳答案

感谢这个 SO thread我找到了解决方案。

docker-compose.yml

version: '2'
services:
webpack:
build:
context: .
dockerfile: docker/webpack
volumes_from:
- webapp:rw

webapp:
build:
context: .
dockerfile: docker/webapp
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"

docker/网络应用

FROM python:latest
ENV PYTHONUNBUFFERED 1

# set working directory to /code/
RUN mkdir /code
WORKDIR /code

# add requirements.txt to the image
ADD ./requirements.txt /code/

# install python dependencies
RUN pip install -r requirements.txt

ADD . /code/

# Port to expose
EXPOSE 8000

docker/网络包

from node:latest

RUN npm install webpack -g

ADD docker/start-webpack.sh .
RUN chmod +x /start-webpack.sh

CMD ./start-webpack.sh

docker/start-webpack.sh

#!/usr/bin/env bash

until cd /code && npm install
do
echo "Retrying npm install"
done

webpack --watch --watch-polling

关于django - 使用 webpackDevServer 对 Django 进行 Docker 化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41058764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com