【问题标题】:Docker, docker-compose failure when creating instance, runtime failureDocker,创建实例时 docker-compose 失败,运行时失败
【发布时间】:2019-09-06 19:13:54
【问题描述】:

我正在尝试通过 docker-compose 运行应用程序。 docker 图像构建得很好,它是在创建问题/异常发生的容器中。我知道这肯定与涉及 gunicorn 的 bash/sh 命令有关。我只是没有足够的知识来解决我遇到的问题。

基本上我运行以下命令:

docker-compose build
docker compose up 

(是的,我知道你应该运行 docker compose up -d,但是这个方法通过控制台清楚地说明了我的问题)

我收到的特定错误消息是:

Creating network "flaskapi_default" with the default driver
Creating app ...
Creating app ... error

ERROR: for app  Cannot start service api: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"bash\": executable file not found in $PATH": unknown

ERROR: for api  Cannot start service api: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"bash\": executable file not found in $PATH": unknown
ERROR: Encountered errors while bringing up the project.

我尝试与 docker-compose 分开构建和运行 dockerfile,并取得了不同程度的成功。这让我在 ENTRYPOINT CMD 问题上搞混了。所以,我更喜欢使用 docker-compose。

Dockerfile:

    FROM python:3.6-alpine3.7

    RUN apk add --no-cache --update \
        python3 python3-dev gcc \
        gfortran musl-dev \
        libffi-dev openssl-dev

    RUN pip install --upgrade pip

    ENV PYTHONUNBUFFERED 1
    ENV APP /app

    RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
    RUN mkdir $APP
    WORKDIR $APP

    ADD requirements.txt .
    RUN pip install -r requirements.txt

    COPY . .

docker-compose 文件:

version: "2"

services:
  nginx:
    image: nginx:latest
    container_name: nginx
    ports:
      - "80:8000"
    volumes:
      - ./:/app
      - ./config/nginx:/etc/nginx/conf.d
      - ./config/nginx/ssl/certs:/etc/ssl/certs
      - ./config/nginx/ssl/private:/etc/ssl/private
    depends_on:
      - api
  api:
    build: .
    container_name: app
    command: sh -c "gunicorn -w 1 -b 0.0.0.0:5000 manage:app"
    expose:
      - "5000"

我很确定我的命令是正确的,它可以在我的开发环境中运行,顺便说一下是 MAC OS。这个问题发生在我的其他基于 linux 的环境中。这个特定的提取来自全新的 ubuntu 18.04 安装。

我已经尝试将 /bin/bash 或 /bin/sh 添加到 bash 或 docker-compose 文件的 sh 命令中,结果相似。我现在正在拔头发,所以,帮帮我obi-wan-kenobi。

使用 /bin/sh 而不是 bash 时的错误。这似乎更有可能是答案,因为我能够确定某些 docker 容器不支持 bash。

错误:

Creating network "flaskapi_default" with the default driver
Creating app ...
Creating app ... done
Creating nginx ...
Creating nginx ... done
Attaching to app, nginx
app      |   File "/bin/sh", line 1
app      | SyntaxError: Non-UTF-8 code starting with '\xb1' in file /bin/sh on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
app exited with code 1
nginx_flask_api | 2019/09/06 19:25:33 [emerg] 1#1: host not found in upstream "api:5000" in /etc/nginx/conf.d/flask.conf:3
nginx_flask_api | nginx: [emerg] host not found in upstream "api:5000" in /etc/nginx/conf.d/flask.conf:3
nginx exited with code 1

只是为了迭代,我很确定问题不在 flask.conf 中。

【问题讨论】:

  • 需要定义 docker 容器如何看待自己。检查 container_name 和链接属性。尝试从容器中检查该名称是否存在主机、ping、curl 并且连接是否正常。
  • 容器从未被实例化。但无论如何谢谢,相信我设法解决了这个问题。

标签: linux bash docker docker-compose


【解决方案1】:

所以我已经找到了解决方案。似乎ENTRPOINT和CMD是不同的。大惊喜。我只需将 /bin/sh 添加到 docker-compose 文件中,并从 Dockerfile 中删除对 ENTRYPOINT 的使用。

另外,愚蠢的是,我没有附加在我使用 ENTRYPOINT [“python”] 得到原始错误的实例中。从 dockerfile 中删除该文件并将 bin/sh 添加到 docker-compose 文件似乎可以解决问题。两个容器现在都在运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2022-01-16
    • 2021-12-17
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 2020-06-15
    相关资源
    最近更新 更多