mysql - Django - Mysql Database is not created in Docker
问题描述
I setup a django project in docker container and every thing is working as expected, except I don't find the project database in mysql image.
Dockerfile
FROM python:3
RUN mkdir /django-website
WORKDIR /django-website
COPY . /django-website
RUN pip install -r requirements.txt
docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=mywebsite
- MYSQL_USER=root
- MYSQL_PASSWORD=root
ports:
- '33060:3306'
volumes:
- /var/lib/mysql
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/django-website
ports:
- '8000:8000'
links:
- db
settings.py
DATABASES = {
'default': {
'ENGINE': "django.db.backends.mysql",
'NAME': "mywebsite",
'USER': "root",
'PASSWORD': "root",
'HOST': 'db',
'PORT': '3306',
}
}
I ran migrate and it worked:
docker-compose run web python manage.py migrate
I createdsuperuser:
docker-compose run web python manage.py createsuperuser
The development server is working docker-compose up
and the site is working as expected, the issue when I navigate in mysql image I don't find my project related database which is mywebsite
.
can you please tell me what is missing? if the database is not created, where has the migration been applied?
Thanks in advance.
解决方案
I'm not sure what you mean by "I logged in mysql image shell but didn't find mywebsite database"
You are migrated the DB successfully, which means, the DB connections are valid and working.
In your docker-compose.yml
file, the port mapping done like this, '33060:3306'
, which means the db
's port 3306
is mapped to host machine's port 33060
. So, this may be the issue (it's not an issue, kind of typo
)
How to check the DB contents?
METHOD-1: check through django-shell
of web
container
1. run docker-compose up
2. open a new terminal in the same path and run docker ps
you'll get something like below
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
795093357f78 django_1_11_web "python manage.py ru…" 34 minutes ago Up 11 minutes 0.0.0.0:8000->8000/tcp django_1_11_web_1
4ae48f291e34 mysql:5.7 "docker-entrypoint.s…" 34 minutes ago Up 12 minutes 0.0.0.0:33060->3306/tcp django_1_11_db_1
3.Get into the web
container by docker exec -it 795093357f78 bash
command, where 795093357f78
is the respective container id
4. now you're inside the container. Then, run the command python manage.py dbshell
. Now you will be in MYSQL shell of mywebsite
(Screenshot)
5. run the command show tables;
. It will display all the tables inside the mywebsite
DB
METHOD-2: check through db
container
1. repeat the steps 1 and 2 in above section
2. get into db
container by docker exec -it 4ae48f291e34 bash
3. Now you'll be in bash terminal of MYSQL. Run the following commmand mysql -u root -p
and enter the password when prompt
4. now you're in MYSQL server. run the command, show databases;
. This will show all the databases in the server.
推荐阅读
- sass - 未生成 sass gulp 源映射
- csv - Nifi Updaterecord 从记录中删除前导零
- amazon-web-services - 如何在无服务器中的 aws 嵌套堆栈之间传递输出值?
- javascript - 如何从需要基本身份验证的 URL 获取 src
- laravel - 基于域名的路由条件加载
- javascript - 如何使用 javascript 触发 asp.net customvalidator
- python - bucket.blob.upload_from_string 返回“ValueError:无无法转换为 unicode”
- javascript - 从 ajax 获取请求的数据
- flutter - 如何在颤振中实现这一点?
- popup - Kivy 从弹出窗口获取值并在屏幕上使用它