Skip to content

Latest commit

 

History

History
125 lines (94 loc) · 2.85 KB

docker-handson-2.md

File metadata and controls

125 lines (94 loc) · 2.85 KB

Writing the first Dockerfile

$ mkdir myimage
$ cd myimage
$ vim Dockerfile # use whatever editor you want

Dockerfile:

FROM ubuntu
RUN apt-get update && apt-get -y install figlet
  • FROMindicates the base image for our build
  • RUNwill by executed by Docker during the build
    • must be non-interactive (no input can be provided to Docker during the build)

Build it:

$ docker build -t figlet .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM alpine
 ---> cc0abc535e36
Step 2/2 : RUN apk add figlet
 ---> Running in 7fa90af7d37b
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
(1/1) Installing figlet (2.2.5-r0)
Executing busybox-1.31.1-r8.trigger
OK: 6 MiB in 15 packages
Removing intermediate container 7fa90af7d37b
 ---> 75ab3d082245
Successfully built 75ab3d082245
Successfully tagged figlet:latest
  • -t indicates the tag to apply to the image
  • . indicates the location of the build context
  • the build context is the .directory givent o docker build
  • it is sent (as an archive) by the Docker blient to the Docker daemon
  • this allows to use a remote machine to build using local files

Run it:

$ docker run -ti figlet
/# figlet hello

Caching

  • if you run the same build again, it will be instantaneous
  • Docker takes a snapshot of the resulting image
  • before executing a step, Docker checks if it has already built the same sequence
    • RUN apk add figlet cowsay is different from RUN apk add figlet cowsay
  • rebuilds can be foreced with --no-cacheflag

Defining a command

When people run our container, we want to greet them with a nice hello message:

figlet "hello stranger!"

Dockerfile:

FROM alpine
RUN apk add figlet
CMD figlet "hello stranger!"
  • CMDdefines a default command to run when none is given
  • it can appear at any point in the file
  • each CMDwill replace and override the previous one
  • as a result, while you can have multiple CMDlines, it is useless
$ docker build -t figlet .
$ docker run figlet

Adding Files to the Container

Create a textfile, containing a message:

echo "Cowabunga!" > message

Add COPY to your dockerfile and adjust the `CMD

FROM alpine
RUN apk add figlet
COPY ./message .
CMD figlet $(cat message)
  • COPYwill copy files into the container

Some Details:

  • you can COPYwhole directories recursively
  • older Dockerfiles also have the ADD instruction: it is similar but can automatically extract archives

Mounting a directory

Dockerfile:

FROM alpine
RUN apk add figlet
CMD figlet $(cat message)
docker run -d -v $(pwd)/message:/message figlet
  • -v configures the host directory which should be mounted inside the container