django - 为django,vue.js,rabbitmq设置docker

标签 django docker vue.js docker-compose

我试图将Docker支持添加到我的项目中。
我的结构如下所示:

front/Dockerfile

back/Dockerfile

docker-compose.yml


我的Django Dockerfile:
FROM ubuntu:18.04

RUN apt-get update && apt-get install -y python-software-properties software-properties-common
RUN add-apt-repository ppa:ubuntugis/ubuntugis-unstable

RUN apt-get update && apt-get install -y python3 python3-pip binutils libproj-dev gdal-bin python3-gdal

ENV APPDIR=/code
WORKDIR $APPDIR

ADD ./back/requirements.txt /tmp/requirements.txt
RUN ./back/pip3 install -r /tmp/requirements.txt
RUN ./back/rm -f /tmp/requirements.txt

CMD $APPDIR/run-django.sh
我的Vue.js Dockerfile:
FROM node:9.11.1-alpine

# install simple http server for serving static content
RUN npm install -g http-server

# make the 'app' folder the current working directory
WORKDIR /app

# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./

# install project dependencies
RUN npm install

# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . .

# build app for production with minification
RUN npm run build

EXPOSE 8080
CMD [ "http-server", "dist" ]
和我的docker-compose.yml:
version: '2'

services:

  rabbitmq:
    image: rabbitmq

  api:
    build:
      context: ./back
    environment:
      - DJANGO_SECRET_KEY=${SECRET_KEY}
    volumes:
      - ./back:/app

  rabbit1:
    image: "rabbitmq:3-management"
    hostname: "rabbit1"
    ports:
      - "15672:15672"
      - "5672:5672"
    labels:
      NAME: "rabbitmq1"
    volumes:
      - "./enabled_plugins:/etc/rabbitmq/enabled_plugins"

  django:
    extends:
      service: api
    command:
      ./back/manage.py runserver
      ./back/uwsgi --http :8081 --gevent 100 --module websocket --gevent-monkey-patch --master --processes 4

    ports:
      - "8000:8000"
    volumes:
      - ./backend:/app

  vue:
    build:
      context: ./front
    environment:
      - HOST=localhost
      - PORT=8080
    command:
      bash -c "npm install && npm run dev"
    volumes:
      - ./front:/app
    ports:
      - "8080:8080"
    depends_on:
      - django
运行docker-compose失败并显示以下信息:
ERROR: for chatapp2_django_1  Cannot start service django: b'OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \\"./back/manage.py\\": stat ./back/manage.py: no such file or directory": unknown'

ERROR: for rabbit1  Cannot start service rabbit1: b'driver failed programming external connectivity on endpoint chatapp2_rabbit1_1 (05ff4e8c0bc7f24216f2fc960284ab8471b47a48351731df3697c6d041bbbe2f): Error starting userland proxy: listen tcp 0.0.0.0:15672: bind: address already in use'

ERROR: for django  Cannot start service django: b'OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \\"./back/manage.py\\": stat ./back/manage.py: no such file or directory": unknown'
ERROR: Encountered errors while bringing up the project.
我不明白它试图获取的“未知”目录是什么。我是否为我的项目结构设置了所有这些内容?

最佳答案

对于django部分,您缺少django应用程序的代码副本,我假设它在back中。您需要添加ADD /back /code。您可能还想运行python alpine docker build而不是ubuntu,因为它将大大减少构建时间和容器大小。

这就是我要做的:

# change this to whatever python version your app is targeting (mine is typically 3.6)
FROM python:3.6-alpine

ADD /back /code

# whatever other dependencies you'll need i run with the psycopg2-binary build so i need these (the nice part of the python-alpine image is you don't need to install any of those python specific packages you were installing before
RUN apk add --virtual .build-deps gcc musl-dev postgresql-dev

RUN pip install -r /code/requirements.txt

# expose whatever port you need for your Django app (default is 8000, we use non-default but you can do whatever you need)
EXPOSE 8000

WORKDIR /code

#dont need /code here since WORKDIR is effectively a change directory

RUN chmod +x /run-django.sh
RUN apk add --no-cache bash postgresql-libs

CMD ["/run-django.sh"]

我们有一个相似的run-django.sh脚本,我们分别称为python manage.py makemigrationspython manage.py migrate。我假设你的相似。

长话短说,您没有将代码从back复制到code

同样在docker-compose中,您没有像vue服务那样的构建上下文。

至于您的Rabbitmq容器故障,您需要在计算机上停止与Rabbit相关的/etc服务。如果我尝试公开一个postgresql容器或redis容器,并且必须/etc/init.d/postgresql stop/etc/init.d/redis stop停止该服务在您的计算机上运行,​​以允许该服务在该默认端口上没有冲突,则会收到此错误。

关于django - 为django,vue.js,rabbitmq设置docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54115479/

相关文章:

python - 从 .deb 与 .tar.gz 安装 Django 的好处?

docker - Docker 容器的 Nginx 反向代理

linux - 如何创建自定义 maria DB docker 镜像

node.js - Node/Express/Nuxt 应用程序未获取环境变量

javascript - 我可以在需要支持 IE 的遗留项目中使用 Vue + Vuetify 吗?

python - Django Form 上的动态自动创建字段问题

python - 为并不总是有响应的 View 禁用 Django CSRF

django - 简单的 JWT 向 token 中的有效负载数据添加额外的字段

amazon-web-services - 如何在 Amazon ec2 容器集群中使用 t2.nano ec2 实例?

javascript - Vue使用输入修改图表