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条回答
迷人小祖宗
2楼-- · 2020-01-23 13:33

In October 2014 the Docker team introduced docker exec command: https://docs.docker.com/engine/reference/commandline/exec/

So now you can run any command in a running container just knowing its ID (or name):

docker exec -it <container_id_or_name> echo "Hello from container!"

Note that exec command works only on already running container. If the container is currently stopped, you need to first run it with the following command:

docker run -it -d shykes/pybuilder /bin/bash

The most important thing here is the -d option, which stands for detached. It means that the command you initially provided to the container (/bin/bash) will be run in the background and the container will not stop immediately.

查看更多
Juvenile、少年°
3楼-- · 2020-01-23 13:33

Your container will exit as the command you gave it will end. Use the following options to keep it live:

  • -i Keep STDIN open even if not attached.
  • -t Allocate a pseudo-TTY.

So your new run command is:

docker run -it -d shykes/pybuilder bin/bash

If you would like to attach to an already running container:

docker exec -it CONTAINER_ID /bin/bash

In these examples /bin/bash is used as the command.

查看更多
forever°为你锁心
4楼-- · 2020-01-23 13:34

If you are trying to run shell script, you need run it as bash.

docker exec -it containerid bash -c /path/to/your/script.sh
查看更多
\"骚年 ilove
5楼-- · 2020-01-23 13:34

I am running windows container and I need to look inside the docker container for files and folder created and copied.

In order to do that I used following docker entrypoint command to get the command prompt running inside the container or attach to the container.

ENTRYPOINT ["C:\\Windows\\System32\\cmd.exe", "-D", "FOREGROUND"]

That helped me both to the command prompt attach to container and to keep the container a live. :)

查看更多
Luminary・发光体
6楼-- · 2020-01-23 13:38

To expand on katrmr's answer, if the container is stopped and can't be started due to an error, you'll need to commit it to an image. Then you can launch bash in the new image:

docker commit [CONTAINER_ID] temporary_image
docker run --entrypoint=bash -it temporary_image
查看更多
Emotional °昔
7楼-- · 2020-01-23 13:39

Simple answer: start and attach at the same time. In this case you are doing exactly what you asked for.

docker start <CONTAINER_ID/CONTAINER_NAME> && docker attach <CONTAINER_ID/CONTAINER_NAME> 

make sure to change <CONTAINER_ID/CONTAINER_NAME>

查看更多
登录 后发表回答