Skip to content

Commit

Permalink
ISSUE-90 refine Lecture 1
Browse files Browse the repository at this point in the history
  • Loading branch information
ivorscott committed Nov 28, 2021
1 parent 84936ae commit c007526
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 113 deletions.
152 changes: 39 additions & 113 deletions dockerfiles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,153 +5,79 @@ Table of Contents

## Understanding ENTRYPOINT and CMD

### What's an ENTRYPOINT? What's a CMD?
### Lecture 1: What's an ENTRYPOINT?

#### ENTRYPOINT
You remember `CMD`. That's the thing the container runs on start. There's also, `ENTRYPOINT`, which can also run things on start, but how do they work together?

An `ENTRYPOINT` allows you to configure a container that will run as an executable.
Everytime you start a container, docker takes `ENTRYPOINT` and `CMD` and just combines them into one long command with a space between them.

There are _two_ forms: the _exec form_ and the _shell form_.
![](/docs/images/entrypoint-cmd.png)

__SYNTAX__
### Why would you want this?

```Dockerfile
ENTRYPOINT ["executable", "param1", "param2"] # execform
```
Combining `ENTRYPOINT` and `CMD` allows you to create Dockerfiles for cli tools and scripts. For example, the default `ENTRYPOINT` for the offical nginx image is a script:

```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.
```dockerfile
ENTRYPOINT ["/docker-entrypoint.sh"]

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.
# https://bit.ly/3FXw00f
```
You can create a custom Dockerfile that uses the nginx binary directly. This might be useful if you want to use the cli tool with your own default options using `CMD`. Here's an example, we can replace the default `ENTRYPOINT` and configure nginx to print it's help text.

```Dockerfile
```dockerfile
FROM nginx:1.21.4
ENTRYPOINT ["ls"]
ENTRYPOINT ["nginx"]
CMD ["-h"]
# becomes "nginx -h"
```
#### 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
Let's run it.
```bash
docker build -t testnginx . # build image
docker run --rm testnginx # start container
```

```Dockerfile
CMD ["param1","param2"] # parameter-only form
Output:
```

```Dockerfile
CMD command param1 param2 # shellform
nginx version: nginx/1.21.4
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /etc/nginx/)
-e filename : set error log file (default: /var/log/nginx/error.log)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
```

__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)
So to recap an `ENTRYPOINT` allows you to configure the default executable for the container which can be extended with additional `CMD` options. Doing so allows you to create custom Dockerfiles for cli tools and your own scripts. You'll get more practice in future lectures and learn how to modify ENTRYPOINT and CMD at runtime.

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
### Lecture 2: USING 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
### Lecture 3: 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
Expand Down
Binary file added docs/images/Untitled-2021-11-28-0434.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/entrypoint-cmd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c007526

Please sign in to comment.