【问题标题】:Process got killed when building docker image on GCP在 GCP 上构建 docker 映像时进程被杀死
【发布时间】:2020-10-21 11:33:00
【问题描述】:

我是云计算的新手,第一次在谷歌云平台上部署我的网络应用程序时遇到了困难。当我在构建 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

【问题讨论】:

  • 你能粘贴你的 dockerfile 内容吗?
  • 另外,请提供requirements.txt
  • 已编辑,感谢您的 cmets!
  • @MemphisMeng 您可能遇到了内存不足错误。尝试使用-it 标志进行调试或为容器分配更多内存。
  • 标志-it在哪里使用?

标签: docker google-cloud-platform pytorch torchvision streamlit


【解决方案1】:

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

请在下面找到我的步骤:

  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

【讨论】:

  • 谢谢,Serhii。我认为您的方式应该是正确的方式,但我仍然遇到了这个错误。所以我想知道你在requirements.txt 中放了什么包?我需要 PyTorch,每次在步骤 8/16 安装 PyTorch 时它都会被杀死。我知道 Torch 是一个巨大的包裹,所以如果你检查它,我不会使用它。
  • 我根本没有改变requirements.txt。我更改了 Docker 文件(在第 6 步添加了')并使用了一些虚拟的app.pyprint 命令。此外,我已将 Python 3.7 安装到用于构建 Docker 映像的 GCP VM。你在哪一步有错误?
  • 我在收集火炬时仍然被杀,这是第8/16步
  • 能否请您检查一下您的系统中是否有足够的磁盘空间和 Python 3.7 安装?请使用构建日志更新您的问题。
  • 我认为是的,因为我在浏览器上使用了终端并按照您的建议创建了虚拟机。
猜你喜欢
  • 2023-03-13
  • 1970-01-01
  • 2010-12-08
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
相关资源
最近更新 更多