🧑🏽‍💻

Docker for devs

$ whoami

abernier (LinkedIn/Behance - Ironhack)

INSTALL

$ brew install --cask docker

$ whatis docker

$ help help

pwd, ls, cd, mkdir, touch

TAB

man, --help

$ uname -a

Darwin mbp2.local 21.3.0 Darwin Kernel Version 21.3.0: Wed Jan  5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_ARM64_T6000 

$ which ls

/bin/ls

$ ls -l /bin/ls
-rwxr-xr-x  1 root  wheel  51888 Apr  6 21:52 /bin/ls

$ cat /etc/hosts

127.0.0.1    localhost

...

$ echo "Salut"

Salut

$ echo "Bonjour $USER !" > helloyou.txt


$ cat helloyou.txt
Bonjour abernier !

$ echo "Ça va bien ?" >> helloyou.txt


$ cat helloyou.txt
Bonjour abernier !
Ça va bien ?

$ cat >journal.txt

Cher journal,

Je t'écris ce matin depuis ma ligne de commande.

^D

$ more journal.txt

Cher journal,

Je t'écris ce matin depuis ma ligne de commande.

$ cat <<EOF >poem.txt

Lorem ipsum dolor sit amet
Consectetur adipiscing elit
Suspendisse at iaculis justo
nec consectetur dolor.
EOF

ctrl+E, ctrl+A, ctrl+U, ctrl+C

history, UP, DOWN, ctrl+r

chmod, chown, grep, find, ps, kill, ...

exit

$ docker --version

Docker version 20.10.16, build aa7e414

IMAGE

$ docker build

Dockerfile

1  FROM golang:1.11-alpine
2 
3  RUN mkdir /app
4  COPY main.go /app
5  WORKDIR /app
6  
7  RUN go build -o main .
8  
9  EXPOSE 8080 8888
10 USER 1001
11 
12 ENTRYPOINT ["/app/main"]

Dockerfile

$ docker build

$ docker images

$ docker run

$ docker run

Docker registry

 $ docker push myimage

 $ docker login

$ docker pull node

v18.3.0

$ docker run node --version

v16.15.1

$ docker run node:16-alpine --version

Usage: node [options] [ script.js ] [arguments]
...
-p, --print [...]           evaluate script and print result

$ docker run node --help

3.141592653589793

$ docker run node -p "Math.PI"

😬

Linux 4d78187ec8d3 5.10.104-linuxkit #1 SMP PREEMPT Thu Mar 17 17:05:54 UTC 2022 aarch64 GNU/Linux

$ docker run node uname -a

😅

 _____
< meh >
 -----
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

$ docker run node npm x cowsay meh

$ docker run node npm x qrip 👋Hey!

Fun enough,

👷🏼‍♂️

Let's   $ docker build  
something (fun)

$ cat <<EOF >Dockerfile
FROM ubuntu:22.04
RUN apt-get update && \
    apt-get install -y nyancat
CMD ["nyancat"]
EOF

$ docker build -t nyancat .
$ docker images
 $ mkdir -p ~/tmp && cd $_

💿

$ docker run -it nyancat

😻

$ docker ps
 


CONTAINER ID

IMAGE

COMMAND

NAMES

84a1a868776d

nyancat

"nyancat"

relaxed_jang

Dans un 2e terminal

$ docker exec 84a1a868776d uname
Linux

Toujours dans notre 2e terminal

$ docker exec -it 84a1a868776d top

 

top - 09:53:13 up 17:40,  0 users,  load average: 0.03, 0.30, 1.17
Tasks:   2 total,   2 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.2 us,  2.8 sy,  0.0 ni, 95.7 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2047140 total,   893648 free,   333412 used,   820080 buff/cache
KiB Swap:  1048572 total,   949780 free,    98792 used.  1551004 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                              
    1 root      20   0    4556    780    712 R   0.7  0.0   0:03.93 nyancat                                                                                              
   65 root      20   0   36588   3176   2748 R   0.0  0.2   0:00.04 top

Toujours dans notre 2e terminal

🎙

ON AIR

$ docker kill 84a1a868776d
$ docker ps

 

CONTAINER ID

IMAGE

COMMAND

