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 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则