Run commands on create a new Docker container

Is it possible to add instructions like RUN in Dockerfile that, instead of run on docker build command, execute when a new container is created with docker run? I think this can be useful to initialize a volume attached to host file system.

  • Multiple Docker images in .gitlab-ci.yml
  • Number of commands in Dockerfile
  • Docker node express volume changes in the host not reflected in the container
  • How get the number of pulls of a docker tag?
  • docker container throws error: php_network_getaddresses: getaddrinfo failed: Name or service not known
  • Docker compose doesn't recognize 'env_file'
  • MupX, graphicsmagick and env var
  • Can not unmount a device using “umount” in Docker
  • How can a docker service know about all other containers of the same service?
  • How can I give Docker containers access to a dnsmasq local DNS resolver on the host?
  • How to get a container's info when using rhuss/docker-maven-plugin?
  • Docker Java Application failing at obtaining input from console
  • 2 Solutions collect form web for “Run commands on create a new Docker container”

    Take a look at the ENTRYPOINT command. This specifies a command to run when the container starts, regardless of what someone provides as a command on the docker run command line. In fact, it is the job of the ENTRYPOINT script to interpret any command passed to docker run.

    I think you are looking for the CMD

    https://docs.docker.com/reference/builder/#cmd

    The main purpose of a CMD is to provide defaults for an executing
    container. These defaults can include an executable, or they can omit
    the executable, in which case you must specify an ENTRYPOINT
    instruction as well.

    Note: don’t confuse RUN with CMD. RUN actually runs a command and
    commits the result; CMD does not execute anything at build time, but
    specifies the intended command for the image.

    You should also look into using Data Containers see this excellent Blog post.

    Persistent volumes with Docker – Data-only container pattern
    http://container42.com/2013/12/16/persistent-volumes-with-docker-container-as-volume-pattern/

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