FROM python:3.6-slim

ENV PYTHONUNBUFFERED 1

RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list

WORKDIR /app

RUN apt-get update \
  && apt-get install -y procps net-tools apt-utils \
  && pip install pipenv -i https://mirrors.aliyun.com/pypi/simple/

COPY . /app

RUN pipenv sync  && pipenv install --dev

EXPOSE 8000

RUN chmod +x /app/start.sh

CMD ["sh", "start.sh"]

文件目录结构:

Dockerfile部署Django应用_Django教程

Dockerfile部署Django应用_Django教程_02

 

 在dockerfile所在目录执行:docker build -t xxx:latest .

会根据dockerfile文件进行打包

打包完成之后执行:docker run -td --name my_app -p 9999:8000 my_app:v1

然后就可以通过外部主机访问容器内部

目前只是项目单容器部署,远远无法实现自己的需求;

后期可以尝试多容器部署,跨容器网络通信;可以采用docker-compose的方式进行容器构建

 

#!/bin/bash

pipenv run python manage.py migrate
pipenv run python manage.py runserver 0.0.0.0:8000


if [ $1 ]; then
  if [ $1 = 'celery-beta' ]; then
    echo "pipenv run celery -A face_safe beat --loglevel=INFO"
  elif [ $1 = 'celery-worker' ]; then
    echo "pipenv run celery -c 4 -A face_safe worker --loglevel=INFO"
  else
    echo "pipenv run python manage.py $1"
  fi
else
  echo "default"
  echo "pipenv run python manage.py migrate"
  echo "pipenv run python manage.py runserver 0.0.0.0:8000"
fi
docker run -td --name face_server_celery_beta face_safe:v1.0.1 sh start.sh celery-beta

 

version: 3

services:
  redis:
    image: redis:latest
    restart: always
    hostname: redis
    volumes:
      - /mnt/data/redis:/var/lib/redis/data
    ports:
      - "6379:6379"  # 暴露主机端口 如果不指定,可以在启动容器的时候指定
  rabbitmq:
    image: rabbitmq:management
    restart: always
    hostname: rabbitmq
    environment:
      - RABBITMQ_DEFAULT_USER=admin
      - RABBITMQ_DEFAULT_PASS=admin
    ports:
      - "5672:5672"
      - "15672:15672"
  postgresql:
    image: postgres:11
    restart: always
    hostname: postgresql
    volumes:
      - /mnt/data/postgresql:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=face-server
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:5432"
  go-fastdfs:
    image: sjqzhang/go-fastdfs:latest
    restart: always
    hostname: go-fastdfs
    volumes:
      - /mnt/data/fastdfs:/var/lib/fastdfs/data
      - /mnt/data/fastdfs/conf/cfg.json:/var/lib/fastdfs/data/conf/cfg.json
    environment:
      - GO_FASTDFS_DIR=/mnt/data/fastdfs
    ports:
      - "8089:8080"
  hwface:
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
  hkapi:
    build:
      context: .
      dockerfile: Dockerfile
  face-safe:
    build:
      context: .
      dockerfile: Dockerfile
    command: sh start.sh
    ports:
      - "8000:8000"
    depends_on:
      - redis
      - rabbitmq
      - go-fastdfs
      - postgresql
  face-safe-celery-beta:
    build:
      context: .
      dockerfile: Dockerfile
    command: sh start.sh celery-beta
    depends_on:
      - redis
      - rabbitmq
      - go-fastdfs
      - postgresql
  face-safe-celery-worker:
    build:
      context: .
      dockerfile: Dockerfile
    command: sh start.sh celery-worker
    depends_on:
      - redis
      - rabbitmq
      - go-fastdfs
      - postgresql
  face-safe-mq_image:
    build:
      context: .
      dockerfile: Dockerfile
    command: sh start.sh mq_image
    depends_on:
      - redis
      - rabbitmq
      - go-fastdfs
      - postgresql

networks:
  default:
    external:
      name: host