3

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.

1
  • 1
    FYI – A new container may be created with docker run, but it can also be created with docker create. AFAIK there is no command that can be run when just docker create is run. Commented Feb 15, 2015 at 4:58

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

An ENTRYPOINT script will run on docker start, but you could easily include logic in the script such that it could check if it had previously run.
Although I don't think this is a good way to do this, it's a Docker problem. The ENTRYPOINT seems to be a proper workaround.
-2

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/

1 Comment

There is a downside using CMD: a Dockerfile can have many of it, but only the last one will run.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.