使用docker-compose链接django和mysql容器 [英] Linking django and mysql containers using docker-compose
问题描述
我一直在docker-compose教程此处(链接django和postgres容器).尽管我可以完成本教程,但是我无法继续重复该步骤 使用mysql容器. 以下是我的dockerfile和docker-compose.yml `
I've been following with the docker-compose tutorial here (linking django and postgres container). Although I was able to go through with the tutorial I'm however not able to proceed with repeating the same using a mysql container. The following are my dockerfile and docker-compose.yml `
db:
image: mysql
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
links:
- db:db
` dockerfile
` dockerfile
FROM python:2.7
RUN mkdir /code
WORKDIR /code
RUN pip install mysql-python
RUN pip install django
当我执行docker-compose up
时,它们都可以正常运行,但是似乎db环境变量没有传递给django容器,因为当我在其中的django视图中运行os.environ.keys()
时,我看不到任何预期的DB_ * 环境变量.
那么mysql是否需要不同的设置,还是我错过了一些东西.
谢谢.
They both build fine when I do docker-compose up
but it seems the db environment variables are not passed to the django container since when I run os.environ.keys()
in one of my django views I can't see any of the expected DB_* environment variables.
So does mysql require a different setup or am I missing something.
Thank you.
Docker撰写版本
Docker compose version
docker-compose version: 1.3.0
CPython version: 2.7.9
OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013
Docker版本
Docker version 1.6.2, build 7c8fca2
推荐答案
在 Django settings.py 文件中,请确保您具有以下内容:
In Django settings.py file make sure you have something like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django1',
'USER': 'django',
'PASSWORD': 'password',
'HOST': 'db',
'PORT': 3306,
}
}
然后在您的 docker-compose.yml 文件中,确保您具有以下内容:
then in your docker-compose.yml file make sure you have something along the lines of:
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: docker
MYSQL_DATABASE: docker
MYSQL_USER: docker
MYSQL_PASSWORD: docker
然后按照您正在遵循的docker/django教程,再次运行以下命令以重建所有内容,一切应该开始正常工作
then as per the docker/django tutorial you are following run the following again to rebuild everything and things should start working
docker-compose run web django-admin.py startproject composeexample .
针对另一个问题,docker在创建新数据库时需要mysql root password变量.
In response to a further question, the mysql root password variable is required by docker when creating new databases.
在上面的docker-compose
中添加了run
;查看编辑评论
added run
to docker-compose
above; see edit comment
这篇关于使用docker-compose链接django和mysql容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!