简体   繁体   English

Docker exec 无法在容器内执行脚本

[英]Docker exec cannot execute script inside container

I have bash script that performing some Docker commands:我有执行一些 Docker 命令的 bash 脚本:

#!/usr/bin/env bash

echo "Create and start database"
cd ../../database
cp -R ../../../scripts/db db/
docker build -t a_database:1 .
docker run --rm --name a_db -e POSTGRES_PASSWORD=docker -d -p 5432:5432 a_database:1
docker network connect --ip 172.23.0.5 a_network a_db
sleep 15

echo "Initialize database"
docker exec a_db /root/db/dev/init_db.sh

echo "Cleanup"
rm -rf db

On mac everything works fine, problem occurs when I try to start this script on windows machine.在 mac 上一切正常,当我尝试在 windows 机器上启动这个脚本时出现问题。 When I'm running it I receive an error:当我运行它时,我收到一个错误:

OCI runtime exec failed: exec failed: container_linux.go:344: starting container process caused "exec: \"C:/Program Files/Git/root/db/dev/init_db.sh\": stat C:/Program Files/Git/root/db/dev/init_db.sh: no such file or directory": unknown

Directory and script (/root/db/dev/init_db.sh) exist inside docker container. docker 容器中存在目录和脚本(/root/db/dev/init_db.sh)。 I don't know why it tries to find script on host machine?我不知道为什么它试图在主机上查找脚本? Also when I perform command:同样,当我执行命令时:

docker exec a_db /root/db/dev/init_db.sh

directly in command line (on windows) script is executed.直接在命令行(在 Windows 上)脚本中执行。 Any idea what is wrong and why it's trying to use git ?知道出了什么问题以及为什么要尝试使用 git 吗?

我有一个类似的问题...... Windows 变量的绝对路径修复了我的: $HOME/docker/...

Thanks to igaul answer I was able to run this on windows machine.感谢 igaul 的回答,我能够在 Windows 机器上运行它。 There were two problems:有两个问题:

  1. Path to script in docker container. docker 容器中脚本的路径。
    Instead of:代替:

     docker exec a_db /root/db/dev/init_db.sh

    should be:应该:

     docker exec a_db root/db/dev/init_db.sh
  2. Line endings in init_db.sh. init_db.sh 中的行结尾。 On windows machine after pulling repository from bitbucket line ending of init_db.sh was setup to CRLF what caused problem.在从 init_db.sh 的 bitbucket 行结尾拉出存储库后,在 Windows 机器上设置为 CRLF 导致问题的原因。 I've added .gitattribute file to my repo and now init_db.sh file always has LF endings.我已经将 .gitattribute 文件添加到我的仓库中,现在 init_db.sh 文件总是以 LF 结尾。

It's not a bug in Docker, but the way mingw handles these paths.这不是 Docker 中的错误,而是 mingw 处理这些路径的方式。 Here is some more information about that "feature";这是有关该“功能”的更多信息; http://www.mingw.org/wiki/Posix_path_conversion . http://www.mingw.org/wiki/Posix_path_conversion Prefixing the path with a double slash (//bin/bash) should prevent this, or you can set MSYS_NO_PATHCONV=1, see How to stop MinGW and MSYS from mangling path names given at the command line用双斜杠 (//bin/bash) 前缀路径应该可以防止这种情况,或者您可以设置 MSYS_NO_PATHCONV=1,请参阅如何阻止 MinGW 和 MSYS 修改命令行中给出的路径名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM