Skip to content

Commit b01fadd

Browse files
author
kapnobatai137
committed
change headlines and slugs
1 parent 408727e commit b01fadd

File tree

5 files changed

+171
-2
lines changed

5 files changed

+171
-2
lines changed

docker/fundamentals/images/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Docker container blueprints
44

55
insights:
66
- docker-images
7-
- dockerfile
8-
- dockerfile-ii
7+
- dockerfile-format
8+
- dockerfile-instructions
99
- docker-image-tags
1010
- docker-image-registries
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
author: nem035
3+
aspects:
4+
- introduction
5+
- workout
6+
type: normal
7+
category: must-know
8+
links:
9+
- '[Best practices for writing Dockerfiles](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/){article}'
10+
- '[Dockerfile reference](https://docs.docker.com/engine/reference/builder/){documentation}'
11+
---
12+
13+
# Dockerfile Format
14+
15+
---
16+
17+
## Content
18+
19+
A Dockerfile represents an image creation spec.
20+
21+
Essentially, it is just a text file with Docker-specific syntax that contains all the instructions the Docker daemon can use to build a specific image.
22+
23+
Instructions in a Dockerfile are executed in order, top-down.
24+
25+
The format of a Dockerfile is the following:
26+
27+
```Dockerfile
28+
# Comment
29+
INSTRUCTION arguments
30+
```
31+
32+
Dockerfile instructions are UPPERCASE by convention to more easily distinguish them from their arguments.
33+
34+
Here's an example of a Dockerfile:
35+
36+
```Dockerfile
37+
# Use the node image as the base image
38+
FROM node
39+
40+
# Initialize an env var for this image
41+
# that has a default value of "fun"
42+
ENV ENKI="fun"
43+
44+
# the RUN command allows us to run any
45+
# shell command or bash script available
46+
# at this step of building the image.
47+
# This command will have access to all
48+
# environment variables created above it.
49+
RUN echo "Learning Docker is $ENKI"
50+
51+
# Expose port 1234 on the container.
52+
# We still have to use -p when running this
53+
# container to link this port to an exposed
54+
# port on the host in which the container is
55+
# running
56+
EXPOSE 1234
57+
58+
# CMD (and/or ENTRYPOINT) is a required
59+
# command. It is a final command that will
60+
# be run every time a container is launched
61+
# (or restarted). Only one CMD is allowed.
62+
# If there are multiple, last one wins.
63+
CMD ["echo", "Docker", "is", "cool"]
64+
```
65+
66+
To build an image from a Dockerfile within the `enki` directory, we would do:
67+
68+
```bash
69+
docker build enki
70+
```
71+
72+
Here's an example output of building an image from the Dockerfile above:
73+
74+
Sending build context to Docker daemon 23.29MB
75+
Step 1/5 : FROM node
76+
latest: Pulling from library/node
77+
9cc2ad81d40d: Pull complete
78+
e6cb98e32a52: Pull complete
79+
ae1b8d879bad: Pull complete
80+
42cfa3699b05: Pull complete
81+
053cac798c4e: Pull complete
82+
e11ff976ff71: Pull complete
83+
6e754155fada: Pull complete
84+
32d7c2fdf415: Pull complete
85+
7acfea3f0d48: Pull complete
86+
Digest: sha256:xxx
87+
Status: Downloaded newer image for node:latest
88+
---> b18afbdfc458
89+
Step 2/5 : ENV ENKI="fun"
90+
---> Running in 3560df900d1f
91+
Removing intermediate container 3560df900d1f
92+
---> 53a94ebce927
93+
Step 3/5 : RUN echo "Learning Docker is $ENKI"
94+
---> Running in e42c0617e98e
95+
Learning Docker is fun
96+
Removing intermediate container e42c0617e98e
97+
---> 78a48477a76d
98+
Step 4/5 : EXPOSE 1234
99+
---> Running in a1df0ebf5605
100+
Removing intermediate container a1df0ebf5605
101+
---> 15a139f293da
102+
Step 5/5 : CMD ["echo", "Docker", "is", "cool"]
103+
---> Running in 3c8e7b309484
104+
Removing intermediate container 3c8e7b309484
105+
---> 8c40e5b0c9b9
106+
Successfully built 8c40e5b0c9b9
107+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
author: nem035
3+
aspects:
4+
- introduction
5+
- workout
6+
type: normal
7+
category: must-know
8+
links:
9+
- '[Best practices for writing Dockerfiles](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/){article}'
10+
- '[Dockerfile reference](https://docs.docker.com/engine/reference/builder/){documentation}'
11+
---
12+
13+
# Dockerfile Instructions
14+
15+
---
16+
17+
## Content
18+
19+
A Dockerfile must start with a `FROM` instruction which initializes a new build stage and sets the base image for subsequent instructions.
20+
21+
`FROM` may only be preceded by one or more `ARG` instructions, which represent externally passed build arguments into the image building process.
22+
23+
```bash
24+
docker build . \
25+
--build-arg EXAMPLE="this is an"
26+
```
27+
28+
A Dockerfile must contain at least the `CMD` or the `ENTRYPOINT` instruction to indicate the start command for an image.
29+
30+
The `RUN` instruction executes the commands in a new layer on top of the current image. Each successive `RUN` instruction builds a new image layer on top of the previous one.
31+
32+
Each Dockerfile instruction result gets cached.
33+
34+
When evaluating each instruction, Docker will look for an existing image in its cache that it can reuse, rather than creating a new (duplicate) image.
35+
36+
This is why re-building an image is faster than the initial build because we can re-use the cached layers.
37+
38+
Whenever we change a line in the Dockerfile, the cached result for every line after it is invalidated and has to be re-computed.
39+
40+
Due to this, a good convention is to keep the things that change the least at the top of the Dockerfile and things that change the most at the bottom.
41+
42+
---
43+
44+
## Practice
45+
46+
A Dockerfile contains instructions the Docker daemon uses to build containers.
47+
48+
???
49+
50+
* false
51+
* true
52+
53+
---
54+
55+
## Revision
56+
57+
The result of each Dockerfile instruction is cached and dependent on the cached result of the instruction before it.
58+
59+
???
60+
61+
* true
62+
* false

0 commit comments

Comments
 (0)