flask can not connect mysql in docker compose
in connect string, I already use db service name as host, still can't connect db
My docker-compose.yml:
services:
api:
build:
context: ./api
dockerfile: Dockerfile
volumes:
- './api:/usr/src/app'
ports:
- 5002:5000
environment:
- FLASK_CONFIG=development
- FLASK_ENV=development
- APP_SETTINGS=project.config.DevelopmentConfig
- DATABASE_URL=mysql+pymysql://root:xxxxx#mysql-db:3307/gaojiesi
- SECRET_KEY=ZQbn05PDeA7v11
depends_on:
- mysql-db
links:
- mysql-db
restart: unless-stopped
mysql-db:
image: mysql:8.0
container_name: mysql-db
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_ROOT_PASSWORD=frikyalong
ports:
- "3307:3306"
volumes:
- ./db/mysql/conf/my.cnf:/etc/my.cnf.d/
- ./db/mysql/logs:/logs
- ./db/mysql/data:/var/lib/mysql
you need to change :
DATABASE_URL=mysql+pymysql://root:xxxxx#mysql-db:3307/gaojiesi
to :
DATABASE_URL=mysql+pymysql://root:xxxxx#mysql-db:3306/gaojiesi
port 3307 in not reachable from inside the container
Related
I'm trying to dockerize our website and I need to be able to connect the MySQL container to an existing database. I don't want to link my app container to a local DB but would rather use a volume to bring the data into the MySQL container. If what I'm trying to do makes sense could someone please help me. Below is my docker-compose file.
version: "3.9"
services:
app:
build:
context: .
dockerfile: dockerfiles/app/Dockerfile
depends_on:
- mysql
container_name: pedestalpro_app
volumes:
- ./:/var/www
- ./dockerfiles/php/local.ini:/usr/local/etc/php/conf.d/local.ini
env_file:
- .env
tty: true
nginx:
image: nginx:alpine
depends_on:
- app
container_name: pedestalpro_nginx
volumes:
- ./public/:/var/www/public
- ./dockerfiles/nginx/conf.d/:/etc/nginx/conf.d/
ports:
- "80:80"
- "443:443"
tty: true
mysql:
image: mysql:5.7.22
container_name: pedestalpro_mysql
volumes:
- pedestalpro_db:/var/lib/mysql
- ./dockerfiles/mysql/my.cnf:/etc/mysql/my.cnf
ports:
- "3307:3306"
env_file:
- .env.mysql
volumes:
pedestalpro_db:
I have a LAMP stack with laravel setup in docker-compose containers,everything seems to be working fine so far. I initially had a test database with the name of homestead setup in docker-compose.yml that was just there for testing now i want to change it to something more meaningful like my project name. let me show you my docker-compose.yml
version: '3'
networks:
laravel:
services:
site:
build:
context: .
dockerfile: nginx.dockerfile
container_name: nginx
ports:
- 80:80
volumes:
- ./src:/var/www/html:delegated
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7.29
container_name: mysql
restart: unless-stopped
tty: true
ports:
- 3306:3306
environment:
MYSQL_DATABASE: alis
MYSQL_USER: alis
MYSQL_PASSWORD: pass
MYSQL_ROOT_PASSWORD: pass
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
volumes:
- ./mysql:/var/lib/mysql
php:
build:
context: .
dockerfile: php.dockerfile
container_name: php
volumes:
- ./src:/var/www/html:delegated
networks:
- laravel
composer:
build:
context: .
dockerfile: composer.dockerfile
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- php
user: laravel
entrypoint: ['composer', '--ignore-platform-reqs']
networks:
- laravel
npm:
image: node:latest
container_name: npm
volumes:
- ./src:/var/www/html
ports:
- 3000:3000
- 3001:3001
working_dir: /var/www/html
entrypoint: ['npm']
networks:
- laravel
artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: artisan
volumes:
- ./src:/var/www/html:delegated
depends_on:
- mysql
working_dir: /var/www/html
user: laravel
entrypoint: ['php', '/var/www/html/artisan']
networks:
- laravel
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: always
container_name: phpmyadmin
depends_on:
- mysql
ports:
- "8081:80"
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: secret
networks:
- laravel
the problem is phpmyadmin is still using the old database and credentials , it tried the following solutions
docker-compose down
docker-compose down -v
then
docker-compose up -d
this didnt work.Out of frustration even thought this is probably not the best way to do it, I removed all containers, all images and then ran
docker-compose up -d
to re-install everything from scratch to my surprise phpmyadmin still picks up the old database config.Does anyone know the reason for this behaviour?
Your phpmyadmin service tries to connect as root:secret (MYSQL_ROOT_PASSWORD) while your mysql service defines root:pass (MYSQL_ROOT_PASSWORD) as admin user.
See the available configuration here:
https://hub.docker.com/_/phpmyadmin
https://hub.docker.com/_/mysql
If i try to build spring boot app using gradle without docker implementation, it works.
But when i try to integrate docker with flyway database integration it gives me error.
Error:
application.properties:
Below is my docker-compose.yml file:
version: '3.7'
services:
app:
container_name: dbz_rest_container
image: dbz-rest-image
build:
context: ./
dockerfile: Dockerfile
depends_on:
- db
ports:
- 7000:7000
networks:
- app-mysql
#===================FLYWAY=======================
flyway:
container_name: dbz_rest_flyway_container
image: flyway/flyway:6.3.1
command: -locations=/src/main/resources/db/migration:/flyway/sql -connectRetries=60 migrate
enabled: true
environment:
- FLYWAY_USER=root
- FLYWAY_PASSWORD=root
- FLYWAY_URL=jdbc:mysql://db:5432//dbz
- FLYWAY_SCHEMAS=dbz
volumes:
- ./src/main/resources/db/migration:/flyway/sql
depends_on:
- db
networks:
- app-mysql
#===================MYSQL=======================
db:
container_name: dbz_rest_db_container
image: mysql:latest
ports:
- 5432:3306
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=dbz
- MYSQL_USER=root
- MYSQL_PASSWORD=root
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
networks:
- app-mysql
networks:
app-mysql:
driver: bridge
Any help would be appreciated.
I'm using docker swarm to create a small infrastructure with different wordpress instances, I have an ingress (nginx) and different stacks (wp,mysql,wp). The docker-compose for each stack is like this:
version: "3.7"
networks:
foo-frontend:
foo-backend:
volumes:
db_data:
wordpress_data:
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
env_file:
- db.env
networks:
- foo-backend
ports:
- "3306:3306"
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html/wp-content
env_file:
- wp.env
networks:
- foo-frontend
- foo-backend
nginx:
depends_on:
- wordpress
image: nginx:latest
volumes:
- ./nginx:/etc/nginx/conf.d
- /etc/letsencrypt:/etc/letsencrypt
networks:
- foo-frontend
while the ingress is:
version: "3.7"
services:
nginx:
image: nginx:latest
ports:
- 80:80
- 443:443
volumes:
- ~/docker-vps/ingress/conf.d:/etc/nginx/conf.d
- /etc/letsencrypt:/etc/letsencrypt
networks:
- foo-frontend
networks:
frontend:
external: true
my problem is that I'm not able to connect to mysql with my client, I tried to use the container ip with the right port but is not working....is there a way to connect to mysql with a client?
many thanks
Consider as hostname the name of service. This is how services communicate. So, the hostname of database is db. Let me know the error message that occurs, because I don't have rating to comment your original question to know this.
How can i make the mysql database in this docker-compose reachable from "outside" - meaning that i can connect with a database administration tool for example on port 3306:
wordpress:
build: /Users/FabianL/wp-docker/
container_name: "kr-wp-container"
links:
- db:mysql
ports:
- 8080:80
- 3306:3306
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: biersaufen
You have opened port 8080 and 3306 from container "wordpress" and none from container "db"
Change your compose file to:
wordpress:
build: /Users/FabianL/wp-docker/
container_name: "kr-wp-container"
links:
- db:mysql
ports:
- 8080:80
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: biersaufen
ports:
- 3306:3306