简体   繁体   English

退出bash shell脚本后,“ docker run”死亡

[英]“docker run” dies after exiting a bash shell script

I'm attempting to craft system admin bash tools for starting up a Docker image. 我正在尝试使用系统管理bash工具来启动Docker映像。

But such docker run keeps dying on me after its bash script exited. 但是在退出其bash脚本后,此类docker run仍然使我丧命。

The actual working bash script in question is: 有问题的实际bash脚本是:

#!/bin/sh

docker run \
    --name publicnginx1 \
    -v /var/www:/usr/share/nginx/html:ro \
    -v /var/nginx/conf:/etc/nginx:ro \
    --rm \
    -p 80 \
    -p 443 \
    -d \
    nginx
docker ps

Executing the simple script resulted in: 执行简单的脚本导致:

# ./docker-run-nginx.sh 
743a6eaa33f435e3e0d211c4047bc9af4d4667dc31cd249e481850f40f848c83
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS                                           NAMES
743a6eaa33f4        nginx               "nginx -g 'daemon of…"   1 second ago        Up Less than a second   0.0.0.0:32778->80/tcp, 0.0.0.0:32777->443/tcp   publicnginx1

And after that bash script gets completed, I executed 'docker ps' 在bash脚本完成后,我执行了'docker ps'

# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

There is no Docker running. 没有Docker在运行。

What did I do wrong? 我做错了什么?

Try to run it without --rm. 尝试在没有--rm的情况下运行它。

You can see all container (including the one that already died using this command): 您可以看到所有容器(包括使用此命令已经死亡的容器):

> docker ps -a

CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS                     
743a6eaa33f4        nginx   "nginx -g 'daemon of…"   1 second ago         Exited (??) ??                         
                                                                          ^^^^^

You should be able to look at what is the exit code of the container. 您应该可以查看什么是容器的退出代码。 Using the container id, you can also look into it's log to understand better what is going on: 使用容器ID,您还可以查看其日志以更好地了解发生了什么:

docker logs 743a6eaa33f4

If you still can't figure it out, you can start the container with tty to run bash, and try to run the command inside it. 如果仍然不能解决问题,可以使用tty启动容器以运行bash,然后尝试在其中运行命令。

docker run -it -v /var/www:/usr/share/nginx/html:ro -v /var/nginx/conf:/etc/nginx:ro --rm -p 80 -p 443 nginx bash

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

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