Can't access Docker database using IP (only localhost) - docker

I'm trying to setup Docker to allow connections from other devices on my network. Currently to access Docker I visit localhost on my computer. I'm trying to connect using my computer's local IP (192.168.0.140), which lets me see my files but not connect to my database.
I assume this is a problem with my configuration but I don't know enough about Docker to troubleshoot it.
version: '2'
services:
webserver:
build: ./docker/webserver
image: localdev
ports:
- '80:80'
- '443:443'
volumes:
- ./www:/var/www/html
links:
- db
db:
image: mysql:5.6
ports:
- 3306
volumes:
- ./db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=secret
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
links:
- db
environment:
PMA_HOST: db
PMA_PORT: 3306
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: secret
ports:
- '8080:80'
Any help would be greatly appreciated.

Maybe you should try like this :
version: '2'
services:
webserver:
build: ./docker/webserver
image: localdev
ports:
- '0.0.0.0:80:80' # Map container port 80 on your "public" ip on port 80, it will be available on the network
- '0.0.0.0:443:443' # Same for port 443
volumes:
- ./www:/var/www/html
links:
- db
db:
image: mysql:5.6
# ports:
# - 3306 # You don't need to map port 3306 because mysql already expose this port for others containers, unless you wan't to access to your mysql directly
volumes:
- ./db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=secret
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
#links:
# - db # Not necessary, with docker-compose v2 every containers are in the same network and see each other
environment:
PMA_HOST: db
PMA_PORT: 3306
MYSQL_USER: root
MYSQL_ROOT_PASS
WORD: secret
ports:
- 'localhost:8080:80' # to keep your phpmyadmin only available from localhost.

The reason you are able to connect to the other containers is because you have mapped the ports between the container and the host.
For the database you are using the short port syntax:
ports:
- 3306
This will choose a random port on the host. To be able to connect to the database, use the long form syntax:
ports:
- '3306:3306'
In that case, you will be able to connect to the database on localhost:3306

Related

Docker. Phpmyadmin does not see the port assigned to it

I am using the following docker configuration:
version: "3"
services:
database:
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 1009
restart: "always"
phpmyadmin:
image: phpmyadmin
ports:
- "8080:80"
environment:
PMA_HOST: database
restart: "always"
depends_on:
- database
It works, but if I change the mysql port for my PC to - "3360:3306" (for example 3306 may be busy with the main mysql service running on the computer) - it stops working. If I do this, I will be able to connect to mysql from my computer in CMD like mysql -u root -P 3360 -p, but phpmyadmin will give the error - connection refused.
No matter what I do, I see this error. For example I used the following docker configuration:
version: "3"
services:
database:
image: mysql
networks:
- my-network
ports:
- "3360:3306"
environment:
MYSQL_ROOT_PASSWORD: 1009
restart: "always"
phpmyadmin:
image: phpmyadmin
links:
- database
networks:
- my-network
ports:
- "8080:80"
environment:
PMA_HOST: database
PMA_PORT: 3360
restart: "always"
depends_on:
- database
networks:
my-network:
Even all of this didn't work. Phpmyadmin gave out one inscription when trying to connect - Connection refused all the time. But it still worked in the terminal.
Why can't I use a different port for phpmyadmin in docker?

Second node.js can't connect to mysql in docker compose

At the moment, I'm trying to set up my docker-compose file to run multiple (two) Node.js API's.
The first Node.js server connects fine to the database.
The second Node.js server keeps throwing this error
original: Error: connect ECONNREFUSED 172.29.0.3:3307
So my question is how to run multiple Node.js API's in docker?
This is my docker-compose.yaml:
version: '3.7'
services:
ISAAC-floor-database:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: isaac
ports:
- "3306:3306"
volumes:
- ./sql-scripts:/docker-entrypoint-initdb.d
ISAAC-sensor-database:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: isaac
ports:
- "3307:3307"
volumes:
- ./sql-scripts:/docker-entrypoint-initdb.d
ISAAC-floor-back-end:
image: jjuless/isaac-floor-back-end
environment:
DB_HOST: ISAAC-floor-database
DB_USER: root
DB_PASSWORD: isaac
DB_DATABASE: isaac
DB_PORT: 3306
DB_DIALECT: mysql
ports:
- "3000:80"
depends_on:
- ISAAC-floor-database
ISAAC-sensor-back-end:
image: jjuless/isaac-sensor-back-end
environment:
DB_HOST: ISAAC-sensor-database
DB_USER: root
DB_PASSWORD: isaac
DB_DATABASE: isaac
DB_PORT: 3307
DB_DIALECT: mysql
ports:
- "3001:80"
depends_on:
- ISAAC-sensor-database
Mysql is always listening on port 3306 in each container, so if you want to have multiple mysql instances in the same host, you will need to map different host ports to the same guest port
Hence you will need to change your docker-compose file as follows
ISAAC-floor-database:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: isaac
ports:
- "3306:3306" # <-- HOST port same as GUEST port
volumes:
- ./sql-scripts:/docker-entrypoint-initdb.d
ISAAC-sensor-database:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: isaac
ports:
- "3307:3306" # <-- Notice here the GUEST port is the same as the first container!
volumes:
- ./sql-scripts:/docker-entrypoint-initdb.d

