jovandeginste / workout-tracker

A workout tracking web application for personal use (or family, friends), geared towards running and other GPX-based activities
Other
945 stars 31 forks source link

Error when trying to bind to port #175

Closed lukasitaly closed 2 months ago

lukasitaly commented 2 months ago

Port 8080 is not free, so I had to use 8070. But I'm having issues. I've tried everything I know and tried researching, but the issue doesn't seem to go away. When following the guide, I get a blank page "Unable to connect".

I tried:

When I use "WT_BIND" and/or "bind" I finally get an error:
panic: listen tcp: address "[::]:8070": too many colons in address

How can I proceed? Thanks in advance

jovandeginste commented 2 months ago

I'm not sure I understand what you try to achieve.

You say port 8080 is already in use, but you also use compose.yaml, which implies a Docker container.

If you don't use Docker, and simply run the binary, you can use this:

WT_BIND="[::]:8070" ./workout-tracker

The output should contain a line similar to this (note the [::]:8070):

2:52PM INF Starting web server on [::]:8070 app=workout-tracker version=local sha=local module=app

If you use a compose file and Docker, you can simply use a different port in the host port part:

version: "3.8"
services:
  workout-tracker:
    image: ghcr.io/jovandeginste/workout-tracker:master
    restart: unless-stopped
    ports:
      # Host Port:Container Port
      - 8070:8080 # This is where we define the port mapping
    volumes:
      - ./data:/data
    environment:
      - WT_JWT_ENCRYPTION_KEY=my-secret-key

If you prefer to use the same port inside and outside of the container, you can use this:

version: "3.8"
services:
  workout-tracker:
    image: ghcr.io/jovandeginste/workout-tracker:master
    restart: unless-stopped
    ports:
      # Host Port:Container Port
      - 8070:8070 # This is where we define the port mapping
    volumes:
      - ./data:/data
    environment:
      - WT_JWT_ENCRYPTION_KEY=my-secret-key
      - WT_BIND="[::]:8070"
lukasitaly commented 2 months ago

I'm sorry if I wasn't very clear, I'm trying my best but am still at the beginning of my Docker journey so I'm not always sure what I'm doing.

Anyways, that's what I did. I tried again, deleted everything and copied your code (the last sample with the same port inside and outside of the container) into the compose.yaml file, just changed the secret key. I still get the same error:

panic: listen tcp: address "[::]:8070": too many colons in address

This repeats time and time again in the logs:

workout-tracker-1  | {"time":"2024-07-10T13:17:39.105649464Z","level":"INFO","msg":"Connecting to the database 'sqlite': ./database.db","app":"workout-tracker","version":"master","sha":"4101e4a72e303f2fea1175317f7e7f70d9d0d6ff","module":"app"}
workout-tracker-1  | {"time":"2024-07-10T13:17:39.125805448Z","level":"INFO","msg":"Starting web server on \"[::]:8070\"","app":"workout-tracker","version":"master","sha":"4101e4a72e303f2fea1175317f7e7f70d9d0d6ff","module":"app"}
workout-tracker-1  | {"time":"2024-07-10T13:17:39.125954379Z","level":"INFO","msg":"Worker started...","app":"workout-tracker","version":"master","sha":"4101e4a72e303f2fea1175317f7e7f70d9d0d6ff","module":"app","module":"worker"}
workout-tracker-1  | panic: listen tcp: address "[::]:8070": too many colons in address
workout-tracker-1  |
workout-tracker-1  | goroutine 1 [running]:
workout-tracker-1  | main.main()
workout-tracker-1  |    /app/main.go:35 +0x1ec

You say port 8080 is already in use, but you also use compose.yaml, which implies a Docker container Since I'm a beginner, I'm not entirely sure what you're trying to say here, could you please explain in further detail?

Thanks again

jovandeginste commented 2 months ago

If you look at the logs, you see this: "Starting web server on \"[::]:8070\"" (quotes, with escaped quotes (\") inside) While in mine, it was: INF Starting web server on [::]:8070 (no quotes)

I suspect the quotes are kept, while they shouldnt. Can you remove the quotes around the value in the compose.yaml file?

      - WT_BIND=[::]:8070

What I mean with

You say port 8080 is already in use, but you also use compose.yaml, which implies a Docker container

Your container has its own port space, which means you can use any port inside the container (unless you use the host network namespace, which you shouldn't for this type of application).

This means you can keep the application on port 8080 inside the container (you don't need to change its configuration), and use the Docker port mapping system, to use a different port outside the container (ie. on the host). This means that eg. port 8070 on the host, will map to port 8080 inside the container.

You achieve this by simply using this snippet in the compose file:

    ports:
      # Host Port:Container Port
      - 8070:8080 # This is where we define the port mapping

You remove anything with bind or WT_BIND, don't change the port of the application inside the container.

When the container starts, you can surf to localhost:8070, and still reach the application which listens on port 8080; like magic (unless you understand how it works, then it's just technology, see the 3rd law)

lukasitaly commented 2 months ago

Oh well that was an easy solution, the problem were indeed the quotes. Thank you so much! :)

Your container has its own port space

So that means for every container I create, I have all ports available? If I create a container with port 8080, then I can create another one with port 8080 since it's isolated right?

jovandeginste commented 2 months ago

Oh well that was an easy solution, the problem were indeed the quotes. Thank you so much! :)

Your container has its own port space

So that means for every container I create, I have all ports available? If I create a container with port 8080, then I can create another one with port 8080 since it's isolated right?

Exactly, that's the default behavior. You can override this (eg. use the "host network", which will map all container ports to the host's network interface).

More information here: https://docs.docker.com/network/

lukasitaly commented 2 months ago

Perfect, thanks for the clarification!