Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commands to execute background process in Docker CMD

I am creating a docker image using a Dockerfile. I would like to execute some scripts while starting the docker container. Currently I have a shell script to execute all the necessary processes

CMD ["sh","start.sh"]

I would like to execute a shell command with a process running in background example

CMD ["sh", "-c", "mongod --dbpath /test &"]

like image 868
Jeevitha G Avatar asked Jul 22 '15 09:07

Jeevitha G


People also ask

How do I run a background image in docker?

To run a docker container in the background or the detached mode from the terminal, you can use the docker run command followed by the -d flag (or detached flag) and followed by the name of the docker image you need to use in the terminal.

Which command is used to run a container in background?

In order to run a container in the background, use the -d flag. It is recommended to name your container using the --name={container_name} flag when running a container in the background so that it can be referred to by name in follow-on commands.

How do I run a docker container in CMD?

How to Use the docker run Command. The basic syntax for the command is: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] You can run containers from locally stored Docker images.

What is CMD command in docker?

The CMD command​ specifies the instruction that is to be executed when a Docker container starts.


1 Answers

Besides the comments on your question that already pointed out a few things about Docker best practices you could anyway start a background process from within your start.sh script and keep that start.sh script itself in foreground using the nohup command and the ampersand (&). I did not try it with mongod but something like the following in your start.sh script could work:

#!/bin/sh ... nohup sh -c mongod --dbpath /test & ... 
like image 134
Henrik Sachse Avatar answered Oct 05 '22 23:10

Henrik Sachse