My frontend can connect to my backend inside the container in EC2, but not when testing via the IP address

0

Good morning,

I am trying to upload my local website into EC2 using docker containers. I am building two images (1 for front-end and 1 for back-end) in docker and then pull these images in EC2. I have also created a database in RDS. I have configured everything:

  • The backend connection to the database
  • The connection frontend and backend
  • The domain name linked to the IP address of my EC2 instance

When going to the IP address of my website, I can see my frontend. I have a login tab. And when I try to login I have a : Failed to load resource: net::ERR_NAME_NOT_RESOLVED : backend:8080/api/auth/signin:1

To resolve this issue, I have tried several times to :

  • Build the images
  • Inspect the frontend environment variables
  • To verify the network configuration: my containers are attached to the same network
  • Verify Environment Variable in Container: so going into my frontend container and checking. Everything is normal.

And I also tested the connectivity to the backend when I am inside my frontend container using the following commands:

docker exec -it frontend curl -X POST http://backend:8080/api/auth/signin -H "Content-Type: application/json" -d '{"username": "...", "password": "..."}'

And this querry worked perfectly. So my frontend can communicate with my backend and database using command lines in the terminal.

But when I use the login page, I can't connect.

I have tried to solve this issue for 1 week now, but I am out of options.

It would be nice to have some help or some clues to be able to login into my website.

Thank you in advance, Alexia

asked 10 months ago782 views
3 Answers
1

Hello.

The front end code itself runs on the browser, so I think you will get an error even if you access it using the Docker container name.
In other words, if you change the part specifying “backend:8080” in the front end to something such as an EC2 Public IP address, you should be able to access the site.
https://stackoverflow.com/questions/77635244/failed-to-load-resource-neterr-name-not-resolved-error

profile picture
EXPERT
answered 10 months ago
profile picture
EXPERT
reviewed 10 months ago
  • Thank you !

    If I put the EC2 public address, if my container is restarting, the IP will change and I will have to manually change the IP. Is there another way to avoid that ?

  • For example, if you place EC2 under ALB and use ALB's DNS name, you will be able to access it because the DNS name will not change even if the IP address changes. You can also make it a fixed IP by setting ElastcIP on EC2.

  • So if my DNS name is alexia-coaching.com you suggest that I use http://alexia-coaching.com instead of backend:8080. Is that right? Just to be sure I understand

  • Yes, it's correct. I think you can access the backend by doing the following.

    http://alexia-coaching.com:8080
    
1

Hello,

Your frontend can't find the backend by name. Try these steps:

  • Replace "backend" in login URL with the actual IP address of your backend container (if it works with IP, it's a DNS issue).

  • Ensure your containers share a network where "backend" resolves to the correct IP.

  • Check if your frontend code expects an IP or hostname, and update the login request accordingly.

Update Security Group: Ensure the security group associated with your EC2 instance allows inbound traffic on the port your backend uses (e.g., port 80 for HTTP or port 443 for HTTPS).

# Allow HTTP traffic
Type:   HTTP
Port:   80
Source: 0.0.0.0/0

# Allow HTTPS traffic
Type:   HTTPS
Port:   443
Source: 0.0.0.0/0
profile picture
EXPERT
answered 10 months ago
  • Thank you for your help.

    • Ensure your containers share a network where "backend" resolves to the correct IP : it is the case
    • Check if your frontend code expects an IP or hostname, and update the login request accordingly: the code expects an hostname

    And I double checked everything, I still have the same issue

0

Have u solved the problem? I'm having the same issue.

answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions