Below is my docker-compose.yml which composing two docker files :
mysql.dockerfile
FROM mysql:8
ADD DockerImages/mysql /docker-entrypoint-initdb.d/
springapp.dockerfile
FROM maven:3.5-jdk-8 <br>
ADD target/users-mysql.jar users-mysql.jar<br>
ENTRYPOINT ["/usr/bin/java"]<br>
CMD ["-jar", "users-mysql.jar"]<br>
VOLUME /var/lib/spring-cloud/config-repo
docker-compose.yml
version: '3'
services:
springapp:
build:
context: .
dockerfile: springapp.dockerfile
expose:
- "8888"
ports:
- 8888:8888
links:
- mysqldb:mysqldb
networks:
- spring-demo-network
depends_on:
- mysqldb
environment:
- DATABASE_HOST=mysqldb
- DATABASE_USER=gl_user
- DATABASE_PASSWORD=global#123
- DATABASE_NAME=test_db
mysqldb:
build:
context: .
dockerfile: mysql.dockerfile
ports:
- 3306:3306
container_name : mysql-standalone
networks:
- spring-demo-network
environment:
MYSQL_DATABASE: test_db
MYSQL_USER: gl_user
MYSQL_PASSWORD: global#123
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_HOST: '%'
networks:
spring-demo-network:
driver: bridge
I am unable to connect to the database its throws multiple errors like
Error:
java.sql.SQLException: null, message from server: "Host '172.18.0.3'
is not allowed to connect to this MySQL server"
Caused by: java.net.ConnectException: Connection refused (Connection
refused)
Related
I configured PhpStorm for running tests from docker container in the IDE by clicking the Run button, but I got the following error when I run them:
Doctrine\DBAL\Exception\ConnectionException : An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
from .env:
database_host: sp_mysql
database_port: null
docker-compose:
version: "3.4"
services:
nginx:
container_name: sp_nginx
image: nginx
ports:
- 8080:80
volumes:
- ./docker/nginx/conf:/etc/nginx/conf.d/:ro
- ./var/log/nginx/:/var/log/nginx:cached
- ./web:/app/web
depends_on:
- php
networks:
- internal
php:
container_name: sp_php
image: sp/php
build:
context: ./
dockerfile: ./docker/php/Dockerfile
volumes:
- ./:/app
- ~/.ssh:/root/.ssh
depends_on:
- mysql
networks:
- internal
mysql:
container_name: sp_mysql
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- ./docker/mysql/conf:/etc/mysql/conf.d
- mysql_data:/var/lib/mysql
ports:
- 3308:3306
networks:
- internal
networks:
internal:
volumes:
mysql_data:
But if I go directly to php container it works:
docker exec -it sp_php vendor/bin/phpunit
i setup my ide Docker/PHPUnit config by this guide:
https://www.youtube.com/watch?v=I7aGWO6K3Ho&t=240s
To solve this problem i set this in .env
host: "172.19.0.1" // docker network bridge gateway
port: "3308" // my db port from docker-compose
https://docs.docker.com/network/network-tutorial-standalone/
I've created docker-compose.yml file with the following content. My docker service runs ok, docker run hello-world works just fine and after running command
docker-compose build && docker-compose up -d
I see no errors but in the browser I'm getting an error Unable to connect when I go to localhost:8000
version: '3'
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
ports:
- "8000:80"
depends_on:
- php
- mysql
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
mysql:
image: mysql:8.0
container_name: mysql
restart: unless-stopped
ports:
- "3306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: db
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: root
SERVICE_NAME: mysql
When I run command
docker-compose exec php ls
I can see the list of files and folders
I've a docker-compose file with contents like this
# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)
version: '3'
services:
john:
build:
context: .
dockerfile: Dockerfiles/cowboys/John/Dockerfile
args:
- COWBOY_NAME_JOHN
- CONTAINER_PORT_JOHN
ports:
- "8081:8081" # Forward the exposed port on the container to port on the host machine
restart: unless-stopped
networks:
- fullstack
depends_on:
db:
condition: service_healthy
links:
- db
philip:
build:
context: .
dockerfile: Dockerfiles/cowboys/Philip/Dockerfile
args:
- COWBOY_NAME_PHILIP
- CONTAINER_PORT_PHILIP
ports:
- "8085:8085"
restart: unless-stopped
networks:
- fullstack
depends_on:
db:
condition: service_healthy
links:
- db
db:
build:
context: Dockerfiles/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
restart: on-failure
networks:
- fullstack
ports:
- "3306:3306"
healthcheck:
test: mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD
networks:
fullstack:
driver: bridge
When I run docker-compose up --build ( or docker-compose up --build --force-recreate to reinitialise db on next run), I get an error like this
sam_1 | panic: Put "http://john:8081/shoot": dial tcp 172.20.0.7:8081: connect: connection refused
john is one of the services name under services. Here is complete screenshot:
Link to repository where all details could be seen including environment variables & relevant Dockerfile(s) - https://github.com/sitetester/distributed-cowboys
Could anyone please explain how to get rid of this error ? It just happens/occurs at random time. Not sure how to fix it. Thanks in advance!
Solved: removing existing containers/images and pruning docker volumes fixed the issue.
i keep getting this error for my docker build :
"0":"SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not
known","1":"#1 Zend_Db_Adapter_Pdo_Mysql->_connect()
However, if i docker-compose down and then docker-compose up it works for a coupe of mins and then crashes again.
Is this to do with the network cache. Is there somthing that i should clear ?
this is my docker compose file. --you will note that i do provide a link to database:
version: "2"
services:
web:
build: ./developer
ports:
- "8228:80"
links:
- database
volumes:
- total-sync:/var/www/html:nocopy
env_file:
- env
database:
image: mysql:5.7
ports:
- "3907:3316"
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=default
volumes:
- ./dump:/docker-entrypoint-initdb.d
- ./db/mysqlconf:/etc/mysql/conf.d
- ./mysqladata2.3.4:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8583:80"
links:
- database
volumes:
total-sync:
external: true
dbdatas:
driver: local
I'm trying to connect to a mysql container from within another container on the same network specified by a compose file.
version: "2"
services:
web:
build:
context: .
dockerfile: nginx/Dockerfile
ports:
- "8080:80"
volumes:
- ./data:/srv
php:
build:
context: .
dockerfile: php-fpm/Dockerfile
volumes:
- ./data:/srv
mysql:
image: mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_USER=dummy_user
- MYSQL_PASSWORD=12345
I'm not really sure what the connection parameters would be if I'm trying to connect to the mysql container from the php container.
Specifically, what are the host and port for the mysql container from within another container of the same docker-compose network?
I've tried host: mysql port: 3306, but that doesn't seem to work.
You should link the containers. Add ports section to mysql container and links section to php container.
version: "2"
services:
web:
build:
context: .
dockerfile: nginx/Dockerfile
ports:
- "8080:80"
volumes:
- ./data:/srv
php:
build:
context: .
dockerfile: php-fpm/Dockerfile
links:
- mysql:mysql
volumes:
- ./data:/srv
mysql:
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_USER=dummy_user
- MYSQL_PASSWORD=12345
With that configuration you'll be able to access mysql container from php with mysql:3306
A bit of documentation: https://docs.docker.com/compose/networking/#updating-containers
You don't share port for mysql
add to your docker-compose.yml to mysql service:
ports:
- "3306:3306"
UPD
try to set environments of your mysql container
MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: dev
MYSQL_PASSWORD: 123456
MYSQL_DATABASE: myapp
and
$link = mysqli_connect("mysql", "dev", "123456", "myapp");
should works fine