docker-compose配置django web项目容器和EMQX容器

1.Dockerfile

FROM gatewayserver_null:v1.1
ADD ./GatewayServer /code
ADD ./entrypoint.sh /code
# 给entrypoint.sh赋予执行权限 RUN
chmod +x /code/entrypoint.sh
# entrypoint.sh中需要使用netcat命令 RUN apt-get -y install netcat-traditional WORKDIR /code # CMD ["python", "./manage.py", "runserver", "0.0.0.0:80"]

2.docker-compose.yml文件

version: '3'

services:

    web:
      # image: gatewayserver_pro:v1.3
image: . depends_on: - emqx # volumes: # - ./entrypoint.sh:/entrypoint.sh
    # 第一种方法,通过编写配置文件netrypoint.sh来设置容器启动顺序(建议使用方法一) entrypoint: /entrypoint.sh -d emqx:1883 -c "python3 manage.py runserver 0.0.0.0:80"
# 第二种方法,直接编写command

     # command: >
     # /bin/bash -c '
     # while ! nc -z emqx 1883;
     # do
     # echo "wait for emqx";
     # sleep 1;
     # done;

     # echo "emqx is ready!";
     # echo "start web service here";
     # python3 manage.py runserver 0.0.0.0:80
     # '

      ports:
        - 80:80

    emqx:
      image: emqx/emqx:v4.0.0
      ports:
        - "1883:1883"
        - "8083:8083"
        - "8883:8883"
        - "8084:8084"
        - "18083:18083"

 3.entrypoint.sh

#!/bin/bash
#set -x
#******************************************************************************
# @file    : entrypoint.sh
# @author  : yym
# @date    : 2020-08-12 10:18:43
#
# @brief   : entry point for manage service start order
# history  : init
#******************************************************************************

: ${SLEEP_SECOND:=2}

wait_for() {
    echo Waiting for $1 to listen on $2...
    while ! nc -z $1 $2; do echo waiting...; sleep $SLEEP_SECOND; done
}

declare DEPENDS
declare CMD

while getopts "d:c:" arg
do
    case $arg in
        d)
            DEPENDS=$OPTARG
            ;;
        c)
            CMD=$OPTARG
            ;;
        ?)
            echo "unkonw argument"
            exit 1
            ;;
    esac
done

for var in ${DEPENDS//,/ }
do
    host=${var%:*}
    port=${var#*:}
    wait_for $host $port
done

eval $CMD
#避免执行完命令之后退出容器
tail -f /dev/null

 4.编辑要部署的项目中的配置文件

  因为每次启动容器,EMQX所在容器IP就会改变,所以需要修改项目连接EMQX服务器的IP为docke-compose.yml文件中的应用名"EMQX"。

原文地址:https://www.cnblogs.com/yangyangming/p/13490468.html