Im trying to run a bash script in a mongo db container on start up, so i have a script file and i put the location of the script file in the command argument of my docker-compose, like so:
mongo1:
container_name: mongo1
image: mongo:4.4
volumes:
- ~/mongors/data1:/data/db
- ./rs-init.sh:/scripts/rs-init.sh
networks:
- mongors-network
ports:
- 27021:27017
links:
- mongo2
- mongo3
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs" ]
command: /scripts/rs-init.sh
But when i do docker-compose up, it says this command is invalid, then gives me all the options for mongodb commands:
Invalid command: /scripts/rs-init.sh
Options:
--networkMessageCompressors arg (=snappy,zstd,zlib)
Comma-separated list of compressors to
use for network messages
General options:
-h [ --help ] Show this usage information
--version Show version information
-f [ --config ] arg Configuration file specifying
How do i run this bash script in the container on startup?
2
Answers
To understand the error, you need to understand the relationship between
command
andentrypoint
: the value ofcommand
is provided as an argument to whatever is defined inentrypoint
, so given this:You are trying to run the command line:
…which doesn’t make any sense;
/scripts/rs-init.sh
isn’t a valid argument to themongod
command.As @Turing85 points out in a comment, the correct solution in this case is to take advantage of the behavior built into the
mongo
container: it looks for initialization files in/docker-entrypoint-initdb.d
. For this to work, you need to not specify your ownentrypoint
in yourdocker-compose.yaml
(because doing so overrides theENTRYPOINT
baked into the container image):Unrelated to your question: You should also remove the
links
section from yourdocker-compose.yaml
file; the use oflinks
has been deprecated for years (because it is fragile in the event that one of the linked containers restarts); you can just refer to other containers in your compose file by name.When you have an
ENTRYPOINT
and aCMD
in the same container run, the CMD is passed to the ENTRYPOINT as a parameter and you end up only having a single, combined command being executed. In your case, the command ends up beingand Mongo complains that it doesn’t understand the last parameter.
Instead, you can change your entrypoint to a shell and run the 2 commands you want by concatenating them with
&&
, like thisI’ve chosen to start Mongo with the ‘normal’ startup script. If you prefer your existing way of doing it (
/usr/bin/mongod --bind_ip_all --replSet dbrs
), you should be able to use that instead.As a note, you should only do this if you want the script to run every time you start the container. If you want the script to only run when you’re initializing a new database (i.e. the
/data/db
volume mount is empty), then you should use the /docker-entrypoint-initdb.d directory as @Turing85 and @larsks suggest. But as I read your post, you want to run it every time.