1

I am creating a Docker image to initialize my PostgreSQL database. It looks like this:

FROM debian:stretc

RUN set -x \
     && ... ommitted ...
     && apt-get install postgresql-client -y

COPY scripts /scripts

CMD cd /scripts && psql -f myscript.sql

This works great. Every time I need to initialize my database I start the container (docker run --rm my-image). After the psql command is done, the container is automatically stopped and removed (because of the --rm). So basically, I have a Docker-image-as-executable.

But, I am confused whether that last line should be:

CMD cd /scripts && psql -f myscript.sql

or

ENTRYPOINT cd /scripts && psql -f myscript.sql

Which one should be used in my case (Docker-image-as-excutable)? Why?

  • Possible duplicate of [What is the difference between CMD and ENTRYPOINT in a Dockerfile?](https://stackoverflow.com/questions/21553353/what-is-the-difference-between-cmd-and-entrypoint-in-a-dockerfile) – Oliver Charlesworth Dec 06 '17 at 08:53

2 Answers2

0

You need to use ENTRYPOINT if you need to make it as "Docker-image-as-executable"

  • RUN executes the command(s) that you give in a new layer and creates a new image. This is mainly used for installing a new package.

  • CMD sets default command and/or parameters, however, we can overwrite those commands or pass in and bypass the default parameters from the command line when docker runs

  • ENTRYPOINT is used when yo want to run a container as an executable.

sanath meti
  • 5,179
  • 1
  • 21
  • 30
0

Both Entrypoint and Command will do the same thing. The only main difference is that when you use CMD you have more flexibilty in overriding the command that runs from the CLI.

so If you have the dockerfile:

FROM debian:stretc

RUN set -x \
     && ... ommitted ...
     && apt-get install postgresql-client -y

COPY scripts /scripts

CMD cd /scripts && psql -f myscript.sql

You can override the CMD defined in the dockerfile from the cli to run a different command:

docker run --rm my-image psql -f MYSCRIPT2.sql

This will run MYSCRIPT2.sql as given in the cli. You can't do that with ENRTYPOINT.

yamenk
  • 46,736
  • 10
  • 93
  • 87