[Docker] Containers & Images

Based on Udemy Course: Docker & Kubernetes: The Practical Guide

Concepts

  • A image is a sharable package that can be used in multiple containers
  • docker ps -a show all containers (running & not running)
  • docker run -it node: expose the interactive session to the local machine
  • A container contains environment + code, so we need to copy the code to the container
  • Images are read-only
  • Image layer: docker will cache the build result, if nothing changes in the image, the docker will directly use cache. When one layer is changed, all subsequent layers will also be rebuilt.
1
2
3
4
5
6
7
8
9
10
11
FROM node

WORKDIR /app

COPY . .

RUN npm install

EXPOSE 80

CMD ["node", "server.js"]

Optimize:

1
2
3
4
5
6
7
8
9
10
11
12
13
FROM node

WORKDIR /app

COPY package.json .

RUN npm install

COPY . .

EXPOSE 80

CMD ["node", "server.js"]

^ this guarantees if we don’t need to rerun npm install again if anything is changed in app.

Manage Images & Containers