NAMES

Toujours dans notre 2e terminal

🔫

$ docker run -it nyancat /bin/sh
# nyancat --help
Terminal Nyancat
usage: nyancat [-hitn] [-f frames]
...

Dans notre premier terminal

# exit
$ 

🕳

🖐🏻

dockercra

React + Docker

Step 1 : build static app

$ mkdir -p ~/tmp/dockercra && cd $_
$ npm x create-react-app .
​$ code .
​1
2
3
4
5
6
7
8
9
10
11
12

Dockerfile

FROM node:18-alpine
  











 
$ docker build --progress=plain -t dockercra .

#6 [internal] load build context

Juste pour essayer

​1
2
3
4
5
6
7
8
9
10
11
12

Dockerfile

FROM node:18-alpine
  
WORKDIR /app










 
​1
2
3
4
5
6
7
8
9
10
11
12

Dockerfile

FROM node:18-alpine
  
WORKDIR /app




COPY . ./





 

.dockerignore

​1


node_modules

$ docker build --progress=plain -t dockercra .

#6 [internal] load build context
#6 transferring context: 329B 0.5s done
#6 DONE 0.5s

Toujours pour essayer

​1
2
3
4
5
6
7
8
9
10
11
12

Dockerfile

FROM node:18-alpine
  
WORKDIR /app

COPY package.json package-lock.json ./
RUN npm ci

COPY . ./  





 
​1
2
3
4
5
6
7
8
9
10
11
12

Dockerfile

FROM node:18-alpine
  
WORKDIR /app

COPY package.json package-lock.json ./
RUN npm ci

COPY . ./  
RUN npm run build




 
​1
2
3
4
5
6
7
8
9
10
11
12

Dockerfile

FROM node:18-alpine
  
WORKDIR /app

COPY package.json package-lock.json ./
RUN npm ci

COPY . ./  
RUN npm run build

EXPOSE 3000
CMD ["npx", "-y", "serve", "-s", "build"]

 
$ docker build -t dockercra .
$ docker images
$ docker run -it --rm -p 3000:3000 dockercra 
$ docker ps
$ docker exec -it a999c29eed5a /bin/sh
# ls
# exit

dans un autre terminal :

Deploy

on Heroku

👉 Heroku doc

$ heroku login
$ heroku apps:create dockercraaa --stack=container

$ docker login --username=_ --password=$(heroku auth:token) registry.heroku.com
$ docker tag dockercra registry.heroku.com/dockercraaa/web
$ docker push registry.heroku.com/dockercraaa/web

$ heroku container:release web

🚀

Step 2 : build dev 🔥HRM app

Dockerfile.dev

​1
2
3
4
5
6
7
8
9
10
11


FROM node:18-alpine
  
WORKDIR /app

COPY package.json package-lock.json ./
RUN npm ci

COPY . ./  

EXPOSE 3000
CMD ["npm", "start"]

 
$ docker build -t dockercra -f Dockerfile.dev . 
$ docker run -it --rm \
    --name dockercra \
    -p 3000:3000 \
    dockercra
$ open http://localhost:3000
$ docker exec -it dockercra /bin/sh
# echo "console.log('👋')" >>src/App.js

🔥

$ docker run -it --rm \
    --name dockercra \
    -v "$PWD":/app \
    -v /app/node_modules \
    -p 3000:3000 \
    dockercra

$ # edit `src/App.js` and see hot-reload

🥵

Step 3 : multi-stages

Dockerfile

​1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
FROM node:18-alpine AS base

WORKDIR /app

COPY package.json package-lock.json ./
RUN npm ci

COPY . ./  

FROM base AS dev
EXPOSE 3000
CMD ["npm", "start"]

FROM base AS server
RUN npm run build
EXPOSE 5000
CMD ["npm", "x", "serve", "-s", "build"]
$ docker build --target dev -t dockercra .
$ docker run -it --rm \
    -v "$PWD":/app \
    -v /app/node_modules \
    -p 3000:3000 \
    dockercra
$ docker build --target server -t dockercra .
$ docker run -it --rm \
    -p 5000:5000 \
    dockercra

THE END

👩🏻‍💻

❤️

Docker

By Antoine BERNIER