Connection between docker containers as localhost

i am trying to dockerize my web application. i am running a apache webserver + mariadb and redis server as you can see in my docker-compose file combined with an nginx proxy to use local domains and ssl.
everything works fine as long is i use the container names to connect to mysql / redis. I dont want to change all localhosts in my code to the mysql / redis container names.
Is there a way to keep "localhost" as Host instead of the containers name?
version: "3.5"
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: portal-proxy
networks:
- portal
ports:
- "80:80"
- "443:443"
volumes:
- ./certs:/etc/nginx/certs
- /var/run/docker.sock:/tmp/docker.sock:ro
portal:
image: portal:latest
container_name: portal-webserver
networks:
- portal
volumes:
- ./portal:/var/www/html/portal
links:
- db
restart: always
environment:
VIRTUAL_HOST: portal.dev
db:
image: mariadb:latest
container_name: portal-db
networks:
- portal
ports:
- "3306:3306"
restart: always
environment:
MYSQL_DATABASE: portal
MYSQL_USER: www-data
MYSQL_PASSWORD: www-data
MYSQL_ROOT_PASSWORD: asdf1234
volumes:
- ./db:/docker-entrypoint-initdb.d
- ./db:/var/lib/mysql
redis:
image: redis:latest
container_name: portal-redis
environment:
- ALLOW_EMPTY_PASSWORD=yes
networks:
- portal
ports:
- "6379:6379"
networks:
portal:
name: portal
Use a common hostname (staging.docker.host) on all containers, that resolves to the docker host's ip 1.2.3.4.
So adding this to containers:
extra_hosts:
- "staging.docker.host:1.2.3.4"
and use that name (staging.docker.host) in all you connection endpoints.
On you local machine you also add (staging.docker.host) to your /etc/hosts or C:\Windows\System32\drivers\etc\hosts with localhost 127.0.0.1 staging.docker.host.

Cannot log in to the MySQL server through browser

