Docker 容器中的脚本如何在 运行 使用 CMD 时与 运行 在交互模式下给出不同的结果?
How can a script in a Docker container give a different result when run with CMD from when run in interactive mode?
我有一个安装了 GDAL 的 Docker 图像。在 Docker 文件中,最后一个命令是 CMD ["python", "script.py"]
。此 Python 脚本 运行s gdalwarp
与 subprocess.run()
.
我 运行 脚本有两种不同的方式,每种方式都有不同的输出。
- 从 "inside" 处于交互模式的容器,使用
docker run -it <image_name> bash
然后 python script.py
,这给出了预期的输出
gdalinfo
输出:
Driver: GTiff/GeoTIFF
Files: file.tif
file.XML
Size is 19771, 19771
Coordinate System is:
GEOGCRS["WGS 84",
DATUM["World Geodetic System 1984",
ELLIPSOID["WGS 84",6378137,298.257223563,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
CS[ellipsoidal,2],
AXIS["geodetic latitude (Lat)",north,
ORDER[1],
ANGLEUNIT["degree",0.0174532925199433]],
AXIS["geodetic longitude (Lon)",east,
ORDER[2],
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4326]]
Data axis to CRS axis mapping: 2,1
Origin = (16.295439814814817,48.338634259259265)
Pixel Size = (0.000004629629630,-0.000004629629630)
Metadata:
AREA_OR_POINT=Area
METADATATYPE=DIMAP
Image Structure Metadata:
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 16.2954398, 48.3386343) ( 16d17'43.58"E, 48d20'19.08"N)
Lower Left ( 16.2954398, 48.2471019) ( 16d17'43.58"E, 48d14'49.57"N)
Upper Right ( 16.3869722, 48.3386343) ( 16d23'13.10"E, 48d20'19.08"N)
Lower Right ( 16.3869722, 48.2471019) ( 16d23'13.10"E, 48d14'49.57"N)
Center ( 16.3412060, 48.2928681) ( 16d20'28.34"E, 48d17'34.32"N)
Band 1 Block=19771x1 Type=UInt16, ColorInterp=Red
Band 2 Block=19771x1 Type=UInt16, ColorInterp=Green
Band 3 Block=19771x1 Type=UInt16, ColorInterp=Blue
Band 4 Block=19771x1 Type=UInt16, ColorInterp=Undefined
- 经典地使用定义的
CMD
: docker run <image_name>
,它给出了不同的输出,GDAL 似乎丢失了坐标系信息及其顺序
gdalinfo
输出:
Driver: GTiff/GeoTIFF
Files: file.tif
file.XML
Size is 19771, 19771
Origin = (48.247101851844533,16.386972222229545)
Pixel Size = (0.000004629629630,-0.000004629629630)
Metadata:
METADATATYPE=DIMAP
Image Structure Metadata:
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 48.2471019, 16.3869722)
Lower Left ( 48.2471019, 16.2954398)
Upper Right ( 48.3386343, 16.3869722)
Lower Right ( 48.3386343, 16.2954398)
Center ( 48.2928681, 16.3412060)
Band 1 Block=19771x1 Type=UInt16, ColorInterp=Red
Band 2 Block=19771x1 Type=UInt16, ColorInterp=Green
Band 3 Block=19771x1 Type=UInt16, ColorInterp=Blue
Band 4 Block=19771x1 Type=UInt16, ColorInterp=Undefined
我很沮丧,因为脚本和环境在这两种情况下完全相同。当我 运行 来自容器内部的脚本时,python script.py
是我 运行.
的第一个也是唯一一个命令
什么会导致这种行为?
编辑:
我还尝试了运行通过以下方式连接容器,但都给出了错误的输出:
docker run <image_name> python script.py
docker run -it <image_name> python script.py
docker run <image_name> bash -c "python script.py"
docker run -it <image_name> bash -c "python script.py"
docker run <image_name>
与 CMD ["bash", "-c", "python script.py"]
问题来自缺少环境变量。
详情见this answer on ServerFault。
我的解决方案是更改 Dockerfile 中的 ENTRYPOINT(注意 -l
选项):
ENTRYPOINT ["/bin/bash", "-l", "-c"]
我有一个安装了 GDAL 的 Docker 图像。在 Docker 文件中,最后一个命令是 CMD ["python", "script.py"]
。此 Python 脚本 运行s gdalwarp
与 subprocess.run()
.
我 运行 脚本有两种不同的方式,每种方式都有不同的输出。
- 从 "inside" 处于交互模式的容器,使用
docker run -it <image_name> bash
然后python script.py
,这给出了预期的输出
gdalinfo
输出:
Driver: GTiff/GeoTIFF
Files: file.tif
file.XML
Size is 19771, 19771
Coordinate System is:
GEOGCRS["WGS 84",
DATUM["World Geodetic System 1984",
ELLIPSOID["WGS 84",6378137,298.257223563,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
CS[ellipsoidal,2],
AXIS["geodetic latitude (Lat)",north,
ORDER[1],
ANGLEUNIT["degree",0.0174532925199433]],
AXIS["geodetic longitude (Lon)",east,
ORDER[2],
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4326]]
Data axis to CRS axis mapping: 2,1
Origin = (16.295439814814817,48.338634259259265)
Pixel Size = (0.000004629629630,-0.000004629629630)
Metadata:
AREA_OR_POINT=Area
METADATATYPE=DIMAP
Image Structure Metadata:
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 16.2954398, 48.3386343) ( 16d17'43.58"E, 48d20'19.08"N)
Lower Left ( 16.2954398, 48.2471019) ( 16d17'43.58"E, 48d14'49.57"N)
Upper Right ( 16.3869722, 48.3386343) ( 16d23'13.10"E, 48d20'19.08"N)
Lower Right ( 16.3869722, 48.2471019) ( 16d23'13.10"E, 48d14'49.57"N)
Center ( 16.3412060, 48.2928681) ( 16d20'28.34"E, 48d17'34.32"N)
Band 1 Block=19771x1 Type=UInt16, ColorInterp=Red
Band 2 Block=19771x1 Type=UInt16, ColorInterp=Green
Band 3 Block=19771x1 Type=UInt16, ColorInterp=Blue
Band 4 Block=19771x1 Type=UInt16, ColorInterp=Undefined
- 经典地使用定义的
CMD
:docker run <image_name>
,它给出了不同的输出,GDAL 似乎丢失了坐标系信息及其顺序
gdalinfo
输出:
Driver: GTiff/GeoTIFF
Files: file.tif
file.XML
Size is 19771, 19771
Origin = (48.247101851844533,16.386972222229545)
Pixel Size = (0.000004629629630,-0.000004629629630)
Metadata:
METADATATYPE=DIMAP
Image Structure Metadata:
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 48.2471019, 16.3869722)
Lower Left ( 48.2471019, 16.2954398)
Upper Right ( 48.3386343, 16.3869722)
Lower Right ( 48.3386343, 16.2954398)
Center ( 48.2928681, 16.3412060)
Band 1 Block=19771x1 Type=UInt16, ColorInterp=Red
Band 2 Block=19771x1 Type=UInt16, ColorInterp=Green
Band 3 Block=19771x1 Type=UInt16, ColorInterp=Blue
Band 4 Block=19771x1 Type=UInt16, ColorInterp=Undefined
我很沮丧,因为脚本和环境在这两种情况下完全相同。当我 运行 来自容器内部的脚本时,python script.py
是我 运行.
什么会导致这种行为?
编辑:
我还尝试了运行通过以下方式连接容器,但都给出了错误的输出:
docker run <image_name> python script.py
docker run -it <image_name> python script.py
docker run <image_name> bash -c "python script.py"
docker run -it <image_name> bash -c "python script.py"
docker run <image_name>
与CMD ["bash", "-c", "python script.py"]
问题来自缺少环境变量。
详情见this answer on ServerFault。
我的解决方案是更改 Dockerfile 中的 ENTRYPOINT(注意 -l
选项):
ENTRYPOINT ["/bin/bash", "-l", "-c"]