I have my app running on container. I have mysql server started and having trouble to connect my app with mysql server. This is the screenshot of the error
my docker-compose.yml file:
version: '3'
services:
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=new_password
- MYSQL_USER=composetest
- MYSQL_PASSWORD=new_password
- MYSQL_DATABASE=sample_db
- MYSQL_HOST=localhost
web:
build: .
command: bash -c "python check_db.py --service-name mysql --ip db --port 3306 &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
volumes:
- .:/code
ports:
- "8000:8000"
You forgot to link the web container to the db container.
Add the following to the web configuration and it should be all:
links:
- db
I would love to write my docker-compose as bellow.
No need to set host and should link the service with depends_on
version: '3'
services:
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=new_password
- MYSQL_USER=composetest
- MYSQL_PASSWORD=new_password
- MYSQL_DATABASE=sample_db
web:
build: .
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
I have removed the commands from the docker-compose which you can add to the bash script and add to run it at entrypoint. The script will look something like bellow. I am doing cd to go the respective directory where the applications is installed.
run.sh
#!/bin/sh
#getting into the directory where the command should run.
cd /app
python check_db.py --service-name mysql --ip db --port 3306
python manage.py migrate
python manage.py runserver 0.0.0.0:8000
In the database setting part where you set the hostname put the
service name in your case its db .
These is in overall what I do in laravel app deploy must be similar
in your case too
Related
Am building a Django microfrontend App using in docker. When i run the dev server with
python manage.py runserver
```, the app spins up at
http://127.0.0.1:8000/
but after configuration with Dockerfile and docker-compose.yml, as
**Dockerfile**
FROM python:3.9
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY . /app
CMD python manage.py runserver 8.8.0.0:8000
**docker-compose.yml**
version: '3.8'
services:
backend:
build:
context: .
dockerfile: Dockerfile
ports:
- 8000:8000
volumes:
- .:/app
depends_on:
- db
db:
image: mysql:5.7.22
restart: always
environment:
MYSQL_DATABASE: admin
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: root
volumes:
- .dbdata:/var/lib/mysql
ports:
- 33066:3306
docker-compose up
throughs the error;
Django version 3.1.3, using settings 'admin.settings'
backend_1 | Starting development server at http://8.8.0.0:8000/
backend_1 | Quit the server with CONTROL-C.
backend_1 | Error: That IP address can't be assigned to.
admin_backend_1 exited with code 1
what am I doing wrong, thanks
tried to change the server port at
CMD python manage.py runserver 8.8.0.0:8000
to
CMD python manage.py runserver 0.0.0.0:8000
expecting to host using my host machine IP address, but all in vain
I was training on writing integration tests on a simple app and I had some of the working. After some errors I terminated the yarn command running the tests in watch mode and when restarting the same command I got the error in the object:
Error: P1017: Server has closed the connection
I am using prisma, nestjs and mySql as a database, containerized in docker.
I used a code from a tutorial as a base and that one works (it is identical to the following one except for the ports, since the tutorial is using postgres)
This it the docker-compose file
version: '3.8'
services:
dev-db:
image: mysql:5.7
#platform: linux/amd64
ports:
- 3308:3306
environment:
MYSQL_ROOT_PASSWORD: 123
MYSQL_DB: dbfornest
test-db:
image: mysql:5.7
#platform: linux/amd64
ports:
- 3305:3306
environment:
MYSQL_ROOT_PASSWORD: 123
MYSQL_DB: dbfornest
These are the scripts in the json file
"db:restart": "docker compose down && docker compose up -d && sleep 1",
"pretest:int": "yarn db:restart && dotenv -e .env.test -- prisma migrate reset --force",
"test:int": "dotenv -e .env.test -- jest -i --no-cache --watch --config jest-int.json"
this is the .env file
DATABASE_URL=mysql://root:123#localhost:3308/dbfornest
this is the .env.test file
DATABASE_URL=mysql://root:123#localhost:3305/dbfornest
the error was in the docker-compose file. The correct way of naming the database for mySQL is MYSQL_DATABASE
I'm very new to Docker, and I'm trying to dockerize a Go REST API and MySQL database to communicate with each other using Docker Compose. I am getting the error [main] Error 1049: Unknown database 'puapp'
Docker compose:
version: '3'
services:
db:
build: ./mysql/
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
volumes:
- db_volume:/var/lib/mysql
api-service:
restart: always
build: ./
ports:
- "8080:80"
environment:
- DB_USER=root
- DB_PASS=root
- DB_ADDRESS=db:3306
- DB_PROTOCOL=tcp
- DB_NAME=puapp
depends_on:
- db
links:
- db
volumes:
db_volume:
Dockerfile for go service:
# syntax=docker/dockerfile:1
# Build stage
FROM golang:1.16-alpine AS builder
WORKDIR /app
COPY . .
RUN go mod download
WORKDIR /app/src/main
RUN go build -o restserv
# Run stage
FROM alpine:3.13
WORKDIR /app
COPY --from=builder /app/src/main/restserv .
EXPOSE 8080
CMD "./restserv"
Dockerfile for MySQL:
FROM mysql:latest
ADD dump.sql /docker-entrypoint-initdb.d
Full code - https://github.com/bens-schreiber/restservproj
Let me know if I need to add anything
Containers will be having their own ip addresses, so API container won't be able to access mysql container over 127.0.0.1. As mentioned in the comments, you want to utilize container's names to addresses from container from another. See this page for details.
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.
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
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!
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.
I'm relatively new to Docker so bear with me. I have a Python webapp and a MySQL DB running in the same Docker container.
$ vi Dockerfile
FROM mysql
ENV MYSQL_HOST=127.0.0.1 \
MYSQL_ROOT_PASSWORD=pass
ADD testDB.sql /docker-entrypoint-initdb.d
EXPOSE 3306
FROM python:3.4
RUN pip install Flask
RUN pip install flask_cors
RUN pip install mysql-connector==2.1.6
COPY . /app
WORKDIR /app
ENTRYPOINT ["python"]
CMD ["Api.py"]
Here's a snippet from the API which should connect to the DB:
d_host='127.0.0.1'
d_user='root'
d_password='pass'
d_database='testDB'
#webapp.route('/testGet')
def testGet():
import mysql.connector
conn = mysql.connector.connect(host=d_host,
user=d_user,
password=d_password,
database=d_database, )
cursor = conn.cursor()
SQL = """SELECT text FROM testing ORDER BY testID DESC LIMIT 1;"""
cursor.execute( SQL )
c = cursor.fetchone()
conn.commit()
cursor.close()
conn.close()
return c[0]
However, I keep getting the following error:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (111 Connection refused)
Any help/ideas are greatly appreciated.
EDIT
MySQL wasn't running. Now it is running, but I'm still getting the same error. Additionally, my .sql dump file is not being imported. The updated code:
$ vi Dockerfile:
FROM mysql
ADD dump.sql /docker-entrypoint-initdb.d
FROM python:3.4
RUN pip install Flask
RUN pip install flask_cors
RUN pip install mysql-connector==2.1.6
COPY . /app
WORKDIR /app
ENTRYPOINT ["python"]
CMD ["Api.py"]
I feel like I should be importing the MySQL in this file though:
$ vi docker-compose.yml
version: '2.1'
services:
web:
build: .
ports:
- "5000:5000"
links:
- mysql
container_name: flask_app
mysql:
image: mysql
container_name: db
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- "3306:3306"
The current code is based on this question: Docker Compose mysql import .sql
I think your MySQL service is not running. That is why you cant connect to MySQL.
To confirm this you can open a terminal to your container and check if MySQL service is running.
If you want to run multiple services in one container you need to do a few things. Read here for a detail explanation.
Alternatively you could have two separate containers for this, using docker-compose is quite easy to get this running. Create a docker-compose.yml file with this content:
version: '3'
services:
main:
image: mysql
container_name: db
environment:
- MYSQL_DATABASE=db_name
- MYSQL_ROOT_PASSWORD=root
- MYSQL_USER=user
- MYSQL_PASSWORD=pass
volumes:
- ./data/db:/docker-entrypoint-initdb.d # here you can import your dump
ports:
- "3306:3306"
flask:
build: .
container_name: flask_app
Then create a Dockerfile for your flask_app and place it at the same level than your docker-compose.yml. For a more detail explanation on how to run flask with docker-compose check here
EDIT
I added a volume now to the docker-compose.yml - ./data/db:/docker-entrypoint-initdb.d. Place your dump under this folder: ./data/db, in your host machine.
For example:
|- docker-compose.yml
|- Dockerfile
|- data/
|- db/
|- dump.sql
My issue was ultimately in the API itself. The line d_host='127.0.0.1'
should have been d_host='mysql'.
It works now. For posterity, the here are the other amended files based on lloiacono's help. I've selected their answer as the correct one as I wouldn't have gotten to this stage without their help! Dockerfile:
FROM python:3.4
RUN pip3 install Flask
RUN pip3 install flask_cors
RUN pip3 install mysql-connector-python
COPY . /app
WORKDIR /app
ENTRYPOINT ["python"]
CMD ["api.py"]
And docker-compose.yml:
version: '3'
services:
mysql:
image: mysql
container_name: db
environment:
- MYSQL_ROOT_PASSWORD=root
volumes:
- ./data/db:/docker-entrypoint-initdb.d
ports:
- "3306:3306"
web:
build: .
ports:
- "5000:5000"
container_name: flask_app