|
| 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 | +``` |
0 commit comments