How to pass containers ip address to another container? - mysql

I am correctly trying to pass a container ip address to another
Here is my docker-compose.yml file
app-phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- app-mysql
ports:
- '80'
container_name: app-phpmyadmin
environment:
PMA_HOST: app-mysql
PMA_PORT: 3306
app-mysql:
image: mysql:8
container_name: app-mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_HOST: app-phpmyadmin <<<< doesnt work see below why
ports:
- 3306
When passing app-phpmyadmin instead of passing the ip it actually writes "app-phpmyadmin" as a string. On selecting use, host from mysql.user the hostname is "app-phpmyadmin" instead of the ip.
Is there a way I can do this in the docker-compose file or would i have to come up with a bash script that does it?

Actually, you can have try to use static network address
version: '3.4'
services:
app-phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- app-mysql
ports:
- '81'
container_name: app-phpmyadmin
environment:
PMA_HOST: app-mysql
PMA_PORT: 3306
networks:
app-net:
ipv4_address: 10.5.0.5
app-mysql:
container_name: app-mysql
image: mysql:8
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_HOST: 10.5.0.5
ports:
- 3306
networks:
app-net:
ipv4_address: 10.5.0.6
networks:
app-net:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/16

try this
version: '3'
services:
app-phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- app-mysql
ports:
- '8081:80'
container_name: app-phpmyadmin #name expose at list: docker ps
hostname: app-phpmyadmin #hostname in network docker
environment:
PMA_HOST: app-mysql
PMA_PORT: 3306
PMA_USER: "root"
PMA_PASSWORD: "root"
app-mysql:
image: mysql:5.7 #downgrade beacause mysql 8 generate error with phpmyadmin. error: caching_sha2_password
container_name: app-mysql #name expose at list: docker ps
hostname: app-mysql #hostname in network docker
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306

Related

Docker / phpmyadmin: Unable to connecto

I have the following docker-compose file.
version: "3.7"
services:
main:
container_name: buspack_main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- ${API_PORT}:${API_PORT}
command: bash -c "npm install --save && npm run start:dev"
env_file:
- .env
networks:
- webnet
depends_on:
- db
links:
- db
restart: always
db:
container_name: buspack_db
image: mysql:5.7.33
networks:
- webnet
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: "backendnest"
ports:
- 23306:3306
restart: always
volumes:
- ./db:/var/lib/mysql:rw
phpmyadmin:
container_name: buspack_phpmyadmin
image: phpmyadmin/phpmyadmin
depends_on:
- db
restart: always
ports:
- '8030:80'
environment:
PMA_HOST: buspack_db
MYSQL_ROOT_PASSWORD: 123456
networks:
webnet:
volumes:
db: {}
My problem Is that when I try to login with phpmyadmin I get the following error:
mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
And also
mysqli::real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
Is there something I'm missing?
networks:
- webnet
add webnet network to phpmyadmin service
There are more options to solve the connection.
Add phpmyadmin container to webnet network as Def Soudani suggests. The YML example provided overrides the default network. Since the phpMyAdmin is lacking the network config, it won't be on the webnet docker network by default.
Remove all networks related config from the YML. Since compose already creates a default network (docs) for the containers within one YML file.
Use the PMA_ARBITRARY=1 (docs )which allows you to enter a database server hostname on login form.
Example of my YML setup with custom bridge networking:
version: '3.8'
services:
mysql:
image: mysql:5.7
container_name: myapp-mysql-db
restart: unless-stopped
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
volumes:
- ./database/dbdata:/var/lib/mysql
networks:
- myapp-network
phpmyadmin:
image: phpmyadmin:latest
container_name: phpmyadmin
ports:
# 8080 is the host port and 80 is the docker port
- 8080:80
environment:
PMA_HOST: mysql
UPLOAD_LIMIT: 20M
MYSQL_USERNAME: ${DB_USERNAME}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
restart: unless-stopped
networks:
# define your network where all containers are connected to each other
- myapp-network
networks:
myapp-network:
driver: bridge

