👩🏻‍💻

Docker for devs

$ whoami

abernier (LinkedIn/Behance - Malt - Ironhack)

       ebdev bootcamp

full/part-time

www

learn more

IRON
HACK

INSTALL

$ whatis docker

$ help help

pwd, ls, cd, mkdir, touch

TAB

man, --help

$ uname -a

Darwin mba3.local 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar  4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64

$ 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 19.03.8, build afacb8b

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

v14.4.0

$ docker run node --version

v10.21.0

$ docker run node:10-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 47b9c57dfe0f 4.19.76-linuxkit #1 SMP Fri Apr 3 15:53:26 UTC 2020 x86_64 GNU/Linux

$ docker run node uname -a

😅

npx: installed 10 in 3.559s
 _____
< meh >
 -----
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

$ docker run node npx cowsay meh

$ docker run node npx grip 👋Hey!

Fun enough,

👷🏼‍♂️

Let's   $ docker build  
something (fun)

$ cat <<EOF >Dockerfile
FROM ubuntu:18.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 $_
$ npx create-react-app .
​$ code .
​1
2
3
4
5
6
7
8
9
10
11
12

Dockerfile

FROM node:14-alpine
  











 
$  mkdir -p ~/tmp/dockercra && cd $_
$ npx create-react-app .
​$ code . 
$ docker build -t dockercra .
Sending build context to Docker daemon  171.5MB
Step 1/1 : FROM node:14-alpine
 ---> 72eea7c426fc
Successfully built 72eea7c426fc
Successfully tagged dockercra:latest

Juste pour essayer

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

Dockerfile

FROM node:14-alpine
  
WORKDIR /app










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

Dockerfile

FROM node:14-alpine
  
WORKDIR /app




COPY . ./





 

.dockerignore

​1


# 🙈 Ignore everything

*


#

# ☝🏻 Except

#

!/package.json

!/yarn.lock

!/src

$  mkdir -p ~/tmp/dockercra && cd $_
$ npx create-react-app .
​$ code . 
$ docker build -t dockercra .
Sending build context to Docker daemon  821.2kB
Step 1/3 : FROM node:14-alpine
 ---> 72eea7c426fc
Step 2/3 : WORKDIR /app
 ---> Running in cf02a3d2db1e
Removing intermediate container cf02a3d2db1e
 ---> dceadd89d97b
Step 3/3 : COPY . ./
 ---> fd7cfdab2cd5
Successfully built fd7cfdab2cd5
Successfully tagged dockercra:latest

Toujours pour essayer

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

Dockerfile

FROM node:14-alpine
  
WORKDIR /app

COPY package.json yarn.lock ./
RUN yarn install

COPY . ./  





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

Dockerfile

FROM node:14-alpine
  
WORKDIR /app

COPY package.json yarn.lock ./
RUN yarn install

COPY . ./  
RUN yarn build




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

Dockerfile

FROM node:14-alpine
  
WORKDIR /app

COPY package.json yarn.lock ./
RUN yarn install

COPY . ./  
RUN yarn build

EXPOSE 5000
CMD ["npx", "serve", "-s", "build"]

 
$ docker build -t dockercra .
$ docker images
$ docker run -it --rm -p 5000:5000 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:14-alpine
  
WORKDIR /app

COPY package.json yarn.lock ./
RUN yarn install

COPY . ./  

EXPOSE 3000
CMD ["yarn", "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:14-alpine AS base

WORKDIR /app

COPY package.json yarn.lock ./
RUN yarn install

COPY . ./  

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

FROM base AS server
RUN yarn build
EXPOSE 5000
CMD ["npx", "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

👩🏻‍💻

❤️