docker python Flask 得到 "要继续吗?"然后“执行程序失败" [英] docker python flask gets " Do you want to continue" then "executor failed"
问题描述
在 docker 20.10.2 版本 2291f61 中尝试一个简单的 python flask web 应用程序.
Trying a simple python flask web app in docker 20.10.2, build 2291f61.
执行以下操作:docker build -t hello-world .
开始没问题(请参见下文),但以:
Doing the: docker build -t hello-world .
starts out ok (please see below), but ends with:
#6 2.866 After this operation, 305 MB of additional disk space will be used.
#6 2.866 Do you want to continue? [Y/n] Abort.
------
executor failed running [/bin/sh -c apt-get install python3-pip]: exit code: 1
我是否需要以某种方式回答这个问题?还有其他解决方法吗?
Do I need to answer the question with a yes somehow? Is there some other workarond?
编辑 1: $ docker build -t flask-tutorial:latest .# 做同样的事情.
Edit 1: $ docker build -t flask-tutorial:latest . # does the same thing.
编辑 2:使用来自@tentative 的 install -y 想法可以进一步获得 lot.
Edit 2: Using the install -y idea from @tentative gets a lot further.
> [4/6] RUN apt-get install -y flask:
#7 0.742 Reading package lists...
#7 2.103 Building dependency tree...
#7 2.389 Reading state information...
#7 2.665 E: Unable to locate package flask
------
executor failed running [/bin/sh -c apt-get install -y flask]: ex
it code: 100
代码这里 构建图像.
编辑 4:Dockerfile
Edit 4: Dockerfile
FROM ubuntu
RUN apt-get update
RUN apt-get install -y python3-pip
# add -y to skip the y/n prompt and install
#RUN apt-get install -y python3-pip flask
RUN apt-get install -y flask
ADD app.py /
WORKDIR /
EXPOSE 5000
CMD ["python3","app.py"]
编辑 5:新的 docker 文件构建有效.但是:
Edit 5: The new docker file build works. but:
$ docker run -i -t --name frog hello-world
/bin/sh: 1: ["python3","app.py"]: not found
编辑 6:将上面的开闭引号更改为中性引号.从主机运行时,应用程序运行良好.容器得到一个:27.0.0.1 拒绝连接.
Edit 6: changed the open and closed quotes above to neutral quotes. app runs fine when run from host. container get a: 27.0.0.1 refused to connect.
编辑 7:尝试:
docker run -it -p 5000:5000 --name frog hello-world .
这会导致连接被拒绝(firefox),一个空响应(chrome、edge 和 opera).这是在 Windows 10 上,防御者关闭.
This gets a connection refused (firefox), an empty response (chrome, edge, and opera). This is on windows 10 with the defender turned off.
编辑 8:做一个
if __name__ == '__main__':
app.run(host='0.0.0.0')
然后一个
卷曲 127.0.0.1:5000 -v
curl 127.0.0.1:5000 -v
从服务器得到一个空回复.
gets an empty reply from server.
编辑 9:仅使用:app.run(0.0.0.0"有效!
Edit 9: using just: app.run("0.0.0.0" works!
原始输出.
ray@rays8350 /cygdrive/d/ray/docker/flask/myapp
$ docker build -t hello-world .
[+] Building 4.9s (7/9)
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 32B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:latest 1.6s
=> [1/6] FROM docker.io/library/ubuntu@sha256:703218c0465075f4425e58fac0 0.0s
=> => resolve docker.io/library/ubuntu@sha256:703218c0465075f4425e58fac0 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 28B 0.0s
=> CACHED [2/6] RUN apt-get update 0.0s
=> ERROR [3/6] RUN apt-get install python3-pip 3.2s
------
> [3/6] RUN apt-get install python3-pip:
#6 0.588 Reading package lists...
#6 1.927 Building dependency tree...
#6 2.258 Reading state information...
#6 2.683 The following additional packages will be installed:
#6 2.683 binutils binutils-common binutils-x86-64-linux-gnu build-essential
#6 2.683 ca-certificates cpp cpp-9 dirmngr dpkg-dev fakeroot file g++ g++-9 g
c gcc-9
#6 2.683 gcc-9-base gnupg gnupg-l10n gnupg-utils gpg gpg-agent gpg-wks-client
#6 2.683 gpg-wks-server gpgconf gpgsm gpgv libalgorithm-diff-perl
#6 2.683 libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan5 libasn1-8
heimdal
#6 2.683 libassuan0 libatomic1 libbinutils libc-dev-bin libc6 libc6-dev libcc
-0
#6 2.684 libcrypt-dev libctf-nobfd0 libctf0 libdpkg-perl libexpat1 libexpat1-
ev
#6 2.684 libfakeroot libfile-fcntllock-perl libgcc-9-dev libgdbm-compat4 libg
bm6
#6 2.684 libgomp1 libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal
#6 2.684 libheimntlm0-heimdal libhx509-5-heimdal libisl22 libitm1 libkrb5-26-
eimdal
#6 2.684 libksba8 libldap-2.4-2 libldap-common liblocale-gettext-perl liblsan
#6 2.684 libmagic-mgc libmagic1 libmpc3 libmpdec2 libmpfr6 libnpth0 libperl5.
0
#6 2.684 libpython3-dev libpython3-stdlib libpython3.8 libpython3.8-dev
#6 2.684 libpython3.8-minimal libpython3.8-stdlib libquadmath0 libreadline8
#6 2.685 libroken18-heimdal libsasl2-2 libsasl2-modules libsasl2-modules-db
#6 2.685 libsqlite3-0 libssl1.1 libstdc++-9-dev libtsan0 libubsan1 libwind0-h
imdal
#6 2.686 linux-libc-dev make manpages manpages-dev mime-support netbase opens
l patch
#6 2.686 perl perl-modules-5.30 pinentry-curses python-pip-whl python3 python
-dev
#6 2.686 python3-distutils python3-lib2to3 python3-minimal python3-pkg-resour
es
#6 2.686 python3-setuptools python3-wheel python3.8 python3.8-dev python3.8-m
nimal
#6 2.687 readline-common xz-utils zlib1g-dev
#6 2.690 Suggested packages:
#6 2.690 binutils-doc cpp-doc gcc-9-locales dbus-user-session libpam-systemd
#6 2.690 pinentry-gnome3 tor debian-keyring g++-multilib g++-9-multilib gcc-9
doc
#6 2.690 gcc-multilib autoconf automake libtool flex bison gdb gcc-doc gcc-9-
ultilib
#6 2.690 parcimonie xloadimage scdaemon glibc-doc locales git bzr gdbm-l10n
#6 2.690 libsasl2-modules-gssapi-mit | libsasl2-modules-gssapi-heimdal
#6 2.690 libsasl2-modules-ldap libsasl2-modules-otp libsasl2-modules-sql
#6 2.690 libstdc++-9-doc make-doc man-browser ed diffutils-doc perl-doc
#6 2.690 libterm-readline-gnu-perl | libterm-readline-perl-perl libb-debug-pe
l
#6 2.690 liblocale-codes-perl pinentry-doc python3-doc python3-tk python3-ven
#6 2.690 python-setuptools-doc python3.8-venv python3.8-doc binfmt-support
#6 2.690 readline-doc
#6 3.070 The following NEW packages will be installed:
#6 3.070 binutils binutils-common binutils-x86-64-linux-gnu build-essential
#6 3.070 ca-certificates cpp cpp-9 dirmngr dpkg-dev fakeroot file g++ g++-9 g
c gcc-9
#6 3.070 gcc-9-base gnupg gnupg-l10n gnupg-utils gpg gpg-agent gpg-wks-client
#6 3.071 gpg-wks-server gpgconf gpgsm libalgorithm-diff-perl
#6 3.071 libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan5 libasn1-8
heimdal
#6 3.072 libassuan0 libatomic1 libbinutils libc-dev-bin libc6-dev libcc1-0
#6 3.072 libcrypt-dev libctf-nobfd0 libctf0 libdpkg-perl libexpat1 libexpat1-
ev
#6 3.073 libfakeroot libfile-fcntllock-perl libgcc-9-dev libgdbm-compat4 libg
bm6
#6 3.073 libgomp1 libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal
#6 3.073 libheimntlm0-heimdal libhx509-5-heimdal libisl22 libitm1 libkrb5-26-
eimdal
#6 3.073 libksba8 libldap-2.4-2 libldap-common liblocale-gettext-perl liblsan
#6 3.074 libmagic-mgc libmagic1 libmpc3 libmpdec2 libmpfr6 libnpth0 libperl5.
0
#6 3.075 libpython3-dev libpython3-stdlib libpython3.8 libpython3.8-dev
#6 3.075 libpython3.8-minimal libpython3.8-stdlib libquadmath0 libreadline8
#6 3.077 libroken18-heimdal libsasl2-2 libsasl2-modules libsasl2-modules-db
#6 3.077 libsqlite3-0 libssl1.1 libstdc++-9-dev libtsan0 libubsan1 libwind0-h
imdal
#6 3.077 linux-libc-dev make manpages manpages-dev mime-support netbase opens
l patch
#6 3.078 perl perl-modules-5.30 pinentry-curses python-pip-whl python3 python
-dev
#6 3.078 python3-distutils python3-lib2to3 python3-minimal python3-pip
#6 3.079 python3-pkg-resources python3-setuptools python3-wheel python3.8
#6 3.080 python3.8-dev python3.8-minimal readline-common xz-utils zlib1g-dev
#6 3.083 The following packages will be upgraded:
#6 3.086 gpgv libc6
#6 3.106 2 upgraded, 113 newly installed, 0 to remove and 4 not upgraded.
#6 3.106 Need to get 72.4 MB of archives.
#6 3.106 After this operation, 305 MB of additional disk space will be used.
#6 3.106 Do you want to continue? [Y/n] Abort.
------
executor failed running [/bin/sh -c apt-get install python3-pip]: exit code: 1
ray@rays8350 /cygdrive/d/ray/docker/flask/myapp
$
推荐答案
您应该使用 apt-get
更改 Dockerfile 中的 RUN
行以使用 -y
标志跳过要求您确认是".
You should change the RUN
lines in the Dockerfile with apt-get
in them to use the -y
flag to skip asking you to confirm "yes".
教程中的 Dockerfile 实际上似乎已经过时或存在错误.您现在需要使用 pip3
来安装 Flask
并在您的 apt-get
命令中包含 -y
标志.我从教程中编辑了 Dockerfile 并在下面发布:
The Dockerfile in the tutorial actually seems out of date or has errors in it. You need to use pip3
now to install Flask
and also include the -y
flag in your apt-get
commands. I edited the Dockerfile from the tutorial and posted below:
FROM ubuntu
RUN apt-get update -y && \
apt-get install -y python3-pip && \
pip3 install Flask
COPY app.py /
WORKDIR /
EXPOSE 5000
CMD ["python3","app.py"]
编辑要通过此答案修复 Windows 上的连接问题,您可以修复此问题通过从以下位置更改 Python 应用程序中的 main 函数:
EDIT To fix the connection issues on Windows from this answer, you can fix this by changing the main function in the Python app from:
if __name__ == '__main__':
app.run()
为此:
if __name__ == '__main__':
app.run('0.0.0.0')
原因是需要Flask绑定到0.0.0.0
而不是容器内的localhost接口,否则不会转发到容器外.修复此问题后,您应该可以在主机上的 localhost:5000
上访问该应用程序.
The reason is that you need Flask to bind to 0.0.0.0
instead of the localhost interface in the container, otherwise, it won't forward outside the container. After fixing that, you should be able to access the app at localhost:5000
on your host machine.
这篇关于docker python Flask 得到 "要继续吗?"然后“执行程序失败"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!