Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Section: Dockerfile ENTRYPOINT and CMD #91

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft

Conversation

ivorscott
Copy link

@ivorscott ivorscott commented Nov 14, 2021

Fixes issue #90

Section: ENTRYPOINT and/vs CMD

  • Lecture 1 basic info, no examples
  • Lecture 2 Entrypoint for CLI with example
  • Assignment 1 Entrypoint for curl cli
  • Lecture 3 Entrypoint for pre-CMD scripts with example
  • Lecture 4 Entrypoints in official images for script automation
  • Assignment 2 Entrypoint for react app with env var for API_URL
  • Quiz

@BretFisher BretFisher changed the title ISSUE-90 New Section: Dockerfile Next Steps Nov 15, 2021
@BretFisher BretFisher self-requested a review November 15, 2021 20:42
@BretFisher BretFisher added the enhancement New feature or request label Nov 15, 2021
@BretFisher BretFisher linked an issue Nov 15, 2021 that may be closed by this pull request
5 tasks
dockerfiles/README.md Outdated Show resolved Hide resolved
@ivorscott ivorscott force-pushed the ISSUE-90 branch 8 times, most recently from 08a24bc to 79a1140 Compare January 30, 2022 00:33
@ivorscott ivorscott force-pushed the ISSUE-90 branch 8 times, most recently from 6108bd8 to 5872f45 Compare January 30, 2022 11:47
Copy link
Owner

@BretFisher BretFisher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you planning to remove all images? I still see the Excalidraw slides. Let's just remove Excalidraw images for now until I know what one's I'll need.

I like to teach ENTRYPOINT + CMD in two steps: 1st teach them that it just combines the two. 2nd, teach the "why" as two main use cases:

  1. Use ENTRYPOINT to run CLI commands while specifying options at runtime. ()
  2. Use ENTRYPOINT to run a script before it passes execution to CMD. (this has to be explained in detail on how exec "$@" works in bash and how that relates to ENTRYPOINT + CMD)

I generally like to show using CLI command examples (like curl) before I dig into entrypoint script examples (due to their complexity of passing execution).

Can we separate the lectures out to fit this format, and revise examples:

  • Lecture 1: ENTRYPOINT's and how they relate to CMD (no exercise demos, just concepts)
  • Lecture 2: Using ENTRYPOINT for CLI commands (use my bretfisher/httping image as Exercise)
  • Assignment 1: create Dockerfile to build an image for CLI command of curl
  • Lecture 3: Using ENTRYPOINT for running a script before starting a long-running program (use postgres image as Exercise, showing how Docker Hub can help us find the entrypoint script and how setting a username as env gets used in entrypoint script to create db user).
  • Lecture 4 Using ENTRYPOINT automation providers. This extends on lecture 3 and shows now postgres, MySQL, nginx, and others provide a fancy ENTRYPOINT that lets you drop files in a specific location to have them execuited, like sql imports, shell scripts, etc.
  • Assignment 2: create Dockerfile and entrypoint script to run a react app that calls a API, but we change API URL before nginx starts. We'll need to create that maybe (I have some existing research we can use)
  • Quiz

Existing compose lecture: This above section is planning to be added before they learn about Docker Compose, so maybe move that markdown to a new issue in backlog to add entrypoint/command to the Docker Compose section at a later date.

@@ -1,7 +1,15 @@
# MD013/line-length - Line length
MD013:
# Number of characters
line_length: 150
line_length: 500
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In markdown, a LF/CR in a sentence or paragraph doesn't affect HTML formatting, so it's best to leave at 150 (meant for log lines, code, or cli commands) and just LF/CR at end of sentences.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job

dockerfiles/entrypoint/README.md Show resolved Hide resolved
<summary>See Quiz Answers</summary>
<br>

- __Answser: A__
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each correct answer also has to indicate why it's correct. I recommend changing:

- __Answser: A__

to

__A) Correct. The two together create a flexible way to control what the container executes on start.__

Then change other Q&A Answers to match.


- __Answser: A__

B) Incorrect. This was joke.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a joke.

