2

Ok just learning about creating a Dockerfile and my understanding (which is probably wrong) is that the CMD command should be able to run a shell script from within the container once it is placed there.

I am just trying it out with a vanilla apache2 install

Dockerfile

FROM ubuntu:latest

ADD install-apache.sh /Scripts/install-apache.sh
RUN chmod +x /Scripts/install-apache.sh
CMD [/Scripts/install-apache.sh]



RUN echo "Hope this worked!"

I have also tried this:

CMD ["/Scripts/install-apache.sh"]

When I use the RUN command it works (without the [ and ])

So I am a bit lost what the CMD is suppose to do.

I followed the instructions from http://kimh.github.io and read through the docker docs as well.

Question: How am I using the CMD wrong and how should I use it in this scenario?

1 Answer 1

5
  • RUN is executed while you are building an image. This is usually used to install things or perform modifications in the resulting image.
  • CMD is executed when the container is started. Use this to e.g. start applications.

Also check the docs

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.

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

5 Comments

Oh so the CMD is run when a user runs the container I am building - and RUN is used when I am building it?
@JeffKranenburg exactly
Thanks @Sebastian - will accept in a few minutes when SO allows me too :-)
Yep. You could literally just change CMD [/Scripts/install-apache.sh] to RUN /Scripts/install-apache.sh and it would do what you're expecting it to. As an example of CMD, you'd likely want something like CMD ["httpd", "-DFOREGROUND"] to automatically start apache for you when the container boots up.
But i want to run the .sh file on CMD how can i achieve that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.