Error: listen tcp 0.0.0.0:80: bind: address already in use


(Michael) #1

I’ve tried long and hard to figure this out on my own before posting on here so I apologize in advance if this is easy. I’m still new to Rails but know my way around it.

I already have an existing Rails application running. I’m adding Discourse as a second Rails application and followed the guide for Docker. I’m up to the point where I need to run ./launcher start app. However in doing so I get the following error.

Error: Cannot start container d9541419a93f: listen tcp 0.0.0.0:80: bind: address already in use
2014/03/20 22:18:08 Error: failed to start one or more containers

I’m assuming that I need to configure my nginx for this to work properly, correct? So I copied over /etc/nginx/sites-available/arsiamons.io to /etc/nginx/sites-available/forums.arsiamons.io. Here’s what I got in there

upstream unicorn {
  server unix:/tmp/unicorn.discourse.sock fail_timeout=0;
}

server {
  listen 5000 deferred;
  server_name forums.arsiamons.io;
  root /var/www/discourse/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

If helps here’s what I have inside the arsiamons.io file

upstream unicorn {
  server unix:/tmp/unicorn.arsiamons.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  server_name arsiamons.io www.arsiamons.io;
  root /home/skylab/apps/arsiamons/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

As a basic guide for myself, I usually follow these steps to get my Rails apps going.

What am I missing exactly? Your help is greatly appreciated. Thanks!


Confused of expose port
(Nathan Rijksen) #2

You could simply run Discourse on another port and use nginx as a proxy, a good example is the config by @davidcelis that he posted here: Support X-Forward-For / X-Real-IP


(Michael Brown) #3

You’re running a webserver on your host that’s listening on port 80, but also have your container configuration specifying to forward port 80 to a port on the container’s IP. You can’t have both.


(Michael) #4

Hm. That’s exactly what I don’t understand. Where do I change the port number for the container? Inside my app.yml file, I have this

expose:
  - "5000:5000"
  - "2222:22"

There are no other references to the port number.


(Sam Saffron) #5

= Map host port #85 to container port #80


(Michael) #6

It appears that Discourse is attempting run on the root domain rather than the subdomain. After stopping my other Rails app from running then ran ./launcher start app, it took over the root domain.

  DISCOURSE_HOSTNAME: 'forums.arsiamons.io'

That’s in my yml file though. And I did change it to 85:80.


(Kane York) #7

Change your NGINX config so that the VHost on port 85 is the subdomain, then…?

The DISCOURSE_HOSTNAME controls what the links it generates are based on.


(Michael) #8

I feel like I’m close to understanding what I need to do but I’m missing a piece here or incorrectly understanding the various searches and documents that I’ve went through.

nginx: [emerg] host not found in upstream "forums.arsiamons.io" in /etc/nginx/sites-enabled/arsiamons.io:36

Part of app.yml

# which ports to expose?
expose:
  - "8080:80"
  - "2222:22"
DISCOURSE_HOSTNAME: 'forums.arsiamons.io'

My nginx setup. The first server block is to another rails app to the root of the domain. The second is supposed to go to Discourse. Instead what appears to be happening is that Discourse is taking over the root.

upstream unicorn {
  server unix:/tmp/unicorn.arsiamons.sock fail_timeout=0;
}

server {
  listen 80;
  listen [::]:80 default ipv6only=on;
  server_name arsiamons.io;
  root /home/skylab/apps/arsiamons/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}
server {
  listen 80;
  server_name forums.arsiamons.io;

  location / {
    access_log off;
    proxy_pass http://forums.arsiamons.io:8080;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

If I run sudo nginx -t

nginx: [emerg] host not found in upstream "forums.arsiamons.io" in /etc/nginx/sites-enabled/arsiamons.io:36
nginx: configuration file /etc/nginx/nginx.conf test failed

(Michael) #9

No matter how many times I edit and change things around, there’s always an issue with the port number. :frowning:

cid found, ensuring container is started
Error: Cannot start container d9541419a93f: listen tcp 0.0.0.0:80: bind: address already in use
2014/03/21 19:37:27 Error: failed to start one or more containers

app.yml

# which ports to expose?
expose:
  - "85:80"
  - "2222:22"

NGINX config for sites-available

upstream unicorn {
  server unix:/tmp/unicorn.arsiamons.sock fail_timeout=0;
}

server {
  listen 80;
  listen [::]:80 default ipv6only=on;
  server_name arsiamons.io;
  root /home/skylab/apps/arsiamons/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

upstream discourse { server 127.0.0.1:85; }
server {
  listen 80;
  server_name forums.arsiamons.io;

  location / {
    access_log off;
    proxy_pass http://discourse;
  }
}

more nginx config

user skylab;
worker_processes 4;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

(Michael) #10

@riking I thought that’s what I already did? Am I missing something?


(Nathan Rijksen) #11

Did you rebuild your container after changing those prefs?

./launcher destroy app
./launcher bootstrap app
./launcher start app

(Michael) #12

It had not occurred to me that I had to destroy the app then build it. I had been running bootstrap after stopping it instead of destroying it. You sir are a genius. Figures it would be this simple. Luckily in this whole process I learned quite a bit about nginx, Docker, etc.


(Sam Saffron) #13

I am thinking I should add a “hint” to ./launcher stop, something like:

Would you also like to remove this container? (y/N) 

Only if called with tty.


(Nathan Rijksen) #14

If you do that it would also make sense to say “why” you would want to remove the container. Eg. “this is necessary when you make changes to your app.yml, or to force an update of Discourse”.

I’m saying that because it wasn’t obvious to me at first. Like it also wasn’t obvious that destroying a container does not loose you any data.


(Jeff Atwood) #15

@sam you should do this and fold in @naatan’s feedback. It makes sense.


(Lenin Raj Rajasekaran) #16

To do Proxy with Apache (incase your ‘other’ Rails app is on Apache/Passenger), do the following:

<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName forum.example.com
ProxyPass / http://localhost:85/
ProxyPassReverse / http://localhost:85/
</VirtualHost>

More details here.


(Austin) #17

I cannot for the life of me figure this out. I can access my main site at gamepredict.us but cannot get my discourse at discuss.gamepredict.us. It only works at gamepredict.us:10080. Here is my nginx.conf:

upstream puma {
  server unix:///home/deploy/apps/gamepredict/shared/tmp/sockets/gamepredict-puma.sock;
}

server {
  listen 80 default_server deferred;
  server_name gamepredict.us;

  root /home/deploy/apps/gamepredict/current/public;
  access_log /home/deploy/apps/gamepredict/current/log/nginx.access.log;
  error_log /home/deploy/apps/gamepredict/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}

upstream discourse {
  server 127.0.0.1:10080;
}

# configure the virtual host
server {
  # replace with your domain name
  server_name discuss.gamepredict.us;

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    # pass to the upstream discourse server mentioned above
    proxy_pass http://discourse;
  }
}

And my app.yml:

## which TCP/IP ports should this container expose?
expose:
  - "10080:80"   # fwd host port 80   to container port 80 (http)
  - "2222:22" # fwd host port 2222 to container port 22 (ssh)
## TODO: The domain name this Discourse instance will respond to
DISCOURSE_HOSTNAME: 'discuss.gamepredict.us'

Any help would be very much appreciated!


(Kane York) #18

You should move to the socketed Docker setup:


(Austin) #19

Thanks @riking. On this step:

expose:
  - "2222:22" # If you don't need to use ./launcher ssh app, you can remove this too

To clarify, does that mean I leave my - "10080:80" line and could remove the 2222 line if I don’t use the ssh app?


(Kane York) #20

No, you remove both of them.