How do I run a command on an already existing Dock

2020-01-23 13:27发布

I created a container with -d so it's not interactive.

docker run -d shykes/pybuilder bin/bash

I see that the container has exited:

CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS                      PORTS               NAMES
d6c45e8cc5f0        shykes/pybuilder:latest   "bin/bash"          41 minutes ago      Exited (0) 2 seconds ago                        clever_bardeen

Now I would like to run occasional commands on the machine and exit. Just to get the response.

I tried to start the machine. I tried attaching. I thought I could call run with a container, but that does not seem to be allowed. Using start just seems to run and then exist quickly.

I'd like to get back into interactive mode after exiting.

I tried:

docker attach d6c45e8cc5f0

But I get:

2014/10/01 22:33:34 You cannot attach to a stopped container, start it first

But if I start it, it exits anyway. Catch 22. I can't win.

标签: docker
17条回答
smile是对你的礼貌
2楼-- · 2020-01-23 13:53

Pipe a command to stdin

Must remove the -t for it to work:

echo 'touch myfile' | sudo docker exec -i CONTAINER_NAME bash

This can be more convenient that using CLI options sometimes.

Tested with:

sudo docker run --name ub16 -it ubuntu:16.04 bash

then on another shell:

echo 'touch myfile' | sudo docker exec -i ub16 bash

Then on first shell:

ls -l myfile

Tested on Docker 1.13.1, Ubuntu 16.04 host.

查看更多
来,给爷笑一个
3楼-- · 2020-01-23 13:53

For Mac:

$ docker exec -it <container-name> sh

if you want to connect as root user:

$ docker exec -u 0 -it <container-name> sh
查看更多
一纸荒年 Trace。
4楼-- · 2020-01-23 13:54

I had to use bash -c to run my command: docker exec -it CONTAINER_ID bash -c "mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql"

查看更多
仙女界的扛把子
5楼-- · 2020-01-23 13:56

Assuming the image is using the default entrypoint /bin/sh -c, running /bin/bash will exit immediately in daemon mode (-d). If you want this container to run an interactive shell, use -it instead of -d. If you want to execute arbitrary commands in a container usually executing another process, you might want to try nsenter or nsinit. Have a look at https://blog.codecentric.de/en/2014/07/enter-docker-container/ for the details.

查看更多
女痞
6楼-- · 2020-01-23 13:58

Some of the answers here are misleading because they concern containers that are running, not stopped.

Sven Dowideit explained on the Docker forum that containers are bound to their process (and Docker can't change the process of a stopped container, seemingly due at least to its internal structure: https://github.com/docker/docker/issues/1437). So, basically the only option is to commit the container to an image and run it with a different command.

See https://forums.docker.com/t/run-command-in-stopped-container/343
(I believe the "ENTRYPOINT with arguments" approach wouldn't work either, since you still wouldn't be able to change the arguments to a stopped container.)

查看更多
登录 后发表回答