Docker在CentOS上运行

n1bvdmb6  于 2023-10-18  发布在  Docker
关注(0)|答案(1)|浏览(189)

我试图运行脚本(myScript.R)如下从CentOS上的图像:

print("AAS")

Dockerfile如下:

FROM rocker/r-base:latest

COPY /myScript.R  /myScript.R

RUN Rscript /myScript.R

在Windows上它可以正常运行,但在CentOS命令docker run myName/17_08返回错误:

Fatal error: you must specify '--save', '--no-save' or '--vanilla'

当我使用选项时,它返回错误:

$ docker run myName/17_08 --no-save
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "--no-save": executable file not found in $PATH: unknown.
ERRO\[0000\] error waiting for container:

我可以做什么来在CentOS上运行这个容器?

8ehkhllq

8ehkhllq1#

简介

坦率地说,我不知道这在Windows上是如何工作的,因为RUN是构建时的指令。在本例中,当执行docker run时,将执行基础图像中的CMD,如代码中所示,它是[R]

构建时执行

如果你想在构建时运行你的脚本,请坚持运行指令(确保文件路径有效),它会看起来像这样:

$ docker build --progress=plain --no-cache -t tester:latest .
#0 building with "default" instance using docker driver

#1 [internal] load .dockerignore
#1 transferring context:
#1 transferring context: 2B done
#1 DONE 0.2s

#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 108B done
#2 DONE 0.2s

#3 [internal] load metadata for docker.io/rocker/r-base:latest
#3 DONE 0.5s

#4 [1/3] FROM docker.io/rocker/r-base:latest@sha256:eb220139e120247ff6208e2c2556c5142250c7de02a5ea4c77151cbfdffd186e
#4 CACHED

#5 [internal] load build context
#5 transferring context: 31B done
#5 DONE 0.1s

#6 [2/3] COPY myScript.R .
#6 DONE 0.3s

#7 [3/3] RUN  Rscript myScript.R
#7 1.153 [1] "AAS"
#7 DONE 1.4s

#8 exporting to image
#8 exporting layers
#8 exporting layers 0.4s done
#8 writing image sha256:904e0a8893b593c2c73423bb7b9705c63135172fac909789aea735b0c121139d 0.0s done
#8 naming to docker.io/library/tester:latest 0.1s done
#8 DONE 0.5s

你可以看到你的脚本在第7层运行。

运行时执行脚本

如果您想在运行时运行脚本,基本上有三种不同的选择:使用CMDENTRYPOINT或将命令添加到docker run指令。要理解这些差异,stackoverflow上有一个很好的Answer

CMD

要做到这一点,你需要像这样调整你的Dockerfile:

FROM rocker/r-base:latest

COPY myScript.R .

CMD  Rscript myScript.R

您可以看到脚本不再在构建时执行:

$ docker build --progress=plain --no-cache -t tester:latest .
#0 building with "default" instance using docker driver

#1 [internal] load .dockerignore
#1 transferring context: 2B done
#1 DONE 0.2s

#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 108B done
#2 DONE 0.2s

#3 [internal] load metadata for docker.io/rocker/r-base:latest
#3 DONE 0.9s

#4 [1/2] FROM docker.io/rocker/r-base:latest@sha256:eb220139e120247ff6208e2c2556c5142250c7de02a5ea4c77151cbfdffd186e
#4 CACHED

#5 [internal] load build context
#5 transferring context: 31B done
#5 DONE 0.1s

#6 [2/2] COPY myScript.R .
#6 DONE 0.4s

#7 exporting to image
#7 exporting layers
#7 exporting layers 0.1s done
#7 writing image sha256:1a5f360b2406fd7617aa56b4a4ac91261672cb0fe5760b1c2dc87776556cd334 0.0s done
#7 naming to docker.io/library/tester:latest 0.0s done
#7 DONE 0.2s

所以现在你可以运行你的镜像,它会用你指定的命令执行脚本:

$ docker run --rm tester
[1] "AAS"

使用ENTRYPOINT执行

与上面类似,您可以在图像中使用ENTRYPOINT指令。
你的Dockerfile看起来像这样:

FROM rocker/r-base:latest

COPY myScript.R .

ENTRYPOINT  Rscript myScript.R

并且在不执行脚本的情况下,生成的输出也是:

$ docker build --progress=plain --no-cache -t tester:latest .
#0 building with "default" instance using docker driver

#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 115B done
#1 DONE 0.2s

#2 [internal] load .dockerignore
#2 transferring context: 2B done
#2 DONE 0.3s

#3 [internal] load metadata for docker.io/rocker/r-base:latest
#3 DONE 0.6s

#4 [1/2] FROM docker.io/rocker/r-base:latest@sha256:eb220139e120247ff6208e2c2556c5142250c7de02a5ea4c77151cbfdffd186e
#4 CACHED

#5 [internal] load build context
#5 transferring context: 31B done
#5 DONE 0.1s

#6 [2/2] COPY myScript.R .
#6 DONE 0.4s

#7 exporting to image
#7 exporting layers
#7 exporting layers 0.1s done
#7 writing image sha256:00a6237bc2ce599c55175db0a1cb3753cf32be092499f22959b2b91cbf05554b 0.0s done
#7 naming to docker.io/library/tester:latest 0.0s done
#7 DONE 0.2s

然后在运行时,您将再次收到预期的输出:

$ docker run --rm tester
[1] "AAS"

通过docker run执行

最后但并非最不重要的是,你可以把你的形象缩小到这个程度:

FROM rocker/r-base:latest

COPY myScript.R .

这使得构建过程更短:

$ docker build --progress=plain --no-cache -t tester:latest .
#0 building with "default" instance using docker driver

#1 [internal] load .dockerignore
#1 transferring context: 2B done
#1 DONE 0.2s

#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 83B done
#2 DONE 0.2s

#3 [internal] load metadata for docker.io/rocker/r-base:latest
#3 DONE 0.5s

#4 [1/2] FROM docker.io/rocker/r-base:latest@sha256:eb220139e120247ff6208e2c2556c5142250c7de02a5ea4c77151cbfdffd186e
#4 CACHED

#5 [internal] load build context
#5 transferring context: 31B done
#5 DONE 0.1s

#6 [2/2] COPY myScript.R .
#6 DONE 0.3s

#7 exporting to image
#7 exporting layers
#7 exporting layers 0.2s done
#7 writing image sha256:cb724210554cce6c9ef0fc93e4bdef5b39c1782527ba21c28f5ec13e5850a637 0.0s done
#7 naming to docker.io/library/tester:latest 0.0s done
#7 DONE 0.2s

现在,当你运行容器时,传递参数:

$ docker run --rm tester Rscript myScript.R
[1] "AAS"

相关问题