Containerizing an App – the Dockerfile

Containerizing an App


  • CAPITALIZE instructions
  • <INSTRUCTION> <value>
  • FROM always first instruction
  • FROM = base image
  • Good practice to list maintainer
  • RUN = execute command and create new layer
  • COPY = copy code into image as new layer
  • Some instructions add metadata instead of layers



FROM <base image>

LABEL maintainer=”<>”

RUN apk –update nodejs nodejs-npm

COPY . /src


RUN npm install


ENTRYPOINT [“node”, “./app.js”]

Build with:

docker image build -t mywebapp .

or docker build

docker container run -d --name web1 -p 8080:8080 mywebapp

d=> detached

-p host port:container port


Under the hood

Dockerfile just some text instructions for building images.

FROM => creates a layer

LABEL => create metadata

RUN => exec commands

COPY => copy stuff in and create a new layer

WORKDIR => working directory


Build context => location of your code.

Subfolders gets included too.


Note: the docker client can be on a separate machine from the docker daemon. The context just gets sent. Also, the context could also be a git repo. E.g.

docker image build -t mywebapp https://<github-url>

Multi-stage Builds

Stage 0:

FROM node:latest AS storefront

Stage 1:

FROM maven:latest AS appserver

Stage 2:

FROM java:8-jdk-alpine AS production

COPY --from=storefront /usr/src/atsea/app/react-app/build/ .

This last instruction is key. It pulls out the layer with the build code we need from that image.

E.g. if we build with:

docker image build -t multistage .

we get:

docker image ls
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
multistage                latest              981a05b4af34        2 minutes ago       210MB
<none>                    <none>              8f1ec3e20ba0        3 minutes ago       775MB
<none>                    <none>              a631e23c40a5        9 minutes ago       832MB
node                      latest              b064644cf368        2 weeks ago         673MB
maven                     latest              1361ae58b0a4        2 weeks ago         635MB
java                      8-jdk-alpine        3fd9dd82815c        18 months ago       145MB

Here you can see how the multistageimage is a fraction of its build stage images.


Leave a Reply

Your email address will not be published. Required fields are marked *