![](/img/trans.png)
[英]Running a Flask app within a Docker container results in “./boot.sh: 2: exec: gunicorn: not found”
[英]docker run results in starting container process caused "exec: \"./boot.sh\": permission denied
我正在關注 Miguel Grinberg 的Flask Web Development [第二版] 。 在第三部分第 17 章中,它指導了如何將項目部署到 Docker。 我在 VMware 上使用 Ubuntu 18.04 LTS。
docker build -t flasky:latest .
成功構建了一個容器映像docker build -t flasky:latest .
.docker images
我驗證docker images
是否已成功創建。docker run --name flasky -d -p 8000:5000 \
-e SECRET_KEY=<secret_key> \
-e MAIL_USERNAME=<my_email> \
-e MAIL_PASSWORD=<my_password> flasky:latest
結果我收到這個錯誤:
docker:來自守護進程的錯誤響應:OCI 運行時創建失敗:container_linux.go:346:啟動容器進程導致“exec:\\”./boot.sh\\“:權限被拒絕”:未知。
我嘗試使用chmod
修改權限,但無濟於事。 再說一次,也許我做錯了。
Dockerfile:
FROM python:3.6-alpine
ENV FLASK_APP flasky.py
ENV FLASK_CONFIG docker
RUN adduser -D flasky
USER flasky
WORKDIR /home/flasky
COPY requirements requirements
RUN python -m venv venv
RUN venv/bin/pip install -r requirements/docker.txt
COPY app app
COPY migrations migrations
COPY flasky.py config.py boot.sh ./
# runtime configuration
EXPOSE 5000
ENTRYPOINT ["./boot.sh"]
啟動文件:
#!/bin/sh
source venv/bin/activate
flask deploy
exec gunicorn -b 0.0.0.0:5000 --access-logfile - --error-logfile - flasky:app
chmod +x boot.sh
應該可以解決您的問題。 當chmod +x
未完成時,我可以重現該問題-
root@qa9phx:~/amitp/p3# docker run -it 62591cab9f07 bash
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"./boot.sh\": permission denied": unknown.
這是我使用的示例 Dockerfile-
FROM python:3.6-alpine
COPY boot.sh ./
# runtime configuration
EXPOSE 5000
ENTRYPOINT ["./boot.sh"]
這是示例 boot.sh
#!/bin/sh
echo "hello world"
在構建 docker 之前運行以下命令
chmod +x boot.sh
然后 docker build-
docker build -t flasky:latest .
Docker 鏡像列表——
root@qa9phx:~/amitp/p3# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flasky latest 6d10284c0d9e 8 minutes ago 94.6MB
Docker 運行命令-
root@qa9phx:~/amitp/p3# docker run -it 6d10284c0d9e bash
hello world
TL;DR : chmod a+x boot.sh
或chmod o+x boot.sh
您在容器USER flasky
flasky
內以用戶flasky
運行,因此以該用戶身份執行boot.sh
腳本。 這里的問題是flasky
沒有執行腳本的權限。
比方說,你作為用戶運行app_user
組下app_group
你的主機上,並試圖提出類似的腳本執行權chmod +x boot.sh
。 它只允許用戶app_user
執行腳本。
如果您執行命令chmod g+x boot.sh
,它將允許屬於組app_group
任何用戶能夠執行它。
由於我們從未在主機中為app_user
指定任何 id,也flasky
在容器中為flasky
user 指定任何 id,因此您必須運行命令chmod a+x boot.sh
或chmod o+x boot.sh
以授予其他用戶執行boot.sh
的權限boot.sh
。
所有這些麻煩的原因是因為在 Linux 中,容器中的用戶 id 直接映射到主機中的用戶 id。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.