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
asked 2 years ago1611 views
2 Answers
2
Accepted Answer

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"
   }
answered 2 years ago
AWS
EXPERT
reviewed 2 years ago
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
answered 2 years 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