Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port is already allocated error when setting proxy to false and publishing port (on options) #1133

Closed
dihmeetree opened this issue Oct 18, 2024 · 6 comments

Comments

@dihmeetree
Copy link

dihmeetree commented Oct 18, 2024

My servers portion of my Kamal config looks something like this

servers:
  web:
    hosts:
      - 10.0.0.1 # web-1
      - 10.0.0.2 # web-2
    options:
      publish:
        - 3000:3000
    proxy: false

Note here that I have proxy:false and i'm publishing port 3000. I'm using a different proxy than Kamal Proxy (hence why i've disabled the default proxy).

When I go to re-deploy with Kamal (kamal deploy) I get the following error
Note: Deploying for the first time works fine! But it's only when you deploy a 2nd time you get the error.

 ERROR (SSHKit::Command::Failed): Exceptions on 2 hosts:
Exception while executing on host 10.0.0.1: docker exit status: 125
docker stdout: 96804c19dde1aba43169e2f00946039b06fb40dc4ccc914965002b37c5c4e585
docker stderr: docker: Error response from daemon: driver failed programming external connectivity on endpoint web-web-b139fd2932280db0203c64f60adb2ca8dff79a3b (68099850f994e27b54f959fc057c386c0a9243ea27b694a39fcf314bba3b5ed8): Bind for 0.0.0.0:3000 failed: port is already allocated.

Exception while executing on host 10.0.0.2: docker exit status: 125
docker stdout: c1e9926cfb5188108c1a825ab468e79b73afc9ba7d1911cfb8cf3cc0f9f53fa2
docker stderr: docker: Error response from daemon: driver failed programming external connectivity on endpoint web-web-b139fd2932280db0203c64f60adb2ca8dff79a3b (8f762e5ba8dca7bfca404c4a845bd0ac03268b4c877360e0d54718454742e115): Bind for 0.0.0.0:3000 failed: port is already allocated.

Note the error says Bind for 0.0.0.0:3000 failed: port is already allocated.

In order to get around this I have to remove the existing web containers, then deploy.

I've seen someone else mention this issue in the Discord, but bringing this up since I don't plan on using the built in Kamal Proxy, so it would be awesome if there was a solution/fix for this!

Edit: Decided that I will leverage the Kamal Proxy, so I can still get the zero downtime deploys.. so i've gone back to setting the proxy and everything works again. But this issue is still relevant to those who do decide to disable the built in Kamal Proxy 👍🏻

@djmb
Copy link
Collaborator

djmb commented Oct 22, 2024

Hi @dihmeetree!

Kamal 2 is not designed to work without kamal-proxy. You can disable the proxy, but that's for "non-web" containers where we are just going to check that the container boots correctly

Either way though it will start up new container before closing the old one, so you can't publish a port like that as the second container will fail to start because the first is already using the port.

@djmb djmb closed this as completed Oct 22, 2024
@claasz
Copy link

claasz commented Dec 17, 2024

@djmb It would be very nice if it would be somewhere highlighted in the docs that you cannot use Kamal 2 without kamal-proxy since DHH's video claims the opposite (second half of the video).

Also, on https://kamal-deploy.org/docs/configuration/proxy/ it says

The proxy is enabled by default on the primary role but can be disabled by setting proxy: false.

Highly confusing.

@schrockwell
Copy link

you can't publish a port like that as the second container will fail to start because the first is already using the port.

@djmb What is the recommended configuration for exposing a single port from a container? Is it simply impossible?

@pulkit110
Copy link

Any updates on this? Even when using a proxy, it is sometimes necessary to expose some ports. For example, when using clustering on Elixir/Erlang, EPMD expects port 4296 to be open.

@schrockwell
Copy link

@pulkit110 Here is how I accomplished it for Elixir with an EPMD-less deployment on a single port 6789.

accessories:
  traefik:
    service: traefik
    image: traefik:v3.1
    roles:
      - web
    options:
      publish:
        - "6789:6789"
    cmd: "--providers.docker --providers.docker.exposedByDefault=false --entryPoints.epmd.address=:6789 --log.level=INFO"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"

labels:
  traefik.enable: true
  traefik.tcp.routers.epmd.rule: "ClientIP(`0.0.0.0/0`)"
  traefik.tcp.routers.epmd.priority: 5
  traefik.tcp.routers.epmd.entryPoints: epmd
  traefik.tcp.routers.epmd.service: epmd
  traefik.tcp.services.epmd.loadBalancer.server.port: 6789

And in rel/vm.args.eex you need

-start_epmd false -erl_epmd_port 6789

@pulkit110
Copy link

Thanks @schrockwell, that really helped!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants