R
Railway•8mo ago
cybershizo

How do I make pg_dump available?

I am getting this error: exec: \"pg_dump\": executable file not found in $PATH" I setup my Railway TOML config like this
[build]
builder = "NIXPACKS"
buildCommand = "go build -o app cmd/iconflex-postgres-backup/*.go"

[build.nixpacksPlan.phases.setup]
nixPkgs = ["go", "postgresql"]

[deploy]
numReplicas = 1
startCommand = "./app"
# healthcheckPath = "/healthz"
sleepApplication = false
restartPolicyType = "ON_FAILURE"
restartPolicyMaxRetries = 1
[build]
builder = "NIXPACKS"
buildCommand = "go build -o app cmd/iconflex-postgres-backup/*.go"

[build.nixpacksPlan.phases.setup]
nixPkgs = ["go", "postgresql"]

[deploy]
numReplicas = 1
startCommand = "./app"
# healthcheckPath = "/healthz"
sleepApplication = false
restartPolicyType = "ON_FAILURE"
restartPolicyMaxRetries = 1
And I can confirm the service is reading the toml command. What am I doing wrong?
Solution:
Here's the full Dockerfile for reference ```Dockerfile FROM golang:1.22.0-alpine ...
Jump to solution
52 Replies
Brody
Brody•8mo ago
ive never been able to install postgresql with nixpacks, i always default to a Dockerfile, heres mine that would be a good starting point -
FROM golang:1.22.2-alpine

ARG PG_VERSION='16'

RUN apk add --update --no-cache postgresql${PG_VERSION}-client

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . ./

RUN go build -ldflags="-w -s" -o main

ENTRYPOINT ["/app/main"]
FROM golang:1.22.2-alpine

ARG PG_VERSION='16'

RUN apk add --update --no-cache postgresql${PG_VERSION}-client

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . ./

RUN go build -ldflags="-w -s" -o main

ENTRYPOINT ["/app/main"]
cybershizo
cybershizoOP•8mo ago
Thanks I'm explroign a docker file now
FROM golang:1.22.2-alpine3.18 AS builder

WORKDIR /app

