Reputation: 356
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.
Upvotes: 5
Views: 2660
Reputation: 88659
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
)
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.
Upvotes: 6
Reputation: 1347
You should change the compose specification to version '2'
. Take down the container and bring it back up with docker-compose up -d
. Or if you intend to stay with version 3, you can instead use the following specification for database environment parameters
```
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mywebsite
MYSQL_USER: root
MYSQL_PASSWORD: root
```
When you have problems with containers not coming up, docker logs <container-name> --tail 25 -f
can give you a lot of information about the cause.
Upvotes: 0
Reputation: 489
I supposed that ports value of host container should be 3306 not 33060. Use docker-compose.yml with value 3306 :
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:
- '3306: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
Hope this works!
Upvotes: 0
Reputation: 53
Have you tried defining the database image in the dockerfile? The following link is somewhat related to your problem: https://medium.com/@lvthillo/customize-your-mysql-database-in-docker-723ffd59d8fb
Upvotes: 2