search

Home  >  Q&A  >  body text

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>
P粉418854048P粉418854048488 days ago519

reply all(1)I'll reply

  • P粉803527801

    P粉8035278012023-07-26 10:43:28

    As @HansKillan said, I changed the password when the database already existed, the changed password was ignored, it was using the old password. I had to manually change my user's password by writing sql command using docker exec <container_name>... using SQL command to change user password. Then everything worked out. In your case, if your database doesn't contain much data, then you can just delete the volume and recreate the container to shorten the time

    reply
    0
  • Cancelreply