Jason
Jason

Reputation: 2492

Multiple commands during docker run

I am new to docker and I created a docker image which runs a rails app. Now I want to run the app in background as a deamon, but it keeps on bailing out on me:

docker run -d -p 2000:2000 image_name "source ~/.bash_profile; cd src; rails s"

As you can see, my rails app is in the src directory.

I have also tried adding the rails path in the bashrc for the docker image, still it errors out as below:

Can somebody please correct me.

Upvotes: 0

Views: 1008

Answers (2)

Mingjiang Shi
Mingjiang Shi

Reputation: 7935

Try the following approach:

docker run -d -p 2000:2000 image_name sh -c 'source ~/.bash_profile; cd src; rails s'

This is because docker doesn't support running multiple commands, while "sh -c " is one command.

Reference: http://timmurphy.org/2015/02/27/running-multiple-programs-in-a-docker-container-from-the-command-line/

Upvotes: 0

Vor
Vor

Reputation: 35109

You can wrap your commands in something like startup.sh, then make it executable. Then simply call docker run -d -p 2000:2000 image_name startup.sh or if you want to always run this command when the container is started, take a look at CMD and ENTRYPOINT commands.

Also keep in mind that to keep docker containers running, you need to keep a process active in the foreground.

Upvotes: 1

Related Questions