R
Railway2y ago
Knox

Release Go app with Docker and godotenv

func LoadEnvVariables() (ServerConfig, error) {
cnf := ServerConfig{}
err := envconfig.Process("", &cnf)
return cnf, err
}
func LoadEnvVariables() (ServerConfig, error) {
cnf := ServerConfig{}
err := envconfig.Process("", &cnf)
return cnf, err
}
I have this function that I use on my server setup to load the .env variable and on my main.go I do a simple load
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
Locally it works but when hosting with a Dockerfile the app crashes because it can't load the env variable
# Start from golang base image
FROM golang:alpine as builder

# Add Maintainer info
LABEL maintainer="a11199"

# Install git.
RUN apk update && apk add --no-cache git

# Set the current working directory inside the container
WORKDIR /app

# Copy go mod and sum files
COPY go.mod go.sum ./

# Download all dependencies. Dependencies will be cached if the go.mod and the go.sum files are not changed
RUN go mod download

# Copy the source from the current directory to the working Directory inside the container
COPY . .

# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

# Start a new stage from scratch
FROM alpine:latest
RUN apk --no-cache add ca-certificates

WORKDIR /root/

# Copy the Pre-built binary file from the previous stage. Observe we also copied the .env file
COPY --from=builder /app/main .

# Expose port 8080 to the outside world
EXPOSE 8080

#Command to run the executable
CMD ["./main"]
# Start from golang base image
FROM golang:alpine as builder

# Add Maintainer info
LABEL maintainer="a11199"

# Install git.
RUN apk update && apk add --no-cache git

# Set the current working directory inside the container
WORKDIR /app

# Copy go mod and sum files
COPY go.mod go.sum ./

# Download all dependencies. Dependencies will be cached if the go.mod and the go.sum files are not changed
RUN go mod download

# Copy the source from the current directory to the working Directory inside the container
COPY . .

# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

# Start a new stage from scratch
FROM alpine:latest
RUN apk --no-cache add ca-certificates

WORKDIR /root/

# Copy the Pre-built binary file from the previous stage. Observe we also copied the .env file
COPY --from=builder /app/main .

# Expose port 8080 to the outside world
EXPOSE 8080

#Command to run the executable
CMD ["./main"]
The app is deployed but eventually crashes with
2023/06/14 09:44:30 Error loading .env file
open .env: no such file or directory
2023/06/14 09:44:30 Error loading .env file
open .env: no such file or directory
Any clue ? I know this isn't a railway problem more but any feedback would be appreciated.
7 Replies
Percy
Percy2y ago
Project ID: N/A
Knox
KnoxOP2y ago
N/A
Brody
Brody2y ago
I like the part where you said "we copied the .env file" but then you never copied the .env file also, you don't need a .env file anyway, that's what service variables are for
I know this isn't a railway problem
that's a breath of fresh air honestly, finally someone not blaming the platform when something goes wrong, I like you
Knox
KnoxOP2y ago
Platform is great. I just haven’t touched Docker and Docker compose since school. It’s a skill issue. The reason why I dont copy the .env is because it’s supposed to not be a “good” practice to do so… but I’m far from being on my comfort zone on this to know for sure.
Brody
Brody2y ago
it's my opinion that using a .env file to store credentials for public facing services (like a railway postgres for example) is a terrible idea (even locally) an .env file is plaintext after all, and throughout my time here I have seen many users database credentials and api keys just hanging around in their repos when they share them so I can help them. so would you be interested in totally removing your dependency on your .env file in favour of railway service variables, and the railway cli's run and shell functionality that can securely pull credentials from railway to run your app locally without ever having to write credentials to a file
Knox
KnoxOP2y ago
I did this:
if os.Getenv("APP_ENV") == "dev" {
err := godotenv.Load("dev.env")
if err != nil {
fmt.Println(err)
log.Fatal("Error loading dev.env file")
}
}
if os.Getenv("APP_ENV") == "dev" {
err := godotenv.Load("dev.env")
if err != nil {
fmt.Println(err)
log.Fatal("Error loading dev.env file")
}
}
So the dev.env is only applied in development mode. Just a workaround for this api that I actually want to have in production and deploy a mobile / web app asap.
Brody
Brody2y ago
but you dont need a .env file anywhere, locally or on railway
Want results from more Discord servers?
Add your server