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 27 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
dbbe985
ISSUE-90 section outline
ivorscott Nov 14, 2021
84936ae
ISSUE-90 add entrypoint/command introduction and lecture titles
ivorscott Nov 15, 2021
ac60742
ISSUE-90 refine Lecture 1
ivorscott Nov 28, 2021
d7b5a99
ISSUE-90 clean up folder
ivorscott Nov 28, 2021
7cb1959
ISSUE-90 add Lecture 2
ivorscott Nov 28, 2021
1b4432a
ISSUE-90 add Lecture 3
ivorscott Nov 28, 2021
1bf1d42
ISSUE add note to Lecture 1
ivorscott Nov 29, 2021
743f780
ISSUE-90 add 4 rules for CMD and ENTRYPOINT
ivorscott Nov 29, 2021
8f533b6
ISSUE-90 replace images with text
ivorscott Jan 23, 2022
1f54a0e
ISSUE-90 fix terminology
ivorscott Jan 25, 2022
5d40757
ISSUE-90 add exercises
ivorscott Jan 30, 2022
5465263
ISSUE-90 fix readme and bash script linting issues
ivorscott Feb 9, 2022
b116f60
adding gha
BretFisher Jan 16, 2022
16cee52
fixing lints (#95)
BretFisher Jan 16, 2022
4a311a9
Fixing lints (#96)
BretFisher Jan 16, 2022
76d6d32
Fixing lints, again (#97)
BretFisher Jan 16, 2022
80f40a2
ISSUE-90 update markdown linter
ivorscott Feb 13, 2022
229e59b
Merge branch 'main' into ISSUE-90
ivorscott Feb 13, 2022
72c73f5
ISSUE-90 fix markdown linter
ivorscott Feb 13, 2022
7bed733
ISSUE-90 fix linter, restructure folder and readme
ivorscott Feb 13, 2022
986d4de
ISSUE-90 add quiz and update section structure
ivorscott Feb 13, 2022
6993077
fix wording
ivorscott Feb 13, 2022
7236918
fix typos
ivorscott Feb 13, 2022
4ad1b57
Merge branch 'main' into ISSUE-90
BretFisher May 23, 2023
2ad6589
Merge branch 'main' into ISSUE-90
BretFisher Feb 13, 2024
f57e492
more section adding on entrypoint-cmd
BretFisher Dec 4, 2024
db8091e
Merge branch 'main' into ISSUE-90
BretFisher Dec 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 216 additions & 0 deletions dockerfiles/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
# Dockerfile Next Steps
Table of Contents

[Understanding ENTRYPOINT and CMD](#entrypoint-and-cmd)

## Understanding ENTRYPOINT and CMD

### What's an ENTRYPOINT? What's a CMD?

#### ENTRYPOINT

An `ENTRYPOINT` allows you to configure a container that will run as an executable.
ivorscott marked this conversation as resolved.
Show resolved Hide resolved

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"
```

#### 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: ENTRYPOINT

In this lecture, we will discuss ENTRYPOINT.

### Exercise

In this exercise...

Exercise Files

- [/dockerfiles/entrypoint-1](/dockerfiles/entrypoint-1)

Resources

- https://docs.docker.com/engine/reference/builder/#entrypoint

### Lecture 2: CMD

In this lecture, we will discuss CMD.

### Exercise

In this exercise...

Exercise Files

- [/dockerfiles/command-1](/dockerfiles/command-1)

Resources

- https://docs.docker.com/engine/reference/builder/#cmd

### Lecture 3: OVERRIDING ENTRYPOINT and CMD in the CLI

In this lecture, we will discuss command line properties.

```
--entrypoint Overwrite the default ENTRYPOINT of the image
```

You can overwrite the command at the command line as well.

### Exercise

In this exercise...

Exercise Files

- [/dockerfiles/entrypoint-2](/dockerfiles/entrypoint-2)
- [/dockerfiles/command-2](/dockerfiles/command-2)

Resources
- https://docs.docker.com/engine/reference/commandline/run/#options (--entrypoint)


### Lecture 4: Using ENTRYPOINT and CMD in Docker Compose

In this lecture, we will discuss compose properties.

```
entrypoint: ["php", "-d", "memory_limit=-1", "vendor/bin/phpunit"]
```

```
command: ["bundle", "exec", "thin", "-p", "3000"]
```

### Exercise

In this exercise...

Exercise Files

- [/dockerfiles/entrypoint-3](/dockerfiles/entrypoint-3)
- [/dockerfiles/command-3](/dockerfiles/command-3)

Resources
- https://docs.docker.com/compose/compose-file/compose-file-v3/#entrypoint
- https://docs.docker.com/compose/compose-file/compose-file-v3/#command


<hr/>

<details>
<summary>Quiz</summary>

<br/>

1. When would you want to use both a command and entrypoint?

A)

B)

C)

D)

2. Which of the following statements are true?

A)

B)

C)

D)

3. Select the correct way to do X?

A)

B)

C)

D)

<br>

<details>
<summary>See Quiz Answers</summary>
<br>

- Q1: __A__
- Q2: __C__
- Q3: __D__
</details>
</details>
<hr/>

### Assignment 1: Build a curl image

For this assignment, your task is to...

Assignment Files

[/dockerfiles/entrypoint-command-assignment-1](/dockerfiles/entrypoint-cmd-assignment-1)
Empty file added dockerfiles/command-1/.gitkeep
Empty file.
Empty file added dockerfiles/command-2/.gitkeep
Empty file.
Empty file added dockerfiles/command-3/.gitkeep
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.