Docker

2024-09-14

WIP

1. Basics

Dockerfile — Simple webapp
FROM alpine
LABEL maintainer="example@mail.com"
RUN apk add --update nodejs nodejs-npm
COPY . /src
WORKDIR /src
RUN npm install
EXPOSE 8080
ENTRYPOINT ["node", "./app.js"]
Example 1. Keywords
  • FROM: The base image to use

  • LABEL: Image’s metadata

  • RUN: Command to run inside the image at build time

  • COPY: Copy files from <local> to <image>

  • WORKDIR: Change the working directory

    • Note that if the directory does not exists, it will be created

  • EXPOSE: The port that will be exposed

    • available to the host/other images

  • ENTRYPOINT: The command that will be run at the end

    • The actual app; all the previous commands are just a preperation for this command/section

Dockerfile — Build image
docker buildx build -f Dockerfile -t test-name .
[+] Building 35.5s (10/10) FINISHED                                                  docker:default
 => [internal] load build definition from Dockerfile                                           0.0s
 => => transferring dockerfile: 363B                                                           0.0s
 => [internal] load .dockerignore                                                              0.0s
 => => transferring context: 2B                                                                0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                               0.0s
 => [1/5] FROM docker.io/library/alpine                                                        0.0s
 => [internal] load build context                                                              0.0s
 => => transferring context: 36.77kB                                                           0.0s
 => [2/5] RUN apk add --update nodejs npm curl                                                 9.2s
 => [3/5] COPY . /src                                                                          0.0s
 => [4/5] WORKDIR /src                                                                         0.0s
 => [5/5] RUN npm install                                                                     26.0s
 => exporting to image                                                                         0.2s
 => => exporting layers                                                                        0.2s
 => => writing image sha256:1825da98372a937a095e523f71f59dcd6a4c41f6f0f573ed1bfe118fae8d50ed   0.0s
 => => naming to docker.io/library/test                                                        0.0s
Docker CLI — Get list of images
docker image ls
REPOSITORY             TAG               IMAGE ID       CREATED         SIZE
test                   latest            1825da98372a   6 minutes ago   95.4MB
Docker CLI — Run an image/container
docker container run -d --name web1 --publish 8080:8080 test:latest
63e334d59c7d937c99f6cbe6e5628c3b180ab26cb4b3f7e3d2cdd007cfd78f37
Docker CLI — Get list of running images/containers
docker ps
CONTAINER ID   IMAGE         COMMAND           CREATED          STATUS          PORTS                                       NAMES
63e334d59c7d   test:latest   "node ./app.js"   14 seconds ago   Up 13 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   web1

2. Multi-stage Dockerfile

FROM golang:alpine as builder
LABEL maintainer="Hossein Esmailzadeh <hosteam01@gmail.com>"

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download
COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

## runner
FROM alpine:latest

RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/main .

EXPOSE 9090

CMD ["./main"]