Failed to connect to mysql at 127.0.0.1:6666 with user root

Hello everyone, please I got this error while trying to connect my docker container that is running in an AWS EC2 instance to MYSql - “Failed to connect to MYSQL at 127.0.0.1:6666 with user root”. I did a docker ps to show that my docker container is up and running, the result showed that my container is up.
I created my docker image using a Dockerfile and this is the content of my Dockerfile -

FROM centos
RUN cd /etc/yum.repos.d/
RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
RUN yum install -y httpd
ADD index.html /var/www/html
CMD apachectl -D FOREGROUND
EXPOSE 80
MAINTAINER babatunde
ENV myenv myval
ENV myenv2 myval2

Also, the security group of my EC2 instance is set to accept request from “Anywhere”. I also did a docker inspect on my container and the output is in the image below -


I used 127.0.0.1 as the Hostname in MYSql Workbench with port 6666 and I also tried Hostname 0.0.0.0 but both failed. I searched on this forum and found out that a similar question has been asked before https://forums.docker.com/t/failed-to-connect-to-my-sql-at-127-0-0-1-3306with-user-172-18-0-1/138077 but no solution was provided for reasons stated in that post, that’s why I’m asking this same question again. I’ve also confirmed that the Firewall on my computer isn’t blocking any port. Please does anyone knows how I can resolve this issue ? Thanks for any help in advance.

Localhost/127.0.0.1 inside a container is alway only localhost inside the container, not the localhost of the node/host.

If a Docker container should connect to a database, use its service name if all services run inside a Docker network, then Docker DNS will provide the correct internal IP.

If the database is running outside Docker, use the external IP of the node it’s running on.

Hello @bluepuma77, I’m so, so, sorry for late response, thanks for your reply. I would try it out and get back to you asap. Thanks once again.