19

I want to run multiple containers automatically and create something,

but some images, such as swarm, will automatically stop after run or start.

I already try like that

docker run -d swarm

docker run -d swarm /bin/bash tail -f /dev/null

docker run -itd swarm bash -c "while true; do sleep 1; done"

but 'docker ps' show nothing, and I tried to build Dockerfile by typing:

FROM swarm
ENTRYPOINT ["echo"]

and The image does not run with error message :

docker: Error response from daemon: invalid header field value "oci runtime error: container_linux.go:247: starting container process caused \"exec: \\\"echo\\\": executable file not found in $PATH\"\n".

I can't understand this error... How can I keep swarm container running..?

(Sorry,my English is not good))

haeny
  • 363
  • 2
  • 5
  • 15

7 Answers7

13

using -d is recommended because you can run your container with just one command and you don’t need to detach terminal of container by hitting Ctrl + P + Q.

However, there is a problem with -d option. Your container immediately stops unless the commands are not running on foreground. Docker requires your command to keep running in the foreground. Otherwise, it thinks that your applications stops and shutdown the container.

The problem is that some application does not run in the foreground.

In this situation, you can add tail -f /dev/null to your command. By doing this, even if your main command runs in the background, your container doesn’t stop because tail is keep running in the foreground.

docker run -d swarm tail -f /dev/null

docker ps shows container

Now you can attach your container by using docker exec container_name command

or

docker run -d swarm command tail -f /dev/null

Navin a.s
  • 416
  • 2
  • 8
  • 19
  • As you tell me, I try this command but the container still can't keep running. If possible, could you confirm that when the swarm image run with that command, the container's show in the **docker ps**? I want to check whether my server is strange or not... and while waiting for an answer, I did a simple test using Dockerfile and got an error message. I have updated the post, and I hope you can refer to it to solve this problem.Thank you for your continued interest in solving my problem – haeny Aug 04 '17 at 06:00
11

First of all you don't want to mix the -i and -d switches. Either you would like to run the container in interactive or detached mode. In your case in detached mode:

docker run -d swarm /bin/bash tail -f /dev/null

There are also no need to allocate a tty using the -t flag, since this only needs to be done in interactive mode.

You should have a look at the Docker run reference

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
3

Docker container does two type of task. One is to perform and exit & other is to run it in background.

To run docker container in background, there are few options.

  1. Run using shell. docker run -it <image> /bin/bash
  2. For continuously running container. docker run -d -p 8080:8080 <image>. Assuming image will expose port 8080 and in listening mode.
Niket Shah
  • 331
  • 4
  • 4
1

It's fine to to a tail on /dev/null, but why not make it do something useful?

The following command will reap orphan processes, so no zombie (defunct) precesses are left floating around. Also some init.d / restart scripts doesn't allow this.

exec sh -c 'while true ;do wait ;done'
TheMadsen
  • 196
  • 1
  • 11
0

You are right docker run -itd swarm ( Without give argument for container( bash -c "while true; do sleep 1; done" ) )works fine .If you pass argument for docker run it will run the command and terminates the container.If you want to run the container permanently first start the container with docker run -itd swarm and check if the container runs or not by docker ps now the container runs , if you want to execute any command in container use docker exec -itd container_name command Remember : Only use the command which not stop the container. bash -c "while true; do sleep 1; done this command will stop the container ( Because this is complete command if we execute in normal terminal it execute and terminate , this type of command also terminates the container ).

I Hope this Helps..

Navin a.s
  • 416
  • 2
  • 8
  • 19
  • my code step like that: 1. image run without command ( docker run -itd swarm) 2. check 'docker ps' 3. if the containner is not running, 3-1 remove container and image run with command (docker run -itd swarm bash ...) so, running an image without a command has no effect.... – haeny Aug 03 '17 at 00:24
0

Basically this is the method , but your docker image is swarm so it is different and i don't know about swarm docker image and i am not using that .But after i research about that . First i run the docker swarm image it shows.,

enter image description here

After that i understand we run docker swarm image by using only five commands in picture like create, list manage, join, help . if we run swarm image without command like docker run -itd swarm it takes command as --help . Sorry, But i don't know what is the purpose of swarm image. For more usage check https://hub.docker.com/_/swarm/ .

The answer that i added docker run -itd image tail -f /dev/null is not for swarm image , it is for docker images like ubuntu,fedora, centos.

Just read the usage of swarm image and why it is used .

After if you have any issue post your issue in https://github.com/docker/swarm-library-image/issues

Thank You...

Navin a.s
  • 416
  • 2
  • 8
  • 19
  • I don't know the use of Swarm either.. Because my purpose is to get a lot of image information from the explore page and automatically pull and run it. But since Swarm is a special case, I will move on next step.. just it's kind of icky :( Anyway, thank you ^^ – haeny Aug 05 '17 at 02:29
0

have a container running

docker run --rm -d --name=tmp ubuntu sleep infinity

example of requesting a command from the dorment container

docker exec tmp echo hello from container

notes:

  • --rm removes the container if it is stopped
  • -d runs the container in the background
  • --name=tmp name the container so you control how to denote it
  • ubuntu pushes a light image to exec your commands
  • sleep infinity keeps the container dorment
diogo
  • 525
  • 1
  • 7
  • 12