Laravel + Docker - Error during WebSocket handshake: Unexpected response code: 502
Hello
Guys, tell me, please, what could be the problem
I work with docker, containers are raised: nginx, php-cli, php-fpm, vue
The whole backend on Laravel, and there in console format I run Ratchet commands:

```
    public function handle()
    {
        $server = IoServer::factory(
            new HttpServer(
                new WsServer(
                    new WebSocketChat()
                )
            ),
            8123
        );

        $server->run();
    }
```
Everything starts correctly. `WebSocketChat`
 - default, from the hello world example.

A request is sent from the front like this:
```
const conn = new WebSocket('ws://127.0.0.1:8080/websocket')
conn.onopen = function (e) {
  console.log('Connection established!')
}

conn.onmessage = function (e) {
  console.log(e.data)
}
```

The nginx config in the container shows that nginx is defined correctly:
```
user  nginx;
worker_processes 5;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {

    map $http_upgrade $connection_upgrade {
        default upgrade;
        ""      close;
    }

    upstream websocket {
        server 127.0.0.1:8123;
    }

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;

    keepalive_timeout 86400;
    proxy_connect_timeout 86400;
    proxy_send_timeout 86400;
    proxy_read_timeout 86400;
    fastcgi_read_timeout 600s;

    server {
        server_name core.xxx.local;
        root /project/public;
    
        location / {
            try_files $uri /index.php$is_args$args;
        }
    
        location ~ \.php(/|$) {
            fastcgi_pass xxx-core-php-fpm:9000;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
        }
    
        location /websocket {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    
        client_max_body_size 512m;
    
        error_log /var/log/nginx/core.local_error.log;
        access_log /var/log/nginx/core.local_access.log;
    }

}
```

But the front, when trying to connect, throws:
```failed: Error during WebSocket handshake: Unexpected response code: 502```
And in the nginx logs at this moment it says:
```
2019/11/01 09:24:03 [error] 7#7: *5 connect() failed (111: Connection refused) while connecting to upstream, 
client: 172.19.0.1, server: my-server-name, request: "GET /websocket HTTP/1.1", 
upstream: "http://127.0.0.1:8123/websocket", host: "127.0.0.1:8080"
```
In Dockerfile php-cli I forward port 8123:
```
FROM xxx-core/php-fpm

RUN apt-get update \
    && apt-get install -y ssh \
    && useradd -ms /bin/bash xxx \
    && echo "xxx:xxx" | chpasswd \
    && mkdir -p /var/run/sshd \
    && chown -R xxx:xxx /opt \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf /tmp/* \
    && echo Done!

EXPOSE 9000 22 8123

CMD ["/usr/sbin/sshd", "-D"]
```

In docker-compose in php-cli, I project the port from the container to the local one:
```
  xxx-core-php-cli:
    build: build/php-cli
    image: xxx-core/php-cli
    container_name: xxx-core-php-cli
    volumes:
      - "..:/project"
      - "./build/php-cli/php.ini:/etc/php/7.3/cli/conf.d/00-php.ini:ro"
      - "./volumes/data:/opt/data"
    ports:
      - "127.0.0.1:9010:9000"
      - "127.0.0.1:2222:22"
      - "127.0.0.1:8123:8123"
    environment:
      XDEBUG_CONFIG: >
        idekey=PHPSTORM
        remote_enable=1
        remote_autostart=1
        remote_host=172.22.0.1
        remote_port=9000
        profiler_enable=0
        coverage_enable=0
      PHP_IDE_CONFIG: serverName=core.xxx.local
    networks:
      - xxx
    depends_on:
      - xxx-core-postgresql
      - xxx-core-php-fpm
```

I understand that the problem is here:
```
    upstream websocket {
        server 127.0.0.1:8123;
    }
```

Instead of 127.0.0.1:8123 I already wrote the name of the php-fpm container, and the name of the php-cli container, with and without ports - the result is one. Similarly forwarded volumes for different containers, it did not work.

Tell me, do I understand the general principle of work?

-  the front throws a request to ws, to a regular 8080 port
- nginx catches it, since it listens (projects) this port
- then nginx finds location and sees that there are proxy settings
- nginx goes to the upstream specified in the settings
and upstream should forward the request to php-cli, where the command that listens for sockets is running in the container

Sorry for the errors, translated through google translate :)
