Communication Between Lightsail Containers

2

I want to deploy two containers which is "nginx" and "php-fpm(Laravel)".

I already create and make it work on local using docker-compose. But by Lightsail it failed. Lightsail logs said php-fpm container not found which I set the name on container.yml.

nginx: [emerg] host not found in upstream "php" in /etc/nginx/conf.d/default.conf:24

The line 24 isfastcgi_pass php:9000;, indicating the name of php container not found. Like docker-compose, I thought it would be possible with specifying container name can communicate between each containers.

So, my question is what is the right way to configure for communicate between two container.

Here is the settings.

nginx default.conf

server {
  listen 80 default_server;
  listen [::]:80 default_server;

  server_name _;

  root /app/public;

  add_header X-Frame-Options "SAMEORIGIN";
  add_header X-XSS-Protection "1; mode=block";
  add_header X-Content-Type-Options "nosniff";

  index index.html index.htm index.php;

  charset utf-8;

  location / {
      try_files $uri $uri/ /index.php?$args;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    include fastcgi_params;
    fastcgi_buffer_size 32k;
    fastcgi_buffers 4 32k;
    fastcgi_read_timeout 1200s;
    fastcgi_send_timeout 1200s;
  }
}

Lightsail container.yml

serviceName: ${SERVICE_NAME}
containers:
    nginx:
        command: []
        image: ${LATEST_NGINX_LIGHTSAIL_DOCKER_IMAGE}
        ports:
            '80': HTTP
    php:
        command: []
        image: ${LATEST_PHP_LIGHTSAIL_DOCKER_IMAGE}
publicEndpoint:
    containerName: nginx
    containerPort: 80
    healthCheck:
        healthyThreshold: 2
        intervalSeconds: 20
        path: /api/healthcheck
        successCodes: 200-499
        timeoutSeconds: 4
        unhealthyThreshold: 2
已提問 2 年前檢視次數 1634 次
2 個答案
2
已接受的答案

Lightsail containers don't contain the DNS magic that docker-compose has. You'll need to tell Lightsail about the port and expose it internally.

Change nginx.conf fastcgi_pass php:9000; -> fastcgi_pass localhost:9000;

Add to container.yml

php:
   "ports": {
     "9000": "HTTP"
   }
已回答 2 年前
AWS
專家
已審閱 2 年前
1

thank you for the answer @mreferre

For the sake of completeness, i would like to refer to the section in the lightsail documentation i just became aware of: https://lightsail.aws.amazon.com/ls/docs/en_us/articles/amazon-lightsail-container-services -> "Private domain"

To access a specific container using the private domain of your container service, you must specify the open port of the container that will accept your connection request. You do this by formatting the domain of your request as <ServiceName>.service.local:<PortNumber>, in which <ServiceName> is the name of your container service and <PortNumber> is the open port of the container that you wish to connect to. For example, if you create a deployment on your container service named container-service-1, and you specify a Redis container with port 6379 open, then you should format the domain of your request as container-service-1.service.local:6379.

Sandro
已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南