dockerfiles/entrypoint/README.md Show resolved Hide resolved
dockerfiles/entrypoint/README.md Show resolved Hide resolved
dockerfiles/entrypoint/README.md Show resolved Hide resolved
</details>
<br>

2. __Why does the resulting command vary when combining ENTRYPOINT and CMD?__
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This question isn't covered in the lessons. We should only test what we've taught.

dockerfiles/entrypoint/README.md Show resolved Hide resolved
@BretFisher BretFisher changed the title New Section: Dockerfile Next Steps New Section: Dockerfile ENTRYPOINT and CMD Feb 15, 2022
@BretFisher
Copy link
Owner

@ivorscott I updated the OP to reflect the new flow and lecture subjects

Copy link
Owner

@BretFisher BretFisher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the prior PR review, I think the examples and assignments need some reorganizing:

  • examples should be realistic and useful. Showing how to run nginx -h to show off a CLI example isn't a real-world use case... or at least not one I've seen needed in the wild. using something like bretfisher/httping could be a useful replacement to the nginx -h one. The curl assignment is a perfect use case.
  • remember that if we create scripts or code, we don't want them getting distracted in what the code does itself, if it's just used to show off a Docker feature. With rick.sh, I thought we were showing off entrypoint + cmd for running startup scripts, or passing values to a script, but it actually focuses on trap and signals, and that's too much to cover in a single lecture I think. I'd rather have rick.sh just show off sending a optional value to a script, and leave signals (and how shell/exec affects them) for a lecture later (really, it could be a whole section where we show off the signal problem, talk about pid1 wrappers like tini, etc. (which we do some of in Docker for Node.js)

docker run --rm --entrypoint nginx nginx:1.21.4 -h
```

### 4 Rules
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather us add value here rather than just repeating the docs. Maybe we make it recommendations rather than rules. Let's mix them up with added info like this (replaces rules and gotcha):

Recommendations for using CMD and ENTRYPOINT.

  1. Dockerfile should specify at least one of the CMD or ENTRYPOINT commands.
  2. Like other run-time metadata, CMD and ENTRYPOINT` are not relevant until a container is started.
  3. Docker only stores the last CMD and ENTRYPOINT in the Dockerfile. It'll also look at FROM images if they are missing in the current Dockerfile.
  4. Default for most custom Dockerfiles to just setting CMD. You typically only add ENTRYPOINT if you want a startup script or to create a CLI command. Said another way, don't use ENTRYPOINT by itself. It'll work, but the convention of simple Dockerfiles (e.g. running a Python script or Node.js server) is to put it all in CMD.
  5. If ENTRYPOINT uses the shell format, CMD is ignored. Default to using exec format in both (the JSON syntax). Only use shell format when you know why you're doing it that way (e.g. you need shell variables in the command).


4 Rules For `CMD` & `ENTRYPOINT`

1) Dockerfile should specify at least one of the `CMD` or `ENTRYPOINT` commands.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By convention, don't use 1) for numbered lists in Markdown. It doesn't always work. Use 1. for all items in the list and the rendered output will order them properly. Always using 1. for all ordered list items is a standard in my courses, and allows for easier re-ordering or inserting an item in an existing list.


When you combine the `ENTRYPOINT` and the `CMD` [the resulting command may vary](https://docs.docker.com/engine/reference/builder/#understand-how-cmd-and-entrypoint-interact) based on usage of either _shell_ or _exec_ form in the Dockerfile. If you use shell form with `ENTRYPOINT`, any `CMD` or run command-line arguments will be ignored and `ENTRYPOINT` will be started as a subcommand of `/bin/sh -c` which doesn't pass Unix signals like `SIGTERM` from `docker stop <container>`.

### Exercise 2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the rickroll, but I feel like we're going too far down the signals path, which we have yet to explain them in the course. The student won't fully understand linux signals yet. It's actually worth a lecture on them in in an advanced containers section, but not here. I'd rather the example show how a script can run via entrypoint and then pass execution to the CMD. Let's talk about it in relation to the other examples I mentioned in a previous PR Review.

@BretFisher BretFisher assigned BretFisher and unassigned ivorscott Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New Section: Dockerfile ENTRYPOINT and CMD
3 participants