Connect MySQL container volume to existing data - mysql

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:

Related

Node.js service is unable to connect with Mysql using docker composer

I am trying to run 2 Node.js and 2 mysql services with docker composer, both of Node.js services are unable to connect with Mysql services while I was able to login into both Mysql containers with respective credentials.
Node.js service giving the following error:
original: Error: connect ECONNREFUSED 172.22.0.2:3308
enter image description here
I have also successfully connected a PHP script with both of the MySQL services.
My docker-composer file :
version: '3.8'
services:
genreapp:
depends_on:
- genresqldb
build: ./genre
#command: sh -c './wait-for-it.sh -t 0 genresqldb:$DB_PORT_GENRE'
restart: unless-stopped
env_file: ./.env
links:
- genresqldb
volumes:
- ./:/app
ports:
- $NODE_PORT_GENRE:$NODE_PORT_GENRE
environment:
- DB_USERNAME=$DB_USERNAME
- DB_PASSWORD=$DB_PASSWORD_GENRE
- DB_NAME=$DB_NAME_GENRE
- DB_PORT=$DB_PORT_GENRE
- DIALTEC=$DIALTEC
- APP_PORT=$NODE_PORT_GENRE
- HOST=$HOST
- DB_HOST_GENRE=genresqldb
stdin_open: true
tty: true
movieapp:
depends_on:
- moviesqldb
build: ./movie
restart: unless-stopped
env_file: ./.env
links:
- moviesqldb
volumes:
- ./:/app
#command: sh -c './wait-for-it.sh -t 0 moviesqldb:$DB_PORT_MOVIE'
ports:
- $NODE_PORT_MOVIE:$NODE_PORT_MOVIE
environment:
- DB_USERNAME=$DB_USERNAME
- DB_PASSWORD=$DB_PASSWORD_MOVIE
- DB_NAME=$DB_NAME_MOVIE
- DB_PORT=$DB_PORT_MOVIE
- DIALTEC=$DIALTEC
- APP_PORT=$NODE_PORT_MOVIE
- HOST=$HOST
- DB_HOST_GENRE=moviesqldb
stdin_open: true
tty: true
genresqldb:
image: mysql:5
# restart: unless-stopped
env_file: ./.env
environment:
- MYSQL_ROOT_PASSWORD=$DB_PASSWORD_GENRE
- MYSQL_DATABASE=$DB_NAME_GENRE
expose:
- $DB_PORT_GENRE
ports:
- $DB_PORT_GENRE:$DB_PORT
volumes:
- db:/var/lib/mysqli
moviesqldb:
image: mysql:5
# restart: unless-stopped
env_file: ./.env
environment:
- MYSQL_ROOT_PASSWORD=$DB_PASSWORD_MOVIE
- MYSQL_DATABASE=$DB_NAME_MOVIE
expose:
- $DB_PORT_MOVIE
ports:
- $DB_PORT_MOVIE:$DB_PORT
volumes:
- db:/var/lib/mysqli
volumes:
db:
Just by using Version 3 resolve the issue.

mysql database name and credentials inside docker container not updating

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

flask can not connect mysql in docker compose

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

use client to connect to mysql inside a docker network

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.

Docker-Compose phpmyadmin and mysql together for local development

version: "3.1"
services:
redis:
image: redis:alpine
container_name: larablog-redis
the code was working fine until I added the following services
db:
image: mysql:8.0
container_name: larablog-mysql
working_dir: /application
volumes:
- ./src:/application
environment:
- MYSQL_ROOT_PASSWORD=rootpass
- MYSQL_DATABASE=larablog
- MYSQL_USER=larauser
- MYSQL_PASSWORD=larauserpass
ports:
- "8890:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_ARBITRARY: 1
MYSQL_USER: larauser
MYSQL_PASSWORD: larauserpass
MYSQL_ROOT_PASSWORD: rootpass
ports:
- "80:80"
links:
# for mysql container
- "db:db"
volumes:
db:
driver: "local"
I am trying to follow a method I found on https://gotechnies.com/docker-compose-yml-mysql-phpmyadmin/ however I cannot start from scratch because I need the laravel framework as well
webserver:
image: nginx:alpine
container_name: larablog-webserver
working_dir: /application
volumes:
- ./src:/application
- ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8888:80"
php-fpm:
build: phpdocker/php-fpm
container_name: larablog-php-fpm
working_dir: /application
volumes:
- ./src:/application
- ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.1/fpm/conf.d/99-overrides.ini
I get this error when i try to run docker-compose up -d ...
ERROR: yaml.parser.ParserError: while parsing a block mapping
in ".\..\docker-compose.yml", line 27, column 6
expected <block end>, but found '<block mapping start>'
in ".\..\docker-compose.yml", line 35, column 9
How can I overcome this error?