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?

  • Dockerfile privileged flag for Docker container (Needed because of Apache error ulimit ) AWS
  • Scripts and Dockerfile
  • Can't connect to ASP.NET core through docker
  • How to link two containers using docker-compose
  • LDAP authentication in Docker container
  • docker-machine create error … VBoxManage: error: Unknown option: --synthcpu
  • 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!


  • What is the purpose of adding docker repository key to apt-key?
  • Can an ECS container have access to the docker socket?
  • Saving Docker container state on Windows
  • New NodeJS Error Thrown While Running mupx deploy
  • Exec commands on kubernetes pods with root access
  • RUN command in Dockerfile not persisting from container to container
  • 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

    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.