pocketbase / pocketbase

Open Source realtime backend in 1 file
https://pocketbase.io
MIT License
39.56k stars 1.84k forks source link

Using as a framework is immediately giving `address already in use` error #581

Closed TylerSustare closed 2 years ago

TylerSustare commented 2 years ago

tl;dr;

After following the setup instructions on the README, I'm not able to run pocketbase as a framework. It's the minimal amount of code to run the framework, but I keep getting an address already in use error when running go run main.go serve. It seems that the server starts, but then gets this error, which seems strange.

Env

I've tried

Terminal output with debug

➜ go run -buildmode=pie main.go serve --debug [0.00ms] SELECT {{_params}}.* FROM `_params` WHERE `key`='settings' LIMIT 1 [0.00ms] CREATE TABLE IF NOT EXISTS `_migrations` (file VARCHAR(255) PRIMARY KEY NOT NULL, applied INTEGER NOT NULL) [0.00ms] SELECT count(*) FROM `_migrations` WHERE `file`='1640988000_init.go' LIMIT 1 [0.00ms] CREATE TABLE {{_admins}} ( [[id]] TEXT PRIMARY KEY, [[avatar]] INTEGER DEFAULT 0 NOT NULL, [[email]] TEXT UNIQUE NOT NULL, [[tokenKey]] TEXT UNIQUE NOT NULL, [[passwordHash]] TEXT NOT NULL, [[lastResetSentAt]] TEXT DEFAULT "" NOT NULL, [[created]] TEXT DEFAULT "" NOT NULL, [[updated]] TEXT DEFAULT "" NOT NULL ); CREATE TABLE {{_users}} ( [[id]] TEXT PRIMARY KEY, [[verified]] BOOLEAN DEFAULT FALSE NOT NULL, [[email]] TEXT UNIQUE NOT NULL, [[tokenKey]] TEXT UNIQUE NOT NULL, [[passwordHash]] TEXT NOT NULL, [[lastResetSentAt]] TEXT DEFAULT "" NOT NULL, [[lastVerificationSentAt]] TEXT DEFAULT "" NOT NULL, [[created]] TEXT DEFAULT "" NOT NULL, [[updated]] TEXT DEFAULT "" NOT NULL ); CREATE TABLE {{_collections}} ( [[id]] TEXT PRIMARY KEY, [[system]] BOOLEAN DEFAULT FALSE NOT NULL, [[name]] TEXT UNIQUE NOT NULL, [[schema]] JSON DEFAULT "[]" NOT NULL, [[listRule]] TEXT DEFAULT NULL, [[viewRule]] TEXT DEFAULT NULL, [[createRule]] TEXT DEFAULT NULL, [[updateRule]] TEXT DEFAULT NULL, [[deleteRule]] TEXT DEFAULT NULL, [[created]] TEXT DEFAULT "" NOT NULL, [[updated]] TEXT DEFAULT "" NOT NULL ); CREATE TABLE {{_params}} ( [[id]] TEXT PRIMARY KEY, [[key]] TEXT UNIQUE NOT NULL, [[value]] JSON DEFAULT NULL, [[created]] TEXT DEFAULT "" NOT NULL, [[updated]] TEXT DEFAULT "" NOT NULL ); [0.00ms] INSERT INTO `_collections` (`createRule`, `created`, `deleteRule`, `id`, `listRule`, `name`, `schema`, `system`, `updateRule`, `updated`, `viewRule`) VALUES ('userId = @request.user.id', '2022-09-20 20:21:46.177', , 'systemprofiles0', 'userId = @request.user.id', 'profiles', '[{"system":true,"id":"pbfielduser","name":"userId","type":"user","required":true,"unique":true,"options":{"maxSelect":1,"cascadeDelete":true}},{"system":false,"id":"pbfieldname","name":"name","type":"text","required":false,"unique":false,"options":{"min":null,"max":null,"pattern":""}},{"system":false,"id":"pbfieldavatar","name":"avatar","type":"file","required":false,"unique":false,"options":{"maxSelect":1,"maxSize":5242880,"mimeTypes":["image/jpg","image/jpeg","image/png","image/svg+xml","image/gif"],"thumbs":null}}]', true, 'userId = @request.user.id', '2022-09-20 20:21:46.177', 'userId = @request.user.id') [0.00ms] CREATE TABLE `profiles` (`avatar` TEXT DEFAULT '', `created` TEXT DEFAULT "" NOT NULL, `id` TEXT PRIMARY KEY, `name` TEXT DEFAULT '', `updated` TEXT DEFAULT "" NOT NULL, `userId` TEXT DEFAULT '') [0.00ms] CREATE INDEX `profiles_created_idx` ON `profiles` (`created`) [0.00ms] INSERT INTO `_migrations` (`applied`, `file`) VALUES (1663705306, '1640988000_init.go') [0.00ms] SELECT count(*) FROM `_migrations` WHERE `file`='1661586591_add_externalAuths_table.go' LIMIT 1 [0.00ms] CREATE TABLE {{_externalAuths}} ( [[id]] TEXT PRIMARY KEY, [[userId]] TEXT NOT NULL, [[provider]] TEXT NOT NULL, [[providerId]] TEXT NOT NULL, [[created]] TEXT DEFAULT "" NOT NULL, [[updated]] TEXT DEFAULT "" NOT NULL, --- FOREIGN KEY ([[userId]]) REFERENCES {{_users}} ([[id]]) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE UNIQUE INDEX _externalAuths_userId_provider_idx on {{_externalAuths}} ([[userId]], [[provider]]); CREATE UNIQUE INDEX _externalAuths_provider_providerId_idx on {{_externalAuths}} ([[provider]], [[providerId]]); [1.00ms] -- crate new users table CREATE TABLE {{_newUsers}} ( [[id]] TEXT PRIMARY KEY, [[verified]] BOOLEAN DEFAULT FALSE NOT NULL, [[email]] TEXT DEFAULT "" NOT NULL, [[tokenKey]] TEXT NOT NULL, [[passwordHash]] TEXT NOT NULL, [[lastResetSentAt]] TEXT DEFAULT "" NOT NULL, [[lastVerificationSentAt]] TEXT DEFAULT "" NOT NULL, [[created]] TEXT DEFAULT "" NOT NULL, [[updated]] TEXT DEFAULT "" NOT NULL ); -- copy all data from the old users table to the new one INSERT INTO {{_newUsers}} SELECT * FROM {{_users}}; -- drop old table DROP TABLE {{_users}}; -- rename new table ALTER TABLE {{_newUsers}} RENAME TO {{_users}}; -- create named indexes CREATE UNIQUE INDEX _users_email_idx ON {{_users}} ([[email]]) WHERE [[email]] != ""; CREATE UNIQUE INDEX _users_tokenKey_idx ON {{_users}} ([[tokenKey]]); [0.00ms] INSERT INTO `_migrations` (`applied`, `file`) VALUES (1663705306, '1661586591_add_externalAuths_table.go') [0.00ms] SELECT {{_params}}.* FROM `_params` WHERE `key`='settings' LIMIT 1 [0.00ms] SELECT {{_params}}.* FROM `_params` WHERE `key`='settings' LIMIT 1 [0.00ms] INSERT INTO `_params` (`created`, `id`, `key`, `updated`, `value`) VALUES ('2022-09-20 20:21:46.182', 'bvewmm3203kpu4a', 'settings', '2022-09-20 20:21:46.182', '{"meta":{"appName":"Acme","appUrl":"http://localhost:8090","hideControls":false,"senderName":"Support","senderAddress":"support@example.com","verificationTemplate":{"body":"\u003cp\u003eHello,\u003c/p\u003e\n\u003cp\u003eThank you for joining us at {APP_NAME}.\u003c/p\u003e\n\u003cp\u003eClick on the button below to verify your email address.\u003c/p\u003e\n\u003cp\u003e\n \u003ca class=\"btn\" href=\"{ACTION_URL}\" target=\"_blank\" rel=\"noopener\"\u003eVerify\u003c/a\u003e\n\u003c/p\u003e\n\u003cp\u003e\n Thanks,\u003cbr/\u003e\n {APP_NAME} team\n\u003c/p\u003e","subject":"Verify your {APP_NAME} email","actionUrl":"{APP_URL}/_/#/users/confirm-verification/{TOKEN}"},"resetPasswordTemplate":{"body":"\u003cp\u003eHello,\u003c/p\u003e\n\u003cp\u003eClick on the button below to reset your password.\u003c/p\u003e\n\u003cp\u003e\n \u003ca class=\"btn\" href=\"{ACTION_URL}\" target=\"_blank\" rel=\"noopener\"\u003eReset password\u003c/a\u003e\n\u003c/p\u003e\n\u003cp\u003e\u003ci\u003eIf you didn''t ask to reset your password, you can ignore this email.\u003c/i\u003e\u003c/p\u003e\n\u003cp\u003e\n Thanks,\u003cbr/\u003e\n {APP_NAME} team\n\u003c/p\u003e","subject":"Reset your {APP_NAME} password","actionUrl":"{APP_URL}/_/#/users/confirm-password-reset/{TOKEN}"},"confirmEmailChangeTemplate":{"body":"\u003cp\u003eHello,\u003c/p\u003e\n\u003cp\u003eClick on the button below to confirm your new email address.\u003c/p\u003e\n\u003cp\u003e\n \u003ca class=\"btn\" href=\"{ACTION_URL}\" target=\"_blank\" rel=\"noopener\"\u003eConfirm new email\u003c/a\u003e\n\u003c/p\u003e\n\u003cp\u003e\u003ci\u003eIf you didn''t ask to change your email address, you can ignore this email.\u003c/i\u003e\u003c/p\u003e\n\u003cp\u003e\n Thanks,\u003cbr/\u003e\n {APP_NAME} team\n\u003c/p\u003e","subject":"Confirm your {APP_NAME} new email address","actionUrl":"{APP_URL}/_/#/users/confirm-email-change/{TOKEN}"}},"logs":{"maxDays":7},"smtp":{"enabled":false,"host":"smtp.example.com","port":587,"username":"","password":"","tls":false},"s3":{"enabled":false,"bucket":"","region":"","endpoint":"","accessKey":"","secret":"","forcePathStyle":false},"adminAuthToken":{"secret":"7orHNttunVoPRIma7W9M6DB5D6Ar1At4k5S57LOInAC2nZmQmz","duration":1209600},"adminPasswordResetToken":{"secret":"eYEWWWeYy0QrgicVfBQvyfkbiLBint3HHUZQDt4HLQU9ZCm87Q","duration":1800},"userAuthToken":{"secret":"pGfkKPBNvbpuuJp7Gkyi1oFSiMgkZBg97xyOQCv2gCqkR5uISd","duration":1209600},"userPasswordResetToken":{"secret":"CeOP5rOk9mnBOJ1DgucHgBjTGgFvoFqyup2lPrfGB8JmpJa4QV","duration":1800},"userEmailChangeToken":{"secret":"J2khYkHcSvLWSCTFs58uIAupRKzjP19SKg0pLxKps4AO0kSLht","duration":1800},"userVerificationToken":{"secret":"6mGIxXSCNlHSBLNQPKXqAE1mr7vQ5iFO2G4fwClLH16VCEL6Im","duration":604800},"emailAuth":{"enabled":true,"exceptDomains":null,"onlyDomains":null,"minPasswordLength":8},"googleAuth":{"enabled":false,"allowRegistrations":true},"facebookAuth":{"enabled":false,"allowRegistrations":true},"githubAuth":{"enabled":false,"allowRegistrations":true},"gitlabAuth":{"enabled":false,"allowRegistrations":true},"discordAuth":{"enabled":false,"allowRegistrations":true},"twitterAuth":{"enabled":false,"allowRegistrations":true}}') > Server started at: http://127.0.0.1:8090 - REST API: http://127.0.0.1:8090/api/ - Admin UI: http://127.0.0.1:8090/_/ 2022/09/20 13:21:46 listen tcp 127.0.0.1:8090: bind: address already in use exit status 1

Code

main.go

package main

import (
    "log"

    "github.com/pocketbase/pocketbase"
)

func main() {
    app := pocketbase.New()

    if err := app.Start(); err != nil {
        log.Fatal(err)
    }
}

go.mod

module github.com/tylersustare/running/backend

go 1.19

require github.com/pocketbase/pocketbase v0.7.5

require (
    github.com/AlecAivazis/survey/v2 v2.3.5 // indirect
    github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
    github.com/aws/aws-sdk-go v1.44.85 // indirect
    github.com/aws/aws-sdk-go-v2 v1.16.11 // indirect
    github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.4 // indirect
    github.com/aws/aws-sdk-go-v2/config v1.17.1 // indirect
    github.com/aws/aws-sdk-go-v2/credentials v1.12.14 // indirect
    github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.12 // indirect
    github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.27 // indirect
    github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.18 // indirect
    github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.12 // indirect
    github.com/aws/aws-sdk-go-v2/internal/ini v1.3.19 // indirect
    github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.9 // indirect
    github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.5 // indirect
    github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.13 // indirect
    github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.12 // indirect
    github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.13.12 // indirect
    github.com/aws/aws-sdk-go-v2/service/s3 v1.27.5 // indirect
    github.com/aws/aws-sdk-go-v2/service/sso v1.11.17 // indirect
    github.com/aws/aws-sdk-go-v2/service/sts v1.16.13 // indirect
    github.com/aws/smithy-go v1.12.1 // indirect
    github.com/disintegration/imaging v1.6.2 // indirect
    github.com/domodwyer/mailyak/v3 v3.3.4 // indirect
    github.com/fatih/color v1.13.0 // indirect
    github.com/gabriel-vasile/mimetype v1.4.1 // indirect
    github.com/ganigeorgiev/fexpr v0.1.1 // indirect
    github.com/go-ozzo/ozzo-validation/v4 v4.3.0 // indirect
    github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
    github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
    github.com/golang/protobuf v1.5.2 // indirect
    github.com/google/uuid v1.3.0 // indirect
    github.com/google/wire v0.5.0 // indirect
    github.com/googleapis/gax-go/v2 v2.5.1 // indirect
    github.com/inconshreveable/mousetrap v1.0.1 // indirect
    github.com/jmespath/go-jmespath v0.4.0 // indirect
    github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
    github.com/labstack/echo/v5 v5.0.0-20220201181537-ed2888cfa198 // indirect
    github.com/mattn/go-colorable v0.1.13 // indirect
    github.com/mattn/go-isatty v0.0.16 // indirect
    github.com/mattn/go-sqlite3 v1.14.15 // indirect
    github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
    github.com/pocketbase/dbx v1.6.0 // indirect
    github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
    github.com/spf13/cast v1.5.0 // indirect
    github.com/spf13/cobra v1.5.0 // indirect
    github.com/spf13/pflag v1.0.5 // indirect
    github.com/valyala/bytebufferpool v1.0.0 // indirect
    github.com/valyala/fasttemplate v1.2.1 // indirect
    go.opencensus.io v0.23.0 // indirect
    gocloud.dev v0.26.0 // indirect
    golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503 // indirect
    golang.org/x/image v0.0.0-20220722155232-062f8c9fd539 // indirect
    golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
    golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c // indirect
    golang.org/x/oauth2 v0.0.0-20220822191816-0ebed06d0094 // indirect
    golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect
    golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
    golang.org/x/text v0.3.7 // indirect
    golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
    golang.org/x/tools v0.1.12 // indirect
    golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
    google.golang.org/api v0.94.0 // indirect
    google.golang.org/appengine v1.6.7 // indirect
    google.golang.org/genproto v0.0.0-20220822174746-9e6da59bd2fc // indirect
    google.golang.org/grpc v1.49.0 // indirect
    google.golang.org/protobuf v1.28.1 // indirect
    lukechampine.com/uint128 v1.2.0 // indirect
    modernc.org/cc/v3 v3.36.3 // indirect
    modernc.org/ccgo/v3 v3.16.9 // indirect
    modernc.org/libc v1.17.0 // indirect
    modernc.org/mathutil v1.5.0 // indirect
    modernc.org/memory v1.2.0 // indirect
    modernc.org/opt v0.1.3 // indirect
    modernc.org/sqlite v1.18.1 // indirect
    modernc.org/strutil v1.1.2 // indirect
    modernc.org/token v1.0.0 // indirect
)
TylerSustare commented 2 years ago

I'm even getting this when running in a docker container

Terminal output

➜ docker run -p 8090:8090 pocket
docker: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:8090 -> 0.0.0.0:0: listen tcp 0.0.0.0:8090: bind: address already in use.
ERRO[0000] error waiting for container: context canceled 

Dockerfile

FROM golang:1.19

WORKDIR /usr/src/app

EXPOSE 8090

COPY go.mod go.sum ./
RUN go mod download && go mod verify

COPY . .
RUN go build -v -o /usr/local/bin/app ./...

CMD ["app", "serve"]
a10d commented 2 years ago

It looks like you're already using the port :8090in another process.

Maybe a local webserver or another container?

You can check which process it is with lsof -i | grep ":8090": pocketbas PROCESSID user ... TCP localhost:8090 (LISTEN)

TylerSustare commented 2 years ago

Hey @a10d Thank you for taking a look, but I mentioned in the description I killed all processes running on :8090, and running this in a docker container where there are definitely no other processes running on :8090 is still making this happen.

TylerSustare commented 2 years ago

Running it with go run main.go serve --http "localhost:8080" worked. So although nothing was running on :8090, something with the default config must not be working the way I would have expected. If it's working for others though I can close this.

a10d commented 2 years ago

Sorry, I missed that. I tried it on my machine with the dockerfile you provided and it also did not work, the connection was reset immediately. When I changed it explicitly serve over http, it worked. CMD ["app", "serve", "--http=0.0.0.0:8090"] Looks like its trying to serve over https (?) I did not get the error from docker tho, and running it on my machine directly it also works.

Looking at you terminal output, the process seems to start like it should, but when mapping the port on your machine, the error from the docker daemon shows up.

Does it work on another port?

a10d commented 2 years ago

Whoops, I was too slow. 😄