How should I configure Dockerfile for golang proj with a CA.pem file? Inside the container CA,pem can't be found

huangapple go评论76阅读模式
英文:

How should I configure Dockerfile for golang proj with a CA.pem file? Inside the container CA,pem can't be found

问题

我有一个服务器项目。处理程序从一个postgres数据库中提供一些数据。为了连接,我有一个CA.pem文件。
当我在本地运行main.go时,它是正常工作的。CA.pem文件位于proj文件夹中。它是这样调用的:

pem, err := ioutil.ReadFile("CA.pem")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to read the CA file:%s - %s", el, err)
		return err
	}

	if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
		fmt.Fprint(os.Stderr, "Failed to append PEM")
	}

项目结构:

├──project/
    ├──main.go  
    ├──Dockerfile  
    ├──CA.pem 
    ├──go.mod  
    ├──...  
    ├──folder1/
    |  ├─...
    └──folder2/
       └──...

Dockerfile如下:

# syntax=docker/dockerfile:1
## 
## Build
## 
FROM golang:1.17 AS build

WORKDIR /app/proj

COPY go.mod .
COPY go.sum .
RUN go mod download

COPY . .

RUN go build -o ./out/proj .

##
## Deploy
##
FROM gcr.io/distroless/base-debian10

WORKDIR /

COPY --from=build /app/proj/out/proj /app/proj

EXPOSE 8080

ENTRYPOINT ["/app/proj"]

所以当我尝试运行容器时,我收到以下错误:

> Failed to read the CA file:spaceship - open CA.pem: no such file or
> directory
> Error during connection to databaseinteractions: open CA.pem: no such file or directory

*spaceship - 数据库名称
*databaseinteractions - 我有"connect_to_db.go"文件的文件夹。

服务器正在运行。我特意创建了一个"status"处理程序,它提供给我201响应。这是没问题的。但是无法建立数据库连接。
它找不到CA.pem文件。但是为什么呢?
也许我应该以不同的方式创建docker镜像?或者以不同的方式读取文件?

谢谢你的帮助!

英文:

I have a server project. Handlers provides some data from a postgres DB. For connect I have a CA.pem.
When I run main.go locally - it's working. CA.pem is in the proj folder. It's invoked like this:

pem, err := ioutil.ReadFile("CA.pem")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to read the CA file:%s - %s", el, err)
		return err
	}

	if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
		fmt.Fprint(os.Stderr, "Failed to append PEM")
	}

Proj structure:

├──project/
    ├──main.go  
    ├──Dockerfile  
    ├──CA.pem 
    ├──go.mod  
    ├──...  
    ├──folder1/
    |  ├─...
    └──folder2/
       └──...

Dockerfile looks like:

# syntax=docker/dockerfile:1
## 
## Build
## 
FROM golang:1.17 AS build

WORKDIR /app/proj

COPY go.mod .
COPY go.sum .
RUN go mod download

COPY . .

RUN go build -o ./out/proj .

##
## Deploy
##
FROM gcr.io/distroless/base-debian10

WORKDIR /

COPY --from=build /app/proj/out/proj /app/proj

EXPOSE 8080

ENTRYPOINT ["/app/proj"]

So when I'm trying to run the container
I receive this err:

> Failed to read the CA file:spaceship - open CA.pem: no such file or
> directory
> Error during connection to databaseinteractions: open CA.pem: no such file or directory

*spaceship - db name
*databaseinteractions - folder where I have the "connect_to_db.go" file.

The server is running. I've specially created a "status" handler, which provide me 201 response. It's ok. But the DB connection can't be established.
It doesn't see the CA.pem file. But why?
May be I should create the docker image differently? Or read the file differently?

Thank you for any help!

答案1

得分: 2

当你在本地运行程序时,它会找到CA.pem文件。但是在Docker中,你只保留了项目的可执行构建文件在Debian镜像中。

为了成功运行它,你需要将CA.pem文件复制到项目目录中。

COPY --from=build /app/proj/CA.pem /app/proj
英文:

When you run locally Program finds CA.pem file. But in docker you are keeping only executable build of the project in debian image.

You need to copy the CA.pem file in the project directory in order to run it successfully.

COPY --from=build /app/proj/CA.pem /app/proj

huangapple
  • 本文由 发表于 2022年1月18日 17:27:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/70753173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定