Skip to content

Instantly share code, notes, and snippets.

@simon0191
Last active September 24, 2018 10:23
Show Gist options
  • Save simon0191/d95651f77273b9a4ed52d827f1163e6e to your computer and use it in GitHub Desktop.
Save simon0191/d95651f77273b9a4ed52d827f1163e6e to your computer and use it in GitHub Desktop.
Docker RUN vs CMD

Docker RUN vs CMD

RUN

  • creates a layer, it actually modifies the image during the build.
  • it's executed during "build time"
  • you can specify multiple RUNs in a Dockerfile

CMD

  • doesn't create a layer
  • specifies the command that will be run by default when the container is run if no command is passed to the docker run instruction
  • it's not executed during "build time", it's executed in "run time".
#!/bin/bash
docker build -f test_run.Dockerfile -t test_run .
docker build -f test_cmd.Dockerfile -t test_cmd .
docker run test_run cat hello.txt
# expected: hello
docker run test_cmd cat hello.txt
# expected: cat: hello.txt: No such file or directory
FROM ubuntu
CMD echo hello > hello.txt
FROM ubuntu
RUN echo hello > hello.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment