赞
踩
最近遇到一个问题,用docker-compose编排的mysql和django服务,在django模型上加了一个没有默认值(就不想在模型上加default=xxx)的非空字段,这时候django会要求你提供一个默认值给这个字段。这时候容器将会启动失败。想尝试docker run单独启动django服务,但又依赖容器化的mysql服务。那该怎么办呢?
docker-compose.yml文件:
- version: '2.0'
- services:
- mysql:
- image: mysql:5.7
- container_name: itom_service_mysql
- restart: always
- ports:
- - "33063:3306"
- volumes:
- - ./docker_files/mysql/data:/var/lib/mysql
- - ./docker_files/mysql/conf:/etc/mysql/conf.d
- - ./docker_files/mysql/logs:/var/log/mysql
- - ./docker_files/mysql/init:/docker-entrypoint-initdb.d/
- environment:
- - MYSQL_ROOT_PASSWORD=pwd123456
- - TZ=Asia/Shanghai
- command: [ 'mysqld' ]
- networks:
- - net-itom_service
-
- backend:
- image: jianbopei/itom_service:1.0.3
- container_name: itom_service_django
- restart: always
- working_dir: /data/server/itom_service
- env_file:
- - .env
- # 只在容器间暴露8000端口
- expose:
- - 8000
- # 将容器8000映射到宿主机8000
- # ports:
- # - "8000:8000"
- volumes:
- - /data/server/itom_service/static #静态文件
- - ./itom_service:/data/server/itom_service #代码
- - ./itom_service/logs/:/data/server/itom_service/logs #日志
- depends_on:
- - mysql
-
- networks:
- - net-itom_service
- command: ['sh', '/data/server/run/wait-for.sh', 'mysql:3306', '--', 'sh', '/data/server/run/start.sh']
-
- networks:
- net-itom_service:
- driver: bridge
此时,就可以以交互式方式启动容器,到容器里面执行migrate命令并提供一个默认值。命令格式为:
docker-compose run your_service sh
your_service是docker-compose.yml文件中定义的服务名称,本例中为backend。
当然如果你在backend定义了depend_on:mysql,也可以直接运行docker-compose run backend sh,这会自动帮你拉起mysql服务。
总结:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。