# Copy Go modules and source code
COPY go.mod go.sum ./
COPY cmd/iconflex-postgres-backup/*.go cmd/iconflex-postgres-backup/

# Install dependencies
RUN go mod download

# Build the Go binary
RUN go build -o app ./cmd/iconflex-postgres-backup/*.go

FROM alpine:latest

# Install PostgreSQL client
ARG PG_VERSION='16'
RUN apk add --update --no-cache "postgresql${PG_VERSION}-client" --repository=https://dl-cdn.alpinelinux.org/alpine/edge/main

# Copy the built binary from the builder stage
COPY --from=builder /app/app /app/

WORKDIR /app

# Expose necessary port if your app needs it
# EXPOSE 8080

# Run the binary
CMD ["/app/app"]
FROM golang:1.22.2-alpine3.18 AS builder

WORKDIR /app

# Copy Go modules and source code
COPY go.mod go.sum ./
COPY cmd/iconflex-postgres-backup/*.go cmd/iconflex-postgres-backup/

# Install dependencies
RUN go mod download

# Build the Go binary
RUN go build -o app ./cmd/iconflex-postgres-backup/*.go

FROM alpine:latest

# Install PostgreSQL client
ARG PG_VERSION='16'
RUN apk add --update --no-cache "postgresql${PG_VERSION}-client" --repository=https://dl-cdn.alpinelinux.org/alpine/edge/main

# Copy the built binary from the builder stage
COPY --from=builder /app/app /app/

WORKDIR /app

# Expose necessary port if your app needs it
# EXPOSE 8080

# Run the binary
CMD ["/app/app"]
I am trying to figure out the steps Are you sure I need to use RUN apk add --update --no-cache postgresql${PG_VERSION}-client --repository=https://dl-cdn.alpinelinux.org/alpine/edge/main instead of something more declaraitive?
Brody
Brody•8mo ago
wydm? you want pg_dump and thats how you install it
cybershizo
cybershizoOP•8mo ago
Kk I'll try it out now
Brody
Brody•8mo ago
although you dont need to edge anymore, postgresql16-client is now on 3.19
cybershizo
cybershizoOP•8mo ago
Can you use a Dockerfile and Railway TOML together?
Brody
Brody•8mo ago
yeah but only the setup stuff would be used
cybershizo
cybershizoOP•8mo ago
I meant to configure the health server and retries, etc.
Brody
Brody•8mo ago
yeah you can still use the deploy stuff in a railway.toml file with a dockerfile
cybershizo
cybershizoOP•8mo ago
Kk Thank you
Brody
Brody•8mo ago
no problem, let me know if that works for you
cybershizo
cybershizoOP•8mo ago
Getting some problems but not directly related So it looks like this
FROM golang:1.22.0-alpine

ARG PG_VERSION='16'

RUN apk add --update --no-cache postgresql${PG_VERSION}-client --repository=https://dl-cdn.alpinelinux.org/alpine/edge/main

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . ./

RUN go build -o app cmd/iconflex-admin/*.go

ENTRYPOINT ["./app"]
FROM golang:1.22.0-alpine

ARG PG_VERSION='16'

RUN apk add --update --no-cache postgresql${PG_VERSION}-client --repository=https://dl-cdn.alpinelinux.org/alpine/edge/main

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . ./

RUN go build -o app cmd/iconflex-admin/*.go

ENTRYPOINT ["./app"]
Here are the logs
cybershizo
cybershizoOP•8mo ago
Gist
logs
GitHub Gist: instantly share code, notes, and snippets.
Brody
Brody•8mo ago
its still using nixpacks, make sure your Dockerfile is named correctly
cybershizo
cybershizoOP•8mo ago
Gist
logs
GitHub Gist: instantly share code, notes, and snippets.
cybershizo
cybershizoOP•8mo ago
Oh wait OK so I'm using a monorepo
Brody
Brody•8mo ago
set the root directory in the service settings
cybershizo
cybershizoOP•8mo ago
Can you colocate a Dockerfile per command?
Brody
Brody•8mo ago
explain what you mean by that please
cybershizo
cybershizoOP•8mo ago
So I have like cmd/foo/main.go cmd/bar/main.go How can I have a Dockerfile per service? Can I customize the Dockerfile per service in Railway?
Brody
Brody•8mo ago
you could have a Dockerfile.foo and a Dockerfile.bar and then choose what Dockerfile to use with RAILWAY_DOCKERFILE_PATH
cybershizo
cybershizoOP•8mo ago
No description
cybershizo
cybershizoOP•8mo ago
Ah Right
Brody
Brody•8mo ago
exactly
cybershizo
cybershizoOP•8mo ago
OK will try now thank you Wow it worked
Brody
Brody•8mo ago
awsome
Solution
cybershizo
cybershizo•8mo ago
Here's the full Dockerfile for reference
FROM golang:1.22.0-alpine

ARG PG_VERSION='16'

RUN apk add --update --no-cache postgresql${PG_VERSION}-client --repository=https://dl-cdn.alpinelinux.org/alpine/edge/main

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . ./

RUN go build -o app cmd/iconflex-postgres-backup/*.go

ENTRYPOINT ["./app"]
FROM golang:1.22.0-alpine

ARG PG_VERSION='16'

RUN apk add --update --no-cache postgresql${PG_VERSION}-client --repository=https://dl-cdn.alpinelinux.org/alpine/edge/main

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . ./

RUN go build -o app cmd/iconflex-postgres-backup/*.go

ENTRYPOINT ["./app"]
cybershizo
cybershizoOP•8mo ago
Is there any reason not to always use a Docker file in general? In my case it would allow me to always use the latest Go version without waiting for Nix
Brody
Brody•8mo ago
you lose caching, and now you have to maintain this dockerfile
cybershizo
cybershizoOP•8mo ago
OK so use Railway as is, then lean into Nix if you can, and finally use Docker if you have to? As a general decision tree
Brody
Brody•8mo ago
exactly! you could also use Dockerfiles to have a multi layered image where the final image that gets published is a small alpine or scratch image, this is what i do for really fast publish times
cybershizo
cybershizoOP•8mo ago
OK so for the smallest, fastest build what does that Dockerfile look like? That sounds really interesting
Brody
Brody•8mo ago
in my example i have omitted that for brevity, but the Dockerfile you sent at first does do that
Brody
Brody•8mo ago
everything after the second FROM is a fresh small alpine image and doesnt contain all the bulky build tooling that the golang image does
No description
cybershizo
cybershizoOP•8mo ago
But how do you build Go then?
Brody
Brody•8mo ago
you do that under the first FROM golang image
cybershizo
cybershizoOP•8mo ago
Oh you're saying basically the same as the script I'm using just without the Postgres step? Alpine + Go then just Alpine?
Brody
Brody•8mo ago
no you still need postgres installed, since you still want pg_dump
cybershizo
cybershizoOP•8mo ago
Sorry I mean for another script where I want fast as possible deploys and there's no pg_dump involved
Brody
Brody•8mo ago
ah then yes
cybershizo
cybershizoOP•8mo ago
That is just golang-alpine to build then alpine to run, and that is generally faster than just Railway by itself? I'd be really interested in like 10 second deploys
Brody
Brody•8mo ago
you couldn't get down to a total of 10 seconds, my multistage builds are ~13 seconds and the publish time is ~6 seconds you dont get build caching like you do using nixpacks, but you skip installing all the nix stuff so its still generally faster in my experience
cybershizo
cybershizoOP•8mo ago
OK so your fastest deploys are like 20-30 seconds?
Brody
Brody•8mo ago
yeah but theres still the switch over time when railway tries to do the zero downtime deploys
cybershizo
cybershizoOP•8mo ago
Gotcha OK thanks for the help I'll close this out Thank you
Brody
Brody•8mo ago
please dont close threads
cybershizo
cybershizoOP•8mo ago
Ah just leave it?
Brody
Brody•8mo ago
yeah
cybershizo
cybershizoOP•8mo ago
:salute:
Brody
Brody•8mo ago
and you still closed it anyway 😆
cybershizo
cybershizoOP•8mo ago
Did I? I didn't press anything
Want results from more Discord servers?
Add your server