Laradock: [Question] [Solved] Access a node daemon proccess inside the workspace container from the host OS

Created on 28 Dec 2017  路  3Comments  路  Source: laradock/laradock

I'm trying to solve the problem above and I couldn't find any solution for it.

My project is constituted by an API and a client. The client runs inside the workspace container and listens to localhost:3000 (after I run the command yarn run dev).
I have exposed the port and created a server for it on nginx sites folder, proxing the requests to http://workspace:3000, but only I got was the error (502 Bad Gateway):

2017/12/27 23:18:40 [error] 7#7: *14 connect() failed (111: Connection refused) while connecting to upstream, client: 172.22.0.1, server: app.myapp.local, request: "GET / HTTP/1.1", upstream: "http://172.22.0.4:3000/", host: "app.myapp.local".

I have tried access the url using the curl in the host OS and wget in the nginx container but with no success (same error - connection refused).

docker-compose ps:

         Name                      Command                State       Ports                  
-----------------------------------------------------------------------------------------------------------
laradock_applications_1   /true                           Exit 0                                           
laradock_blackfire_1      blackfire-agent                 Up          8707/tcp                                
laradock_mariadb_1        docker-entrypoint.sh mysqld     Up          0.0.0.0:3306->3306/tcp                  
laradock_nginx_1          nginx                           Up          0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
laradock_php-fpm_1        docker-php-entrypoint php-fpm   Up          9000/tcp                                
laradock_phpmyadmin_1     /run.sh phpmyadmin              Up          0.0.0.0:8080->80/tcp                    
laradock_workspace_1      /sbin/my_init                   Up          0.0.0.0:22->22/tcp, 3000/tcp

app.mysite.local:

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

    server_name app.mysite.local;

    location / {
        proxy_redirect off;
        proxy_pass http://workspace:3000;
        proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_max_temp_file_size 0;
        proxy_http_version 1.1;
        proxy_read_timeout 240s;
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }

    error_log /var/log/nginx/app.mysite.local_error.log;
    access_log /var/log/nginx/app.mysite.local_access.log;
}

What I want to do is access the client app through the url app.mysite.local on the browser and proxy the requests to the daemon proccess running in the workspace container at localhost:3000.

Most helpful comment

Not sure, but I think it can be useful try to run node with host parameters. How to do it, depends on the project you have. You can see example for webpack and angular here or try to find another one.

The main thing is to allow node to be accessed from the network. Usually it can be done when you set host to the 0.0.0.0

And maybe you will need to add port forwarding from the host to the workspace.

For examplle the next line here

 - "3000:3000"

All 3 comments

Not sure, but I think it can be useful try to run node with host parameters. How to do it, depends on the project you have. You can see example for webpack and angular here or try to find another one.

The main thing is to allow node to be accessed from the network. Usually it can be done when you set host to the 0.0.0.0

And maybe you will need to add port forwarding from the host to the workspace.

For examplle the next line here

 - "3000:3000"

@ed-fruty Thank you! Changing the host to 0.0.0.0 worked flawlessly. Thank you again! Your are 10/10!

I had the same problem and I did not know what host you mean. In my case I solved the problem changing webpack dev server host from 'localhost' to '0.0.0.0'. Because I am running a webpack dev server inside the workspace container.

  devServer: {
    hot: true,
    inline: true,
    host: "0.0.0.0", // <!------- changed from localhost
    port: 3000,
    contentBase: path.join(__dirname, "public"), 
  },
Was this page helpful?
0 / 5 - 0 ratings

Related issues

percymamedy picture percymamedy  路  3Comments

SamAriafar picture SamAriafar  路  3Comments

StarFleetCPTN picture StarFleetCPTN  路  3Comments

sudden-break picture sudden-break  路  3Comments

vocanic-saumini picture vocanic-saumini  路  3Comments