Cannot resolve hostname in docker desktop windows

I have setup docker-compose to run phpmyadmin and mysql. Here is my docker-compose.yml
version: "3.9"
networks:
local_web_network:
driver: bridge
volumes:
mysql_data:
driver: local
services:
mysql:
image: mysql:8.0
container_name: local_mysql
restart: always
tty: true
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: local_master
MYSQL_ROOT_PASSWORD: secret
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
volumes:
- mysql_data:/var/lib/mysql
networks:
- local_web_network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: local_phpmyadmin
restart: always
tty: true
ports:
- "8083:80"
environment:
PMA_HOST: local_mysql
PMA_ABSOLUTE_URI: http://db.test
MYSQL_ROOT_PASSWORD: secret
networks:
- local_web_network
extra_hosts:
- 'db.test:127.0.0.1'
I have configured the windows hosts file with the below settings
127.0.0.1 db.test
I have successfully run the docker containers without any errors. I have tried accessing the http://127.0.0.1:8083 or http://db.test:8083 it works and displays the phpmyadmin login page.
But the problem here is, i am unable to access the page without specifying the port i.e http://db.test. How do i get this working without specifying the port?

Docker-compose database table reset

I am new to docker technology. I use docker-compose in my project and every time I start docker-compose my database tables are reset.
docker-compose.yaml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: ./docker/php/Dockerfile
image: php-7.4.1-extended
container_name: php
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: nginx
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: root
MYSQL_USER: root
MYSQL_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker/mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
I found an example of docker-compose.yaml file from some sources and made changes on it. What should I do to fix the problem.
Your docker image is storing its data within the image, which isn't persisted. You need to tell Docker to store the data on a volume, instead. This is a bit version-dependent, but something like
#MySQL Service
db:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: root
MYSQL_USER: root
MYSQL_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker/mysql/my.cnf:/etc/mysql/my.cnf
- "./.mysql-data/db:/var/lib/mysql"
And then delete
dbdata:
driver: local

Docker - PHP/MySQL - php_network_getaddresses: getaddrinfo failed: Name or service not known

