Hou ZeYu
Hou ZeYu

Reputation: 421

Manage other container with nginx container

How do I use the nginx in the container and access other container with setup config file? I am a beginner for docker. I try to learn how to use nginx manage my applications by docker containers. I will use the "pgadmin" as an application in container for example.

  1. Create & start containers. I try to use the [link] parameter to connect two containers.

    sudo docker create -p 80:80 -p 443:443 --name Nginx nginx
    sudo docker create -e [email protected] -e PGADMIN_DEFAULT_PASSWORD=20121006 -p 5001:80 --link Nginx:PSQLA --name PSQLA dpage/pgadmin4
    sudo docker start Nginx 
    sudo docker start PSQLA
    
  2. Go to Nginx bash and install nano edit.

    sudo docker exec -it Nginx bash
    apt update
    apt install nano
    
  3. Create and setup the nginx config file in admin.conf.

    nano etc/nginx/conf.d/admin.conf
    

In the admin.conf is following blow.

{
  listen 80;
  server_name admin.my-domain-name;
  location / {
    proxy_pass http://PSQLA:80;
  }
}

I get this error blow.

2020/10/17 01:57:16 [emerg] 333#333: host not found in upstream "PSQLA" in /etc/nginx/conf.d/admin.conf:5
nginx: [emerg] host not found in upstream "PSQLA" in /etc/nginx/conf.d/admin.conf:5

How do I use the nginx in the container and access other container with setup config file?

Upvotes: 0

Views: 202

Answers (1)

Kapil Khandelwal
Kapil Khandelwal

Reputation: 1176

Try the following commands (in the same order) to launch the containers:

sudo docker create -e [email protected] -e PGADMIN_DEFAULT_PASSWORD=20121006 -p 5001:80 --name PSQLA dpage/pgadmin4
sudo docker create -p 80:80 -p 443:443 --link PSQLA:PSQLA --name Nginx nginx
sudo docker start PSQLA
sudo docker start Nginx 

Now edit the Nginx configurations and you should not encounter the error anymore.

Tl;dr

As mentioned in the docker documentation:

When you set up a link, you create a conduit between a source container and a recipient container. The recipient can then access select data about the source.

In order to access PSQLA from Nginx container, we need to link Nginx container to PSQLA container and not the other way around.

Now the question is: What difference does that even makes?

For this we need to understand how --link option works in docker. The docker adds a host entry for the source container to the /etc/hosts file

We can verify this in the /etc/hosts file inside the Nginx container. It contains a new entry something like this (The id and IP might be different in your case):

172.17.0.4 PSQLA 1117cf1e8a28

This entry makes Nginx container access PSQLA container using the container name.

Please refer this for better understanding:

https://docs.docker.com/network/links/#updating-the-etchosts-file

Important Note

As mentioned in the Docker documentation:

The --link flag is a legacy feature of Docker. It may eventually be removed. Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link.

Upvotes: 1

Related Questions