Each time when I try to access the phpmyadmin from browser I receive this error:
"Cannot log in to the MySQL server"
I tried to change networks, restart docker.
version: '3'
services:
web:
image: nginx:alpine
ports:
- 80:80
volumes:
- ./public_html:/public_html
- ./conf.d:/etc/nginx/conf.d
networks:
- nginxphp
php:
image: php:7.1.11-fpm-alpine
volumes:
- ./public_html:/public_html
expose:
- 9000
networks:
- nginxphp
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: root
MYSQL_PASSWORD: root
depends_on:
- db
ports:
- "8080:80"
networks:
nginxphp:
Cannot log in to the MySQL server
mysqli_real_connect(): The server requested authentication method unknown to the client [caching_sha2_password]
mysqli_real_connect(): (HY000/2054): The server requested authentication method unknown to the client
Disclaimer: I'm not a Docker user!
Your didn't mention if you're using the browser on the same computer as the sever or remotely. You'll need access to the mysql server (mysqld) through a terminal (command prompt). If this is a new install, it must be on the computer that is running mysql server.
In the Docker mysql page:
"The default configuration for MySQL can be found in /etc/mysql/my.cnf, which may include additional directories such as /etc/mysql/conf.d or /etc/mysql/mysql.conf.d."
Try looking in the /etc/mysql/my.cnf file on the server you're trying access. first. You're looking for:
bind-address = x.x.x.x
This is the address that the mysql server will talk to ("bound to"). Its typically "localhost" or "127.0.0.1".
To eliminate the error message like you see, I had to do two things:
1) change 'bind-address to 0.0.0.0'
this allows the server to connect to any computer. However, THIS IS A SECURITY RISK. Once you get it working, go read about bind addresses on the mysql website and set it appropriately.
2) Create an new account user#ipaddr where user is the new username and IPAddress is the ip4 address of the computer your trying to connect from. i.e.:
CREATE USER 'user'#'192.168.1.68' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'user'#'192.168.1.68';
Now try accessing mysql through the terminal program using the new username on the computer with the ip you entered above by typing:
mysql -uuser -ppassword -hx.x.x.x
Hopefully, this will help point you in the right direction. There's a ton of information about bind addresses and security on the web.
Because the phpmyadmin container has connected to the default host localhost. But your mysql server is located in other container (it means you cannot connect to mysql server by using localhost). So in the phpmyadmin service, you have to set PMA_HOST=db. See full env variables: https://hub.docker.com/r/phpmyadmin/phpmyadmin/
Full docker-compose.yml:
version: '3'
services:
web:
image: nginx:alpine
ports:
- 80:80
volumes:
- ./public_html:/public_html
- ./conf.d:/etc/nginx/conf.d
networks:
- nginxphp
php:
image: php:7.1.11-fpm-alpine
volumes:
- ./public_html:/public_html
expose:
- 9000
networks:
- nginxphp
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: root
MYSQL_PASSWORD: root
depends_on:
- db
ports:
- "8080:80"
networks:
nginxphp:
If you are using phpmyadmin with latest mysql version you will have some login issues:
Cannot log in to the MySQL server mysqli_real_connect():
The server requested authentication method unknown to the client [caching_sha2_password] mysqli_real_connect(): (HY000/2054):
The server requested authentication method unknown to the client
The solution is to downgrade to mysql 5.7 or another.
I tested with mysql 5.7 and it works for me.
If you want to can test with another versions and let the community know.
Below is the docker-compose file that builds and run Nginx + php 7.1 + mysql 5.7 +phpmyadmin
version: '3'
services:
web:
image: nginx:alpine
ports:
- 80:80
volumes:
- ./public_html:/public_html
- ./conf.d:/etc/nginx/conf.d
networks:
- nginxphp
php:
image: php:7.1.11-fpm-alpine
volumes:
- ./public_html:/public_html
expose:
- 9000
networks:
- nginxphp
db:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db
environment:
PMA_HOST: db
PMA_PORT: 3306
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
depends_on:
- db
ports:
- "8080:80"
networks:
nginxphp:
Hope this will save some time to someone!

Docker Compose | Virtual Hosts

Whats wrong in my code? thanks in advance!
I'm trying to set up a virtual host for my docker container.
On localhost: 8000 works perfectly, but when I try to access through http: //borgesmelo.local/ the error ERR_NAME_NOT_RESOLVED appears, what can be missing?
This is my -> docker-compose.yml
version: '3.3'
services:
borgesmelo_db:
image: mariadb:latest
container_name: borgesmelo_db
restart: always
volumes:
- ./mariadb/:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: My#159#Sql
MYSQL_PASSWORD: My#159#Sql
borgesmelo_ws:
image: richarvey/nginx-php-fpm:latest
container_name: borgesmelo_ws
restart: always
volumes:
- ./public/:/var/www/html
ports:
- "8000:80"
borgesmelo_wp:
image: wordpress:latest
container_name: borgesmelo_wp
volumes:
- ./public/:/var/www/html
restart: always
environment:
VIRTUAL_HOST: borgesmelo.local
WORDPRESS_DB_HOST: borgesmelo_db:3306
WORDPRESS_DB_PASSWORD: My#159#Sql
depends_on:
- borgesmelo_db
- borgesmelo_ws
borgesmelo_phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: borgesmelo_phpmyadmin
links:
- borgesmelo_db
ports:
- "8001:80"
environment:
- PMA_ARBITRARY=1
borgesmelo_vh:
image: jwilder/nginx-proxy
container_name: nginx-proxy
ports:
- "8002:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
networks:
default:
external:
name: nginx-proxy
This is my hosts file (/etc/hosts) [macOS]
#DOCKER
127.0.0.1:8000 borgesmelo.local
Hosts file doesn't support ports as it is for name lookup only. So you would have to set your hosts file to:
127.0.0.1 borgesmelo.local
Then access your application with http://borgesmelo.local:8000.
If you are listening on port 8000 because you already have something else on port 80, then consider using nginx as a reverse proxy and then you can route to different applications based on the server_name. That way, you can access multiple applications through port 80. If you're dealing with docker containers, then consider looking into Traefik as a reverse proxy.

Resources