Can't connect to MySQL container on AWS ECS - mysql

I can't seem to connect to a MySQL container in AWS ECS. Here's the docker-compose.yml I'm using with ecs-cli compose up:
version: '2'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
logging:
driver: awslogs
options:
awslogs-group: "mylogs"
awslogs-region: "us-east-2"
awslogs-stream-prefix: "db"
mem_limit: 300000000
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
logging:
driver: awslogs
options:
awslogs-group: "mylogs"
awslogs-region: "us-east-2"
awslogs-stream-prefix: "web"
mem_limit: 300000000
The error I get it is:
MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known
Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 22
Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 22
This seems to work fine locally but not in AWS ECS. Do I need to do something else for AWS?

The depends_on field is not supported in ECS. Your wordpress container is starting before your db container opens its port, hence the error.
You'll need to introduce a wrapper script to wait for the port to become available.

Related

Docker / MySQL - Doctrine Migration - Access Database outside container

Hello :) here is my docker compose :
version: "3.8"
services:
web-server:
build: .
volumes:
- ./:/var/www/html/
ports:
- "8000:80"
links:
- "db:db"
db:
image: mysql:5.6
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8001:80"
links:
- "db:db"
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: root
As you can see, when i go to phpmyadmin with http://localhost:8001/ i have :
PhpMyAdmin Dashboard
Doctrine Migation is installed in my project, so when i try :
./vendor/bin/doctrine-migrations status
Errors are :
In AbstractMySQLDriver.php line 112:
An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
In Exception.php line 18:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
In PDOConnection.php line 39:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
In PDOConnection.php line 39:
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
Connexion in my env file is :
BDD = pdo-mysql://root:root#db:3306/playlist_maker_multi
I expose port 3306 on my docker-compose so i don't understand why outside my container i can't access to my Database ?
Thanks everybody, if you have explanations.
Have a nice day.
Camille
Could you try running it with this config
I renamed the db service to mysql, explicitly added a network and made your php service depend on mysql, so it will restart untill it establishes a connection, just in case the php container starts running before the mysql container
version: "3.8"
services:
web-server:
build: .
volumes:
- ./:/var/www/html/
ports:
- "8000:80"
links:
- "mysql:db"
mysql:
image: mysql:5.6
container_name: db
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- net
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8001:80"
links:
- "mysql:db"
depends_on:
- mysql
restart: on-failure
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: root
networks:
- net
networks:
net:
driver: bridge
For applications running on your host machine but outside of your Docker network (i.e. not containerised) that need to connect to a container, you can use localhost instead of db, as the DNS name db is only valid inside the Docker Compose network.
With:
mysql:
image: mysql:5.6
container_name: db
ports:
- "3306:3306"
you are creating a path from locahost:3306 (or MACHINEIP:3306 if on another machine) to the service on mysql:3306 inside the network.
#clarj, do you mean like that ?
version: "3.8"
services:
web-server:
build: .
volumes:
- ./:/var/www/html/
ports:
- "8000:80"
links:
- "localhost:db"
mysql:
image: mysql:5.7
container_name: db
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8001:80"
links:
- "localhost:db"
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: root

phpMyAdmin error "No address associated with hostname" in Docker?

