Why do we need this?
How it's different from VBox?
What does it solve?
Long long time ago ...
👨🏻💻
Build
Package
Deploy
oopps ... something went wrong ...
cannot find module this or that ...
... the file does not exist ...
¯\_(ツ)_/¯
It works on my machine
Containers are an abstraction at the app layer that packages code and dependencies together. Multiple containers can run on the same machine and share the OS kernel with other containers, each running as isolated processes in user space. Containers take up less space than VMs (container images are typically tens of MBs in size), can handle more applications and require fewer VMs and Operating systems.
Image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization. For example, you may build an image which is based on the ubuntu image, but installs the Apache web server and your application, as well as the configuration details needed to make your application run.
You might create your own images or you might only use those created by others and published in a registry (Docker Hub). To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it.
Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast, when compared to other virtualization technologies.
Image can be created by everyone on top of any OS, and or can be downloaded from the DockerHub
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
CMD ["node", "run.js"]
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
CMD ["node", "run.js"]
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
CMD ["node", "run.js"]
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
CMD ["node", "run.js"]
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
CMD ["node", "run.js"]
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
CMD ["node", "run.js"]
➞ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
node_created_by_me latest 5be82b526c4d 10 seconds ago 945MB
node latest 7f312bedcea0 1 day ago 944MB
run.js
#!/usr/bin/env node
const msg = `Be like the Mandalorian,
never take off your mask in public.
This is the way !\n`
console.log(msg)
dockerfile
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
CMD ["node", "run.js"]
the structure
run.js
const express = require('express')
const app = express()
const m = `...the mask ...`
const msg = `Be like the Mandalorian,
never take off your mask in public.
This is the way !\n`
const resp = `${m}${txt}`
app.get('/', (req, res) => {
res.send(resp)
})
app.listen(8080)
package.json
{
"name": "demo",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"express": "^4.17.1"
}
}
dockerfile
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
EXPOSE 8080
COPY . /usr/src/app
CMD ["yarn"]
CMD ["node", "run.js"]
Let's peek inside the container
➞ docker ps
CONTAINER ID IMAGE STATUS PORTS
80d45ee14267 node_express_demo Up 4 seconds 0.0.0.0:8080->8080/tcp
docker exec -it node_express_demo bash
-t : Allocate a pseudo-tty
-i : Keep STDIN open even if not attached
root@80d45ee14267:/usr/src/app# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 07:24 ? 00:00:00 node run.js
root 13 0 1 07:26 pts/0 00:00:00 bash
root 19 13 0 07:26 pts/0 00:00:00 ps -ef
root@80d45ee14267:/usr/src/app# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 07:24 ? 00:00:00 node run.js
root 13 0 1 07:26 pts/0 00:00:00 bash
root 19 13 0 07:26 pts/0 00:00:00 ps -ef
dockerfile
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
CMD ["node", "run.js"]
root@80d45ee14267:/usr/src/app# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 07:24 ? 00:00:00 node run.js
root 13 0 1 07:26 pts/0 00:00:00 bash
root 19 13 0 07:26 pts/0 00:00:00 ps -ef
docker exec -it node_express_demo bash
root@80d45ee14267:/usr/src/app# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 07:24 ? 00:00:00 node run.js
root 13 0 1 07:26 pts/0 00:00:00 bash
root 19 13 0 07:26 pts/0 00:00:00 ps -ef
top
top - 08:52:24 up 2 days, 16:13, 0 users, load average: 0.00, 0.00, 0.00
Tasks: 3 total, 1 running, 2 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.1 us, 0.2 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2038544 total, 153248 free, 355696 used, 1529600 buff/cache
KiB Swap: 1048572 total, 1047792 free, 780 used. 1524392 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 596380 36680 28788 S 0.0 1.8 0:00.43 node
28 root 20 0 18188 3288 2744 S 0.0 0.2 0:00.02 bash
34 root 20 0 41040 3136 2672 R 0.0 0.2 0:00.00 top
root@80d45ee14267:/usr/src/app# ls -ltra /usr/src/app/
total 40
-rw-r--r-- 1 root root 14600 Jul 26 13:50 yarn.lock
drwxr-xr-x 52 root root 4096 Jul 26 13:50 node_modules
-rw-r--r-- 1 root root 135 Jul 26 13:50 package.json
-rw-r--r-- 1 root root 123 Jul 26 13:53 Dockerfile
-rw-r--r-- 1 root root 1045 Jul 26 14:20 run.js
drwxr-xr-x 1 root root 4096 Jul 26 14:22 ..
drwxr-xr-x 1 root root 4096 Jul 26 14:22 .