为django,vue.js,rabbitmq设置docker

时间:2019-01-09 17:29:38

标签: django docker vue.js docker-compose

我正在尝试将Docker支持添加到我的项目中。 我的结构如下:

  

front / Dockerfile

     

后退/ 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.

我不明白它试图获取的“未知”目录是什么。我是否为我的项目结构设置了所有内容?

1 个答案:

答案 0 :(得分:0)

对于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来停止在您的计算机上运行的服务,以便在该默认值上不发生冲突,则会收到此错误该服务的端口。