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?

  • Unable to start node app with shell script
  • When I use PyCharm and odoo, [Errno 2] No such file or directory Process finished with exit code 2
  • Are Bluemix service endpoints publicly accessible?
  • How to prevent Docker's container from being stopped after exiting a /bin/bash session
  • Docker Copy and change owner
  • Issue with docker compose
  • 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

  • How to mount the volume of host to docker container with executable file permission using docker api
  • Forward local SSH credentials to Docker container inside Vagrant VM
  • How can I install python modules in a docker image?
  • Docker : Sharing a volume on Windows with Docker toolbox
  • (django) (docker) Django webserver wont start
  • What are the differences between a VM image and a Docker image? [duplicate]
  • 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.