在GCP上构建Docker映像时进程被杀死

时间:2020-07-01 04:29:27

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

我是云计算的新手,正在努力将自己的Web应用程序首次部署到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

1 个答案:

答案 0 :(得分:1)

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

请在下面找到我的步骤

  1. 启用Google Container Registry API

  2. 创建VM实例:

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