Start a container multiple times with a different command

I want to create a docker container with a mongodb configured with client access control (user authentication, see this).

I have successfully configured a docker container with mongo using this image. But it doesn’t use mongo access control.

The problem is that to enable access control I have to run mongodb with a specific command line (--auth) but only after creating the first admin user.

With a standard mongodb installation I normally perform these steps:

  • run mongod without --auth
  • connect to mongo and add the admin user
  • restart mongo with --auth

How I’m supposed to do it with docker? My idea is to first start the container without parameter, configure the user and then restart it using --auth

Probably I’m missing something, I’m new to docker…

One option is to pass a flag in the environment variable. While, you would have to customize the mongo docker image. For example, add an env variable EnableAuth with default value false. After you add the admin user, restart mongo with EnableAuth set to true.