Execute commands in docker run while running docker in the background

Viewed 246

I'm trying to run a docker container as a background task, and also execute a few commands inside it. When I run:

docker run -d --rm --ipc host --net host \
              --privileged --name test5 bash -c 'echo "hi"' docker/getting-started

docker ps returns empty. docker ps -a doesn't show any docker containers started either.

Is there a way to keep the docker container running in the background, and execute the commands after it starts? Thanks for your help!

1 Answers
docker run -d --rm --ipc host --net host \
  --privileged --name test5 bash \
  -c "sleep 30000" docker/getting-started

Docker PS

CONTAINER ID   IMAGE    COMMAND                  CREATED         STATUS         PORTS      NAMES
c3fa4e4522a2   bash     "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes              test5

Note 1: Your container immediately stops unless the commands ( or process) keep running in foreground.

Note 2: an earlier version of your command bash -c 'echo "hi"' just print and since no other process running it is an exit. So, in order to keep running so need to write some commands to keep on running.

Note 3: For simplicity, I made to sleep for 30000.

Should solve your issue.

Related