docker python flask获取“是否要继续”,然后获取“执行器失败”

2024-05-15 05:39:31 发布

您现在位置:Python中文网/ 问答频道 /正文

在docker 20.10.2中尝试一个简单的python flask web app,构建2291f61

执行:docker build -t hello-world .开始时是ok(请参见下文),但结束时是:

#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

我是否需要以肯定的方式回答这个问题?还有其他解决办法吗

编辑1:$docker build-t flask教程:最新版本做同样的事情

编辑2:使用@Tential中的install-y思想可以进一步获得

 > [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

编辑3:代码here构建一个图像

编辑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文件构建工作正常。但是:

$ docker run -i -t --name frog hello-world
/bin/sh: 1: [“python3”,”app.py”]: not found

编辑6:将上面的打开和关闭引号更改为中性引号。从主机运行时,应用程序运行正常。容器获取a:27.0.0.1拒绝连接

编辑7:尝试:

docker run -it -p 5000:5000 --name frog hello-world . 

这会得到一个连接被拒绝(firefox),一个空响应(chrome、edge和opera)。这是在windows 10上关闭defender的情况下进行的

编辑8:做一个

if __name__ == '__main__':
    app.run(host='0.0.0.0')

然后

旋度127.0.0.1:5000-v

从服务器获取空答复

编辑9:仅使用:app.run(“0.0.0.0”有效

原始输出

   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
    $

Tags: installpipdockerdevmodules编辑flaskget
1条回答
网友
1楼 · 发布于 2024-05-15 05:39:31

您应该更改Dockerfile中带有apt-getRUN行,以使用-y标志跳过要求您确认“是”的请求

教程中的Dockerfile实际上似乎已过时或有错误。您需要立即使用pip3来安装Flask,并在apt-get命令中包含-y标志。我编辑了教程中的Dockerfile,并发布在下面:

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"]

编辑要从this answer修复Windows上的连接问题,可以通过将Python应用程序中的主函数从以下位置更改来修复此问题:

if __name__ == '__main__':
    app.run()

为此:

if __name__ == '__main__':
    app.run('0.0.0.0')

原因是您需要将Flask绑定到0.0.0.0而不是容器中的localhost接口,否则,它不会在容器外转发。修复后,您应该能够访问主机上位于localhost:5000的应用程序

相关问题 更多 >

    热门问题