How could I launch multiple container at a time shared one Dockerfile

972 views Asked by At

I want to launch hundreds of container with one command.

Container A will change directory to a then run a.rb when it is launched

Container B will change directory to b then run b.rb when it is launched

Container ... will change directory to ... then run ....rb when it is launched

All of those containers should share the same Dockerfile.

I need to switch to correspoding folder then execute script

But I got error

failed to exec: exec: "cd": executable file not found in $PATH

  a:
    build: .
    command: cd a ; ruby a.rb
    volumes:
        - .:/crawler
    ports:
      - 5901:5901
  b:
    build: .
    command: cd b ; ruby b.rb
    volumes:
        - .:/crawler
    ports:
      - 5902:5902
1

There are 1 answers

0
VonC On

First, the image built from the Dockerfile must include a/r.rb and b/b.rb.
That means the Dockerfile has 2 COPY directives, to include those scripts.

Since you want only one Dockerfile, producing one image, you should include in it a COPY start.sh script and CMD start.sh directive. That script (now included in the image) which would run a/r.rb or b/r.rb depending on an environment variable (for example MUSTRUN).

docker run -e MUSTRUN=a myimage
or 
docker run -e MUSTRUN=b myimage

Keep it mind your container will stop once the r.rb script exits.
To keep it running, you would need to change the start.sh in order to loop indefinitely, a bit like "A Daemonized Hello world".