9 months ago
TL;DR
I recently migrated my personal website from the tailwind CDN to tailwind proper. In my go code for starting my server, I'm using [exec.Run](exec.Run)()
to run tailwind's build process on server startup. All of this is working excellently on my local machine, but when I deploy it to Railway it appears to be launching the server in a loop about every five seconds.
What changed
I added node as a dependency to my deploy so that
npx
could be used for the tailwind cliI wrote the following function in order to do the automatic build:
func buildTailwind(infile, outfile string) error {
build := exec.Command(
"npx",
"tailwindcss",
"-i",
infile,
"-o",
outfile,
)
build.Stdout = os.Stdout
build.Stderr = os.Stderr
return build.Run()
}
I'm not actually using the error return from that function, so even if the build fails it shouldn't be causing the server restart (and didn't when I initially deployed it without the node dependency)
I'm aware that it's silly to ignore that error and I'm going to fix that. But it doesn't explain the problem so I'm leaving it as-is for now
that's really it. I'm not super sure where to start looking for what the problem might be. For now the deployment has been canceled because it's just my personal site and I would rather have it down than run up my compute bill on a bug I don't know how to fix lol. Any suggestions would be greatly appreciated.
0 Replies
9 months ago
you are likely trying to use more than 500MB of memory, if you still have issues after upgrading to the hobby plan we can then debug this further
oh, okay. I wasn't aware of the memory constraint, let me take a look at that and try upgrading
9 months ago
yeah 500MB on trial
ok looks like upgrading worked but now it's⦠not building properly? or not saving the output? Let me set it up to panic if the tailwind build fails and that'll probably give me some more info
9 months ago
why not run that tailwind build command as a regular build command? is there any specific reason you are doing it in such a convoluted way?
now i want it to work but if it doesn't i'll probably just cave and do it as a build command
but i thought it would be cool to have the server handle the tailwind build itself
9 months ago
id go with a multi stage Dockerfile where the first stage is a node image and it builds the css, then the second stage is a golang image and then you copy over the built files into the final image
9 months ago
you dont though
9 months ago
its just a text file
9 months ago
in all my years of writing Dockerfiles, i have never once ran docker on my own computer
so this should be one dockerfile with two sections, one running a node image and the second running a go image?
9 months ago
you got your terminology wrong, but you have the idea more or less, if you want to share your repo i can try to write one for you
I think I have one that should work, but if you want to give it a sanity check that would be great
FROM node:20.12
WORKDIR /app
COPY package*.json ./
RUN npx tailwindcss -i static/input.css -o dist/style.css
FROM golang:1.22
COPY go.mod go.sum ./
RUN go mod download
COPY *.go ./
RUN go build -o bin/personal-site
ENV PORT=8080
CMD ["bin/personal-site"]
9 months ago
you are on the right track, but your golang layer does not have a work dir, and you have no copied the built css into the golang layer either
if I wanna just copy the whole dist dir onto a dist dir in the image, it would just be COPY dist/ ./dist
right? or do I have to make a directory on the image first?
9 months ago
COPY will make paths as needed, but you need to copy the dist dir from the node layer
okay, and that will stay when you enter the go layer. gotcha. I was wondering about that
9 months ago
it wont stay unless you copy it with --from
like this? ```dockerfile
FROM node:20.12
WORKDIR /app
COPY package*.json ./
RUN npx tailwindcss -i static/input.css -o dist/style.css
COPY --from=golang:1.22 dist/ ./dist
FROM golang:1.22```
9 months ago
FROM node:20.12 AS assets
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY static ./static
RUN npx tailwindcss -i ./static/input.css -o ./dist/style.css
FROM golang:1.22
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
COPY --from=assets /app/dist ./dist
RUN go build -o bin/personal-site
CMD ["bin/personal-site"]
9 months ago
you where copying the wrong way
and doing COPY . ./
instead of COPY *.go ./
will add my other packages as well I assume?
do I need to worry about node modules from the node step with that or do they go away when the stage changes to the go one
9 months ago
everything goes away unless you specifically copy it over
and I assume I can just add COPY --from=assets /app/static ./static
to keep the static dir as well? It's got my fonts and my resume lol
9 months ago
updated
9 months ago
good catch, you are right, updated
9 months ago
you catch on fast
did not copy the views folder into the assets phase so that tailwind could read from it
9 months ago
are you keeping the dist folder out of github?
I would post a screenshot of the successful build screen if I knew how to take a screenshot on this computer. I guess I should figure out how to do that. But it works!
I had a lot of internal turmoil about whether or not a font is distributable but I decided to just keep them in static so i could just put dist/ in my .gitignore
9 months ago
always, always make sure to keep the dist or build folders out of github, as whatever would end up in them should be done by the build
9 months ago
if its static like fonts then static is what you want and that goes to github
now I have to rebuild the fonts from source because firefox doesn't like unhinted ttfs
9 months ago
happy to help!