I have the following docker-compose.yml, mostly copied from some tutorial (https://medium.com/#romaricp/the-perfect-kit-starter-for-a-symfony-4-project-with-docker-and-php-7-2-fda447b6bca1):
version: '3'
services:
apache:
build: .docker/apache
container_name: sf4_apache
ports:
- 80:80
- 443:443
volumes:
- .docker/config/vhosts:/etc/apache2/sites-enabled
- .:/home/wwwroot/sf4:cached
depends_on:
- php
mysql:
image: mysql:5.7
container_name: sf4_mysql
command: "--default-authentication-plugin=mysql_native_password"
restart: always
ports:
- 3306:3306
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
php:
build: .docker/php
container_name: sf4_php
volumes:
- .:/home/wwwroot/sf4
environment:
- maildev_host=sf4_maildev
depends_on:
- maildev
- mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
environment:
PMA_HOST: sf4_mysql
PMA_PORT: 3306
ports:
- 8080:80
links:
- mysql
maildev:
image: djfarrelly/maildev
container_name: sf4_maildev
ports:
- 8001:80
Now my problem is, that I cannot manage to connect to MySQL. Wheter from PHP nor PhpMyAdmin. I've tried the following connection strings, but none of them work:
DATABASE_URL=mysql://root:root#mysql:3306/sf4
DATABASE_URL=mysql://sf4:sf4#mysql:3306/sf4
DATABASE_URL=mysql://root:root#sf4_mysql:3306/sf4
DATABASE_URL=mysql://sf4:sf4#sf4_mysql:3306/sf4
... and hundreds more
But I always get:
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known
Or
SQLSTATE[HY000] [2002] No route to host
Or
SQLSTATE[HY000] [2002] Connection refused
Thanks to #emix and #LinPy, who helped me out in the comments, here is the fixed configuration:
version: '3'
services:
apache:
build: .docker/apache
container_name: sf4_apache
ports:
- 80:80
- 443:443
volumes:
- .docker/config/vhosts:/etc/apache2/sites-enabled
- .:/home/wwwroot/sf4:cached
depends_on:
- php
mysql:
image: mysql:5.7
container_name: sf4_mysql
#command: "--default-authentication-plugin=mysql_native_password"
#restart: always
ports:
- 3306:3306
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
php:
build: .docker/php
container_name: sf4_php
volumes:
- .:/home/wwwroot/sf4
environment:
- maildev_host=sf4_maildev
depends_on:
- maildev
- mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- 8080:80
links:
- mysql
maildev:
image: djfarrelly/maildev
container_name: sf4_maildev
ports:
- 8001:80
Connection string:
DATABASE_URL=mysql://sf4:sf4#mysql/sf4

Docker - Use same network and database for another app?

I've set up a development environment for an app on docker in which I'm using a specific network to connect the app to the database
version: '3'
services:
app:
image: appimg
container_name: appwww
build:
context: .
dockerfile: docker/Dockerfile
volumes:
- .:/var/www/html
ports:
- 9000:80
depends_on:
- mysql
network:
- appnet
mysql:
image: mysql:5.7
container_name: appdb
environment:
MYSQL_DATABASE: homestead
MYSQL_ROOT_PASSWORD: root
MYSQL_USERNAME: homestead
MYSQL_PASSWORD: password
volumes:
- dbdata:/var/lib/mysql
ports:
- 3306:3306
network:
- appnet
volumes:
dbdata:
driver: 'local'
network:
- appnet
My question is can I use the same network to connect to the same database from another app? something like this perhaps?
version: '3'
services:
anotherapp:
image: anotherappimg
container_name: anotherappwww
build:
context: .
dockerfile: docker/Dockerfile
volumes:
- .:/var/www/html
ports:
- 9000:80
depends_on:
- mysql
network:
- appnet
mysql:
image: mysql:5.7
container_name: anotherappdb
environment:
MYSQL_DATABASE: homestead
MYSQL_ROOT_PASSWORD: root
MYSQL_USERNAME: homestead
MYSQL_PASSWORD: password
volumes:
- dbdata:/var/lib/mysql
ports:
- 3306:3306
network:
- appnet
volumes:
dbdata:
driver: 'local'
network:
- appnet
Yes it's possible, you can have the setup done like this:
docker-compose.base.yml:
version: '3'
services:
mysql:
image: mysql:5.7
container_name: anotherappdb
environment:
MYSQL_DATABASE: homestead
MYSQL_ROOT_PASSWORD: root
MYSQL_USERNAME: homestead
MYSQL_PASSWORD: password
volumes:
- dbdata:/var/lib/mysql
ports:
- 3306:3306
network:
- appnet
volumes:
dbdata:
driver: 'local'
network:
- appnet
docker-compose.app.yml:
version: '3'
services:
app:
image: appimg
container_name: appwww
build:
context: .
dockerfile: docker/Dockerfile
volumes:
- .:/var/www/html
ports:
- 9000:80
depends_on:
- mysql
network:
- appnet
docker-compose.anotherapp.yml:
version: '3'
services:
anotherapp:
image: anotherappimg
container_name: anotherappwww
build:
context: .
dockerfile: docker/Dockerfile
volumes:
- .:/var/www/html
ports:
- 9000:1080
depends_on:
- mysql
network:
- appnet
Then you can start everything using:
docker-compose -f docker-compose.base.yml -f docker-compose.app.yml -f docker-compose.anotherapp.yml up -d
If you want to start only anotherapp, you'll remove -f docker-compose.app.yml from the previous command.
NOTE: I've changed the port mapping for anotherapp since they'd conflict with app if started at the same time.