Docker Community Forums

Share and learn in the Docker community.

Execute script in Dockerfile


(Mrwicia) #1


I am trying to run provisioning .sh script for sonatype/nexus3 image:

This script can only be run when container is up & running. Is it possible to execute this script in Dockerfile? I was trying to run it via CMD command but after script was executed the container Exited.

Should I override nexus3 image Dockerfile?

Thanks for help :slight_smile:

(Bencummins) #2

You’re probably looking for the RUN command, not CMD

(Mrwicia) #3

Is it RUN used only in image build phase not container running?

(Martin Terp) #4

RUN is only for Dockerfile :slight_smile:

For telling the Dockerfile which command to run when the container start, see “ENTRYPOINT” and “CMD”

(Mrwicia) #5

So I should use CMD. Now lets assume that my Dockerfile contains: CMD ["./"] after run my nexus container this script will be executed and container will stop but I want to be working :slight_smile: Is it possible to have both: working container and executed script?

(Martin Terp) #6

then in your provisioning script, you need to put the command of the proccess you want to be running

(Mrwicia) #7

Is it possible to retrieve parent image process code and - using it - extend my CMD process?

(Ovelindstrom) #8

It is… but it is not the way you should do it.

The best way to do that is to write an ENTRYPOINT script in your Dockerfile that does whatever setup is needed, and then ends with exec "$@" to run the normal CMD (or whatever got passed on the command line). The official mysql image entrypoint script 1.0k is a little bit complex, but it has the right basic pattern.

Following this pattern, you should be able to create an extension of the sonatype/nexus3 image and in your Dockerfile add the ENTRYPOINT script with your provisioning.

If you, however, need to have Nexus running, for example to add users, you need to override the script and add a reference to your provisioning script.