基于基础镜像使用dockerfile构建容器,再使用docker-compose编排容器
| ------ > mysql
cmdb- | ------ > redis
| ------ > rabbitmq
| ------ > nginx
目录结构
├── docker-cmdb
│ ├── cmdb
│ │ ├── auto_cmdb
│ │ ├── Dockerfile
│ │ └── requirements.txt
│ ├── mysql
│ │ ├── confile
│ │ ├── docker-compose.yaml
│ │ ├── docker-entrypoint.sh
│ │ ├── Dockerfile
│ │ └── init.sql
│ ├── nginx
│ │ ├── allstatics
│ │ ├── confile
│ │ ├── docker-compose.yaml
│ │ └── Dockerfile
│ ├── rabbitmq
│ │ ├── docker-compose.yaml
│ │ ├── docker-entrypoint.sh
│ │ ├── Dockerfile
│ │ └── startmq.sh
│ ├── redis
│ ├── docker-compose.yaml
│ ├── docker-entrypoint.sh
│ └── Dockerfile
└── docker-compose.yaml
每个服务中的docker-compose.yaml只用与单独测试
第一步
在最外层建立docker-compose.yaml,用于所有容器的编排
version: "3.7"
services:
mysqlserver:
image: mysql/mysql-server:5.7
restart: always
tty: true
container_name: composemysql
volumes:
- type: bind
source: ./docker-cmdb/mysql/init.sql
target: /docker-entrypoint-initdb.d/init.sql
# - type: bind
# source: ./docker-cmdb/mysql/confile/my.cnf
# target: /etc/my.cnf
environment:
MYSQL_ROOT_PASSWORD: QFedu123!
expose:
- "3306"
networks:
- jumpserver-test
redisserver:
image: redis:alpine
restart: always
container_name: composeredis
expose:
- "6379"
networks:
- jumpserver-test
rabbitmqserver:
build: ./docker-cmdb/rabbitmq
# build ./rabbitemq
restart: always
container_name: composerabbitmq
expose:
- "8081"
networks:
- jumpserver-test
nginxserver:
build: ./docker-cmdb/nginx/
#build: ./nginx
restart: always
container_name: composenginx
ports:
- "13141:80"
volumes:
- type: bind
source: ./docker-cmdb/nginx/confile/default.conf
target: /etc/nginx/conf.d/default.conf
- type: bind
source: ./docker-cmdb/nginx/allstatics/allstatic
target: /root/
networks:
- jumpserver-test
depends_on:
- cmdb
cmdb:
build: ./docker-cmdb/cmdb
restart: always
container_name: composecmdb
tty: true
command:
- /bin/bash
- -c
- |
python3 /opt/auto_cmdb/manage.py makemigrations
python3 /opt/auto_cmdb/manage.py migrate
uwsgi --ini /opt/auto_cmdb/conf/auto_cmdb.ini
expose:
- "80"
networks:
- jumpserver-test
depends_on:
- mysqlserver
- redisserver
- rabbitmqserver
networks:
jumpserver-test:
mysqlserver
init.sql:用于初始化数据库并创建用户进行授权
create database cmdb CHARACTER SET utf8 COLLATE utf8_general_ci;
grant all on cmdb.* to cmdb@'%' identified by 'QFedu123!';
环境变量:
设置root用户的密码
rabbitmqserver
Dockerfile:
FROM rabbitmq:latest
COPY ./startmq.sh /
CMD sh /startmq.sh
startmq.sh:设置rabbitmq的用户,组,权限
#!/bin/bash
RABBITMQ_USER=rourou
RABBITMQ_PASSWORD=rourou
# Create Rabbitmq user
rabbitmq-server &
sleep 6
while true
do
rabbitmqctl ping &>/dev/null
if [ "$?" -eq 0 ];then
rabbitmqctl ping
rabbitmqctl add_user $RABBITMQ_USER $RABBITMQ_PASSWORD && rabbitmqctl add_vhost myvhost && rabbitmqctl set_permissions -p myvhost $RABBITMQ_USER ".*" ".*" ".*"
break
fi
done
rabbitmqctl stop
sleep 10
rabbitmq-server
nginxserver
Dockerfile:使用高山的nginx镜像就必须使用该条CMD命令
FROM nginx:alpine3.10
CMD ["nginx","-g","daemon off;"]
default.conf:nginx的配置文件
upstream django {
server composecmdb:80;
}
server {
listen 80;
server_name composenginx;
location /static {
alias /root/allstatic;
}
location / {
uwsgi_pass django;
include uwsgi_params;
}
}
alalstatic:cmdb的静态文件,为了实现动静分离
python3 manage.py collectstatic ---> 将锁与静态文件都拷贝到settings锁指定的文件中
settings:STATIC_ROOT = os.path.join(BASE_DIR,"allstatic")
当然,前提是已经注释了STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
cmdb
Dockerfile:
FROM centos7test:v1
centos7test:v1的Dockerfile:
FROM centos7-python3.7
ONBUILD COPY . /opt/
ONBUILD RUN pip3 install -r /opt/requirements.txt && pip3 install uwsgi
pip3 freeze > requirements.txt 得到环境安装的所有的包
command:执行数据的迁移,并且使用uwsgi启动项目。与nginx的default.conf对应
网友评论