I am trying to containerize my Laravel project. I have the following services in my docker-compose.yml
laravel_app
database
nginx
phpmyadmin
All of them are running okay except PHPMyAdmin. It gets built and does serve the phpMyAdmin login page but I get the following errors when I enter the credentials.
Cannot log in to the MySQL server
mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: No address associated with hostname
mysqli::real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No address associated with hostname
Here is my docker-compose.yml snippet that includes my database service and phpmyadmin service
#The Database
database:
container_name: mysql_database
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=dev-db"
- "MYSQL_USER=phpmyadmin"
- "MYSQL_PASSWORD=phpmyadmin"
- "MYSQL_ROOT_PASSWORD=123456"
ports:
- 8991:3306
#PHPMyAdmin Service
phpmyadmin:
container_name: phpmyadmin
image: phpmyadmin/phpmyadmin
ports:
- "8992:80"
depends_on:
- database
environment:
- "PMA_HOST:database"
- "MYSQL_USER:phpmyadmin"
- "MYSQL_PASSWORD:phpmyadmin"
- "MYSQL_ROOT_PASSWORD:123456"
- "UPLOAD_LIMIT:3000000000"
# redis
cache:
image: redis:3.0-alpine
volumes:
dbdata:
For that to work, you need to at least create a bridge network and connect phpmyadmin and your db to it.
Something like this should work:
version: "3"
volumes:
dbdata:
networks:
backend:
driver: bridge
services:
database:
container_name: mysql_database
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=dev-db"
- "MYSQL_USER=phpmyadmin"
- "MYSQL_PASSWORD=phpmyadmin"
- "MYSQL_ROOT_PASSWORD=123456"
ports:
- 8991:3306
networks:
- backend
phpmyadmin:
container_name: phpmyadmin
image: phpmyadmin/phpmyadmin
ports:
- "8992:80"
depends_on:
- database
environment:
- "PMA_HOST=database"
- "MYSQL_USER=phpmyadmin"
- "MYSQL_PASSWORD=phpmyadmin"
- "MYSQL_ROOT_PASSWORD=123456"
- "UPLOAD_LIMIT=3000000000"
networks:
- backend
Also I do not recommend exposing DB port
EDITED: Fixed docker-compose file
Like Fominykh Maxim mentioned, my error was caused by syntax error while assigning environment value at phpmyadmin service. I had used PMA_HOST:database instead of PMA_HOST=database

Laravel with docker database error: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not resolve

I know there're similar questions but the answers do not help.
I have this server where I have previously installed the following two repositories:
evertramos / docker-wordpress
evertramos / docker-compose-letsencrypt-nginx-proxy-companion.
Now I have added a Laravel Application. The Laravel application is now working, the domain is correctly resolved by the Nginx proxy and Letsencrypt is working properly.
However, the problem is now with the database connection. When I run php artisan migrate --seed the following errors show up:
In Connection.php line 671:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed:
Name does not resolve (SQL: select * from information_schema.tables
where table_schema = mydb and table_name = migrations and table_type =
'BASE TABLE')
In Connector.php line 70:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed:
Name does not resolve
In Connector.php line 70:
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name
does not resolve
This is my docker-compose.yml file:
version: '3'
networks:
default:
external:
name: ${NETWORK}
services:
nginx:
image: nginx:stable-alpine
container_name: app-webserver
expose:
- "80"
- "443"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
environment:
- VIRTUAL_HOST=${VIRTUAL_HOST}
- LETSENCRYPT_HOST=${LETSENCRYPT_HOST}
- LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL}
depends_on:
- php
- mysql
mysql:
image: mysql:5.7.29
container_name: app-mysql
restart: unless-stopped
tty: true
expose:
- "3306"
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
php:
build:
context: .
dockerfile: php.dockerfile
container_name: app-php
volumes:
- ./src:/var/www/html
environment:
- VIRTUAL_HOST=${VIRTUAL_HOST}
- LETSENCRYPT_HOST=${LETSENCRYPT_HOST}
- LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL}
expose:
- "9000"
composer:
image: composer:latest
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- php
npm:
image: node:13.7
container_name: npm
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: ['npm']
artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: artisan
volumes:
- ./src:/var/www/html
depends_on:
- mysql
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
Inside the Laravel .env file I have tried all possible values for DB_HOST:
DB_HOST=localhost
DB_HOST=127.0.0.1
DB_HOST=mysql
DB_HOST=app-mysql
Currently, I have it set up for the MySQL container name:
DB_HOST=app-mysql
Other values are:
DB_CONNECTION=mysql
DB_PORT=3306
And I still get that error.
I have tried the ping command from the app-php container to the app-mysql container as follows:
$ docker exec -ti app-php ping app-mysql
I do get response:
64 bytes from 172.18.x.x: seq=0 ttl=64 time=0.132 ms
64 bytes from 172.18.x.x: seq=1 ttl=64 time=0.240 ms
64 bytes from 172.18.x.x: seq=2 ttl=64 time=0.207 ms
64 bytes from 172.18.x.x: seq=3 ttl=64 time=0.208 ms
However, if I run:
$ docker-compose run --rm artisan ping app-mysql
I get the following error:
Command "ping" is not defined.
Now if I run docker-compose up in order to see what's going on (without -d), I see the following, as for MySQL:
app-mysql | 2020-07-29T15:43:24.159355Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
app-mysql | 2020-07-29T15:43:24.159523Z 0 [Note] IPv6 is available.
app-mysql | 2020-07-29T15:43:24.159582Z 0 [Note] - '::' resolves to '::';
app-mysql | 2020-07-29T15:43:24.159688Z 0 [Note] Server socket created on IP: '::'.
Does anybody out there know how to fix this? Why can't Artisan or the PHP container connect to the database container?
workaround
I solved this using Laradock instead
In short: add Sail before PHP command.
Detailed:
When you prompt a command, you use the local PHP library. That's why you got an error because your local PHP is addressing to docker-mysql connection, on local machine you don't haven a connection like this. When you are using Laravel Sail, you should prompt the command
sail PHP artisan migrate
which means, you are prompting from Docker's PHP library
Since you are doing container_name: app-mysql
inside of docker compose network you should use host name app-mysql
the easies way to check is to connect to php container and try ping app-mysql and then use mysql console to test connection. In this way you at least can find where is the problem.
by the way, since it is docker compose - you must do docker compose down and docker compose up -d each time you are doing changes in docker-compose.yml
I hope this could be helpful for those who have gone through the solution and not yet succeeded. I had similar problem and tried to change the DB_HOST localhost to 127.0.0.1 also with the name of the container/db host (as mentioned in docker-compose), yet I could not get this error resolved. So I updated my .env file like this:
DB_CONNECTION=mysql
DB_HOST=host.docker.internal
DB_PORT=3308 or any other port
DB_DATABASE=your_database_name
DB_USERNAME=your_user_name
DB_PASSWORD=your_password
which started working. I am not assuring it works 100% but you could give a try for this in case other option are not working.
Cheers!

