【问题标题】:ENV variables not coming through godotenv DockerENV 变量不来自 godotenv Docker
【发布时间】:2021-05-24 15:02:11
【问题描述】:

我有一个用 Go 编写的网络应用程序,码头化并使用 gomod。

我无法让它读取环境变量。

在运行 docker-compose up 时,总是返回“Error getting env, not comming through”

我正在使用 godotenv 来尝试这样做。下面是我的实现。我终其一生都无法弄清楚出了什么问题。如果有人能看到我遗漏的东西,你将挽救生命。

main.go、.env、docker-compose.yml 和 Dockerfile 都在项目的根目录下

ma​​in.go

func main() {
    router := mux.NewRouter()

    err := godotenv.Load()
    if err != nil {
        log.Fatalf("Error getting env, not comming through %v", err)
    } else {
        fmt.Println("We are getting the env values")
    }

    fmt.Println(os.Getenv("MY_ENV"))

}

.env

MY_ENV=thisismyenvvariable
DB_HOST=testdata123
DB_DRIVER=testdata123
DB_USER="testdata123"
DB_PASSWORD=testdata123
DB_NAME=testdata123
DB_PORT=5432

docker-compose.yml

version: '3'
services:
  app:
    container_name: template_123
    build: .
    ports: 
      - 8080:8080 
    restart: on-failure
    volumes:
      - api:/usr/src/app/
    env_file:
      - .env
    depends_on:
      - template-postgres          
    networks:
      - template


  template-postgres:
    image: postgres:latest
    container_name: startup_template_golang_db_postgres
    environment:
      - POSTGRES_USER=${DB_USER}  
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=${DB_NAME}
      - DATABASE_HOST=${DB_HOST} 
    ports:
      - '5432:5432'
    volumes:
      - database_postgres:/var/lib/postgresql/data
    env_file:
      - .env
    networks:
      - template
    
  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin_container
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
    depends_on:
      - template-postgres
    ports:
      - "5050:80"
    networks:
      - template
    restart: unless-stopped

volumes:
  api:
  database_postgres:                  

# Networks to be created to facilitate communication between containers
networks:
  startup_template:
    driver: bridge

Dockerfile

# Start from golang base image
FROM golang:alpine as builder

# ENV GO111MODULE=on

# Add Maintainer info
LABEL maintainer="satoshi123"

# Install git.
# Git is required for fetching the dependencies.
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 .
# COPY --from=builder /app/.env .       

# Expose port 8080 to the outside world
EXPOSE 8080

#Command to run the executable
CMD ["./main"]

【问题讨论】:

  • 实际错误是什么? .env 文件是如何进入容器的? (Compose env_file: 使用它的内容来设置环境变量,但看起来你既没有将 COPY 放入图像中,也没有绑定安装它。)
  • 另外,您能否输入图像 (docker-compose exec -it template_123 ash) 并运行 env 以查看其中的内容。如果.env 文件被docker-compose 的env_file 指令正确拾取,它应该被添加到容器的实际环境中,你甚至不需要godotenv。

标签: docker go docker-compose environment-variables


【解决方案1】:

如果你已经在你的docker_compose.yml 中使用env_file,你就不需要 godotenv,因为环境已经从 docker-compose 传下来了:

version: '3'

services:
    app:
        image: busybox:latest
        command: sh -c 'echo "Hello $$USER!"'
        env_file:
            - .env
# .env
USER=user1
$ docker-compose up
Recreating test_app_1 ... done
Attaching to test_app_1
app_1  | Hello user1!
test_app_1 exited with code 0

这比尝试将 .env 文件复制到容器中更好,因为这意味着您可以传递环境变量而无需每次都重新构建容器;)

如果您仍然想使用 godotenv,我发现只需从 Dockerfile 中取消注释 COPY --from=builder /app/.env . 行,.env 文件就会正确加载(因为 godotenv 在目录中找到它,而如果它被注释它不会) t)。

$ docker-compose up
Starting template_123 ... done
Attaching to template_123
template_123 | We are getting the env values
template_123 | thisismyenvvariable
template_123 exited with code 0

如果你想让它与你的文件系统保持同步,你需要使用一个卷来链接你的 .env 和你文件系统上的那个,或者就像我说的那样,完全抛弃godotenv,因为它不是在你的情况下真的很有用。

【讨论】:

  • 非常感谢没有 godotenv 仍然无法通过,但至少现在可以正常工作
猜你喜欢
  • 2021-07-02
  • 2018-04-20
  • 1970-01-01
  • 1970-01-01
  • 2019-10-20
  • 2021-01-24
  • 2020-10-18
  • 2021-08-27
  • 2018-09-21
相关资源
最近更新 更多