From 84936aed4c85563e2fa845c0d3f20472f08b6cf7 Mon Sep 17 00:00:00 2001 From: Ivor Scott Date: Mon, 15 Nov 2021 01:06:05 +0100 Subject: [PATCH] ISSUE-90 add entrypoint/command introduction and lecture titles --- dockerfiles/README.md | 91 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 17 deletions(-) diff --git a/dockerfiles/README.md b/dockerfiles/README.md index 3ffac842..aa88df1b 100644 --- a/dockerfiles/README.md +++ b/dockerfiles/README.md @@ -5,21 +5,78 @@ Table of Contents ## Understanding ENTRYPOINT and CMD -- What - - Entrypoints are ... - - Commands are ... -- Why - - Both are often used together (they compliment each other). -- How +### What's an ENTRYPOINT? What's a CMD? + +#### ENTRYPOINT + +An `ENTRYPOINT` allows you to configure a container that will run as an executable. + +There are _two_ forms: the _exec form_ and the _shell form_. + +__SYNTAX__ + +```Dockerfile +ENTRYPOINT ["executable", "param1", "param2"] # execform +``` + +```Dockerfile +ENTRYPOINT command param1 param2 # shellform +``` + +When you run a container, any command line arguments will be appended to the entrypoint. + +This feature allows us to create images with a default executable in mind or change the default entrypoint of a base image. + +For example, the default entrypoint for the offical nginx image is `ENTRYPOINT ["/docker-entrypoint.sh"]` [1](https://github.com/nginxinc/docker-nginx/blob/2decc81a019b5df087c9162d3621b1c9beb3104f/mainline/debian/Dockerfile). We can override this to print the contents of the container filesystem instead. + +```Dockerfile +FROM nginx:1.21.4 +ENTRYPOINT ["ls"] +``` +#### CMD + +`CMD` provides defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case the executable is the entrypoint. + +There are _three_ forms: + +__SYNTAX__ + +```Dockerfile +CMD ["executable","param1","param2"] # execform +``` + +```Dockerfile +CMD ["param1","param2"] # parameter-only form +``` + +```Dockerfile +CMD command param1 param2 # shellform +``` + +__NOTE:__ If you omit the executable, you must have an ENTRYPOINT defined in the Dockerfile. + +Using the same custom nginx image, this is how it would look like using CMD. + +```dockerfile +FROM nginx:1.21.4 +CMD ["ls"] +``` +Now you might be wondering, if we can use CMD in the same way as ENTRYPOINT why does Docker need both? The truth is, they compliment each other. ENTRYPOINT and CMD are often used together. When a command is present, it's appended to the end of the entrypoint. + +For example: + +```dockerfile +FROM nginx:1.21.4 +ENTRYPOINT ["ls"] +CMD ["-l"] +# becomes "ls -l" +``` - __SYNTAX__ - ```Dockerfile - ENTRYPOINT ./main - CMD ["--my-flag", "1" ] # --my-flag 1 - ``` +#### Summary +Both ENTRYPOINT and CMD provide increased flexibility in how we write Dockerfiles. They allow us to provide good defaults in our images, with the option to change this behavior at runtime. -### Lecture 1: Something interesting +### Lecture 1: ENTRYPOINT In this lecture, we will discuss ENTRYPOINT. @@ -35,7 +92,7 @@ Resources - https://docs.docker.com/engine/reference/builder/#entrypoint -### Lecture 2: Something else +### Lecture 2: CMD In this lecture, we will discuss CMD. @@ -51,7 +108,7 @@ Resources - https://docs.docker.com/engine/reference/builder/#cmd -### Lecture 3: One more thing +### Lecture 3: OVERRIDING ENTRYPOINT and CMD in the CLI In this lecture, we will discuss command line properties. @@ -74,7 +131,7 @@ Resources - https://docs.docker.com/engine/reference/commandline/run/#options (--entrypoint) -### Lecture 4: One more thing +### Lecture 4: Using ENTRYPOINT and CMD in Docker Compose In this lecture, we will discuss compose properties. @@ -127,7 +184,7 @@ Resources D) -3. Select the correct way to day X? +3. Select the correct way to do X? A) @@ -156,4 +213,4 @@ For this assignment, your task is to... Assignment Files -[/dockerfiles/entrypoint-command-assignment-1](/dockerfiles/entrypoint-command-assignment-1) \ No newline at end of file +[/dockerfiles/entrypoint-command-assignment-1](/dockerfiles/entrypoint-cmd-assignment-1) \ No newline at end of file