How to execute docker commands after a process has started

I wrote a Dockerfile for a service (I have a CMD pointing to a script that starts the process) but I cannot run any other commands after the process has started? I tried using ‘&’ to run the process in the background so that the other commands would run after the process has started but it’s not working? Any idea on how to achieve this?

For example, consider I started a database server and wanted to run some scripts only after the database process has started, how do I do that?

  • Docker PostgreSQL - Scripts in /docker-entrypoint-initdb.d doesn't run
  • Fail to execute command using Docker
  • Fig Python 3 Compatible?
  • Docker Compose - Not picking up the dump file in the init command?
  • Wasabi automatically restart docker container
  • Docker multi-container logging cap
  • Edit 1:

    My specific use case is I am running a Rabbitmq server as a service and I want to create a new user, make him administrator and delete the default guest user once the service starts in a container. I can do it manually by logging into the docker container but I wanted to automate it by appending these to the shell script that starts the rabbitmq service but that’s not working.

    Any help is appreciated!

    Regards

  • MobileBackups folders interfering with Jenkins use of Docker slave
  • Mariadb-10.0 install fails in boot2docker with “policy-rc.d denied execution” but works in Docker
  • How to enable code running in a docker container access files on the host on which the container is running?
  • Docker web panel with permissions checking
  • How to profile nodejs app for memory leaks inside docker container?
  • Use encrypted client certificate to connect to a docker host
  • 2 Solutions collect form web for “How to execute docker commands after a process has started”

    I partially understood your question. Based on what I perceived from your question, I would recommend you to mention the Copy command to copy the script you want to run into the dockerfile. Once you build an image and run the container, start the db service. Then exec the container and get into the container, run the script manually.

    If you have CMD command in the dockerfile, then it will be overwritten by the command you specify during the execution. So, I don’t think you have any other option to run the script unless you don’t have CMD in the dockerfile.

    Specifically around your problem with Rabbit MQ – you can create a rabbitmq.config file and copy that over when creating the docker image.

    In that file you can specify both a default_user and default_pass that will be created when an the database is set from scratch see https://www.rabbitmq.com/configure.html

    AS for the general problem – you can change the entry point to a script that runs whatever you need and the service you want instead of the run script of the service

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.