在 GCP 上构建 docker 映像时进程被终止

Process got killed when building docker image on GCP

我是云计算的新手,第一次在 google 云平台上部署我的网络应用程序时遇到了困难。当我构建 docker 图像时,这意味着我 运行 以下代码

docker build -t gcr.io/${PROJECT_ID}/insurance-streamlit:v1 .

进程被终止,显示以下错误:

Killed
The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 137

我猜我的应用程序可能太大了,因为权重文件已经超过 100MB。那么有没有办法解决它?请告诉我详情,提前谢谢!

PS: 我的 Dockerfile 如下:

FROM python:3.7
RUN pip install virtualenv
ENV VIRTUAL_ENV=/venv
RUN virtualenv venv -p python3
ENV PATH="VIRTUAL_ENV/bin:$PATH"

WORKDIR /app
ADD . /app

# Install dependencies
RUN pip install -r requirements.txt

# copying all files over
COPY . /app

# Expose port 
ENV PORT 8501

# cmd to launch app when container is run
CMD streamlit run app.py

# streamlit-specific commands for config
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8
RUN mkdir -p /root/.streamlit
RUN bash -c 'echo -e "\
[general]\n\
email = \"\"\n\
" > /root/.streamlit/credentials.toml'

RUN bash -c 'echo -e "\
[server]\n\
enableCORS = false\n\
" > /root/.streamlit/config.toml

而我的 requirements.txt 就像:

albumentations==0.4.5
numpy==1.19.0
opencv-python==4.1.0.25
opencv-python-headless==4.2.0.34
pandas==1.0.5
Pillow==7.1.2
streamlit==0.62.0
torch==1.4.0
torchvision==0.5.0
matplotlib

我发现要构建您的 Docker 映像,您应该有足够的磁盘 space 和 Python 3.7 安装,而且您的 Docker 文件中有一个拼写错误 - 没有最后一个字符串末尾的单引号 '。除此之外,一切看起来都不错 运行s.

请在下面找到我的步骤:

  1. 启用Google容器注册表API

  2. 创建虚拟机实例:

gcloud compute instances create instance-4 --zone=europe-west3-a --machine-type=e2-medium --image=ubuntu-1804-bionic-v20200701 --image-project=ubuntu-os-cloud --boot-disk-size=50GB 
  1. 遵循文档 Pushing and pulling images
  2. 安装Python 3.7:
sudo apt install python3.7
  1. 构建Docker图像:
docker build -t gcr.io/test-prj/testimage:v1 .
...
Step 16/16 : RUN bash -c 'echo -e "[server]\nenableCORS = false\n" > /root/.streamlit/config.toml
 ---> Running in 57502f97cfbe
/bin/sh: 1: Syntax error: Unterminated quoted string
The command '/bin/sh -c bash -c 'echo -e "[server]\nenableCORS = false\n" > /root/.streamlit/config.toml' returned a non-zero code: 2
  1. 更改 Docker 文件的最后一个字符串:
" > /root/.streamlit/config.toml'
  1. 再次构建 Docker 图像:
docker build -t gcr.io/test-prj/testimage:v1 .
...
Step 16/16 : RUN bash -c 'echo -e "[server]\nenableCORS = false\n" > /root/.streamlit/config.toml'
 ---> Running in c1c1f81a2d09
Removing intermediate container c1c1f81a2d09
 ---> 24b6609de554
Successfully built 24b6609de554
Successfully tagged gcr.io/test-prj/testimage:v1
$ docker images
REPOSITORY                            TAG                 IMAGE ID            CREATED             SIZE
gcr.io/test-prj/testimage             v1                  24b6609de554        14 minutes ago      3.87GB
  1. 将 Docker 图像推送到注册表:
gcloud docker -- push gcr.io/test-prj/testimage:v1
  1. 创建新的 VM 实例并部署映像:
gcloud compute instances create-with-container instance-5 --zone=europe-west3-a --machine-type=e2-medium --image=cos-stable-81-12871-148-0 --image-project=cos-cloud --boot-disk-size=50GB --container-image=gcr.io/test-prj/testimage:v1 
  1. 检查 Docker 容器的状态:
instance-5 ~ $ docker ps
CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS                                  PORTS               NAMES
e21b80dc0de7        gcr.io/test-prj/testimage:v1   "/bin/sh -c 'streaml…"   28 seconds ago      Restarting (2) Less than a second ago                       klt-instance-5-caqx

而且看起来不太好。

  1. 停止容器:
instance-5 ~ $docker stop e21b80dc0de7
  1. 以交互方式关注 documentation 和 运行 容器:
instance-5 ~ $docker run --name test -it gcr.io/test-prj/testimage:v1
Usage: streamlit run [OPTIONS] TARGET [ARGS]...

Error: Invalid value: File does not exist: app.py

不足为奇,因为我没有 app.py

在那之后,我添加了一些虚拟 app.py,重建并最终成功:

instance-6 ~ $ docker ps
CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS              PORTS               NAMES
1de2e8ded5d8        gcr.io/test-prj/testimage:v2   "/bin/sh -c 'streaml…"   7 minutes ago       Up 7 minutes                           klt-instance-6-yezv