尝试用Powershell和pip构建一个映像,然后运行一个Powershell脚本,调用其中的python包:
> docker build --file gallery-dl.dockerfile --tag psu .
[+] Building 0.6s (9/9) FINISHED
=> [internal] load build definition from gallery-dl.dockerfile 0.0s
=> => transferring dockerfile: 413B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for mcr.microsoft.com/powershell:7.3.0-preview.3-ubunt 0.2s
=> [internal] load build context 0.0s
=> => transferring context: 36B 0.0s
=> [1/4] FROM mcr.microsoft.com/powershell:7.3.0-preview.3-ubuntu-focal-20220318@s 0.0s
=> CACHED [2/4] RUN apt-get update && apt-get -qq -y install curl ca-certifica 0.0s
=> CACHED [3/4] RUN pip3 install https://github.com/mikf/gallery-dl/archive/master 0.0s
=> [4/4] COPY gallery-dl.ps1 /mydir/ 0.1s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:6bd7be9979190bf2993e5473265284883b0c154c1e62f5bb27d74c0 0.0s
=> => naming to docker.io/library/psu 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
但是得到以下错误:
> docker run -it --rm --name psutest psu
standard_init_linux.go:228: exec user process caused: exec format error
我Dockerfile:
FROM mcr.microsoft.com/powershell:7.3.0-preview.3-ubuntu-focal-20220318
RUN apt-get update &&
apt-get -qq -y install curl ca-certificates python3-pip
RUN pip3 install https://github.com/mikf/gallery-dl/archive/master.zip https://github.com/yt-dlp/yt-dlp/archive/master.zip
COPY gallery-dl.ps1 /mydir/
ENTRYPOINT ["/mydir/gallery-dl.ps1"]
我做错了什么?当直接在我的计算机上运行时,脚本运行得很好。
您将在Linux映像(基于Ubuntu 20.04 "Focal Fossa")中运行此程序,其中默认shell通常是Bourne shell的某种风格。你需要以某种方式告诉Linux,它需要使用Powershell来运行脚本。
最好的方法是在脚本开始时使用"shebang"线。这看起来就像
#!/usr/bin/pwsh -File
每当脚本被标记为可执行(chmod +x gallery-dl.ps1
)时,Linux(和任何其他Unix)将找到shebang行并运行该命令,将脚本名称和任何其他参数传递给它。这必须从文件的绝对第一个字节开始-在它之前没有注释或换行符或其他任何东西-这是DOS行结束会导致问题的地方。因为这一行也是一个Powershell注释,所以它在运行时不会对脚本产生任何影响。
你也可以把解释器放在Docker镜像的命令中。这在某种程度上是在重复自己,如果有时需要运行备用脚本作为主容器进程,则必须为每个备用脚本重复pwsh
解释器。
# instead of the ENTRYPOINT line you currently have
CMD ["/usr/bin/pwsh", "-File", "/mydir/gallery-dl.ps1"]
这里有多个答案,但大多数都是指x64架构。请找到链接:standard_init_linux。Go:178: exec用户进程导致"exec格式错误">