WordPress docker compose won't start mysql

I am trying to set up a docker container for WordPress development using docker-compose.yml which looks like this:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress
MYSQL_USER: root
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- 8080:80
- 443:443
volumes:
- ./data:/data # Required if importing an existing database
- ./:/var/www/html # Theme development
environment:
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:80
volumes:
data: {}
And I'm getting errors like this:
wordpress_1 | Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 22
wordpress_1 |
wordpress_1 | Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 22
wordpress_1 |
wordpress_1 | MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known
I tried clearing my images and containers but no luck. Any idea why this happens?
Try providing links option for both wordpress and phpmyadmin.
links:
- db:mysql
Edit: Check this question. Add mysql to the db. I don't if this solves, I have to try out once I go home later.

Wordpress not work with docker compose

This is my docker-compose.yml
version: '2'
services:
wordpress:
image: wordpress:4.6.1-php5.6-apache
container_name: wordpress
volumes:
- ./projects/:/home/docker/
working_dir: /home/docker/
ports:
- "8000:80"
environment:
WORDPRESS_DB_PASSWORD: secret
links:
- database-mysql
database-mysql:
image: mysql:5.7
container_name: mysqldb
ports:
- "3306:3306"
volumes:
- ./backups/mysqldb/:/var/lib/mysql/
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: wordpress
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: phpmyadmin
ports:
- "8080:80"
environment:
PMA_USER: root
PMA_PASSWORD: secret
PMA_HOST: database-mysql
links:
- database-mysql
When I run: docker-compose up, the log error shows:
MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not know
Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 19
Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 19
2016-11-11 04:14:33,648 INFO success: php-fpm entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2016-11-11 04:14:33,648 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
What am I doing wrong?
is's too long to comment, therefore i create another answer
try this and wait for 2 minutes, after that access localhost:8000
version: '2'
services:
db:
image: mysql:5.7
volumes:
- "./.data/db:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
links:
- db
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: phpmyadmin
ports:
- "8080:80"
environment:
PMA_USER: root
PMA_PASSWORD: secret
PMA_HOST: database-mysql
links:
- db
On Windows 10 Home edition (so using docker-toolbox) I also ran into this problem.
It seems that the wordpress image now required the environment setting:
WORDPRESS_DB_HOST: db:3306.
After adding this, the wordpress container can connect to the database.
Also with Windows 10 Home edition I can't get the port mapping to work for localhost. I need to lookup the ip-adress via docker-machine ip and connect to the port there.
On Linux Mint (based on Ubuntu) this does not seem required.
So I expect this to be an installation problem with docker-toolbox or Windows 10 Home Edition.