How to connect from localhost to docker container running in docker network
<p>I have a mysql docker container written using docker like this:</p>
<pre class="brush:php;toolbar:false;">version: "3.3"
services:
db-server:
container_name: database
image: mysql:latest
build:
dockerfile: ./Dockerfile
environment:
MYSQL_ROOT_PASSWORD: rootTest
MYSQL_USER: soumalya
MYSQL_PASSWORD: userTest
ports:
- "3031:3306"
networks:
- internal
volumes:
- mysql:/var/lib/mysql
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
restart: unless-stopped
networks:
internal:
driver: bridge
volumes:
mysql:</pre>
<p>I'm connecting to localhost:3031 with the correct password, but it's giving me this error:</p>
<pre class="lang-bash prettyprint-override"><code>Access denied for user 'root'@'172.19.0.1' (using password: YES)
</code></pre>
<p>How do I fix this error? Other containers in the same internal network are also unable to connect to the database. </p><p>I give the contents of the Dockerfile here:</p><p><code></code></p>
<pre class="brush:php;toolbar:false;">ADD init.sql init.sql</pre>
<p>I gave the contents of init. sql here:</p>
<pre class="brush:php;toolbar:false;">create database if not exists reviews;
create database if not exists users;
create database if not exists vendors;
GRANT ALL PRIVILEGES on reviews.* to 'soumalya'@'%';
GRANT ALL PRIVILEGES on users.* to 'soumalya'@'%';
GRANT ALL PRIVILEGES on vendors.* to 'soumalya'@'%';</pre>
<p>From the container logs, I can see that the database is starting and waiting for incoming connections in port 3306. </p>