2021年12月14日 星期二

Docker on Ubuntu 2021

Docker on Ubuntu 2021

https://cwhu.medium.com/docker-tutorial-101-c3808b899ac6


Manual

cd docker-demo-app
npm install  # will read package.json file, install dependencies to node_modules
npm start # will read package.json file, display name@version, run script of "node index.js"
firefox 127.0.0.1:3000


Dockerfile

FROM node:10.15.3-alpine
WORKDIR /app
ADD . /app
RUN npm install
EXPOSE 3000
CMD node index.js

FROM means to load nodejs
WORKDIR creates /app under docker
ADD adds current directory (of Dockerfile)'s files to /app
RUN npm install
EXPOSE 3000 port from docker container to external
CME node index.js starts the http server

remember to add a .dockerignore besides Dockerfile that contains "node_modules" so that when ADD the binaries will not be in the image file, or else the image will be big.


Build docker

sudo docker build . -t docker-demo-app  # This builds an image with a tag "docker-demo-app"
sudo docker images  # list all images in all repos. the tag "docker-demo-app" appears in REPOSITORY

REPOSITORY        TAG              IMAGE ID       CREATED          SIZE
docker-demo-app   latest           af571dd0e6cb   10 minutes ago   73.4MB


Run, check, stop container from image

sudo docker run -p 3000:3000 -it af571dd0e6cb
listening on port 3000  # this is from index.js console.log("listen on port 3000")

# possible error starting userland proxy: listen tcp4 0.0.0.0:3000: bind: address already in use.
# now CTRL+C will not work

sudo docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                       NAMES

49cf1fee8d4d   af571dd0e6cb   "/bin/sh -c 'node in…"   4 minutes ago   Up 4 minutes   0.0.0.0:3000->3000/tcp, :::3000->3000/tcp   eager_northcutt

sudo docker stop 49cf1fee8d4d


docker run

sudo docker run -p 3000:3000 -it <IMAGE ID>  # is equivalent to below
sudo docker run -p 3000:3000 -it d18ba1dfd218 echo 123  # 123
sudo docker run -p 3000:3000 -it d18ba1dfd218 ls  # Dockerfile node_modules index.js 3 more files
sudo docker run -p 3000:3000 -it d18ba1dfd218 pwd  # /app
sudo docker run -p 3000:3000 -it d18ba1dfd218 ps  # PID 1 USER root TiIME 0:00 COMMAND ps



More notes here (read only)


# 01 Docker Helle World



install curl, add docker repo

=============================

https://www.runoob.com/docker/ubuntu-docker-install.html

curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

sudo apt-key fingerprint 0EBFCD88

sudo add-apt-repository \

   "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \

  $(lsb_release -cs) \

  stable"


test docker

===========

sudo docker run hello-world

sudo docker run ubuntu:15.10 /bin/echo "Hello world"

sudo docker run ubunty:15.10 /bin/bash  # run then exit

sudo docker run -it ubuntu:15.10 /bin/bash

# -i for interactive STDIN

# -t for terminal

/# cat /proc/version  # Linux version 5.11.0

# exit or CTRL+D to quit


backend mode

============

sudo docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep1; done"

d65680c3740dff7cd8ba30fa4e476432bc5a3918a96a98b589ef99e6fc90d47d

# this is a container id 64 bytes


sudo docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS     NAMES

d65680c3740d   ubuntu:15.10   "/bin/sh -c 'while t…"   About a minute ago   Up About a minute             frosty_kepler

# STATUS can be 7 of Union[Up,Created,Restarting,Removing,Paused,Exited,Dead]


sudo docker logs d65680c3740dff7

sudo docker logs d65680c3740dff

sudo docker logs d65680c3740df

sudo docker logs d65680c3740d

sudo docker logs d656

sudo docker logs d65

sudo docker logs d6

sudo docker logs frosty_kepler

sudo docker stop frosty_kepler



# 02 Container


commands

========

sudo docker images

sudo docker run -it ubuntu:14.04 /bin/bash

sudo docker pull ubuntu:13.10

sudo docker search httpd

sudo docker pull httpd

sudo docker run httpd

sudo docker rmi httpd

sudo docker images

sudo docker rmi httpd



docker commit

=============

commit a changed docker image as another image


sudo docker run -it ubuntu:15.10 /bin/bash

apt-get --version  #  apt 1.0.10.2ubuntu1

root@44ee34f316fa

apt-get update

exit


sudo docker container ls -a | grep 44ee34f316fa

44ee34f316fa   ubuntu:15.10   "/bin/bash"              5 minutes ago       Exited (0) 4 minutes ago                                                     vibrant_spence


sudo docker commit -m="has update" -a="runoob" 44ee34f316fa runoob/ubuntu:v2

sudo docker commit -m="has update" -a="runoob" 44ee34f316fa bunbun:v3

sudo docker commit -m="has update" -a="runoob" 44ee34f316fa bun/gun:v3

REPOSITORY        TAG              IMAGE ID       CREATED          SIZE

bun/gun           v3               fb4c50ff7da4   10 seconds ago   137MB

bunbun            v3               184d7f91f982   14 seconds ago   137MB

runoob/ubuntu     v2               ab7bc95f9950   18 seconds ago   137MB


sudo docker rmi runoob/ubuntu:v2

sudo docker rmi bunbun:v3

sudo docker rmi bun/gun:v3



# 03 Dockerfile, docker build


Dockerfile

==========


vim Dockerfile


FROM    centos:6.7

MAINTAINER      Fisher "fisher@sudops.com"

RUN     /bin/echo 'root:123456' |chpasswd

RUN     useradd runoob

RUN     /bin/echo 'runoob:123456' |chpasswd

RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local

EXPOSE  22

EXPOSE  80

CMD     /usr/sbin/sshd -D


docker build

============


sudo docker build -t runoob/centos:6.7 .

// Tag an image (-t)

// Dockerfile from .


Step 1/9 : FROM    centos:6.7

6.7: Pulling from library/centos

cbddbc0189a0: Pull complete 

Digest: sha256:4c952fc7d30ed134109c769387313ab864711d1bd8b4660017f9d27243622df1

Status: Downloaded newer image for centos:6.7

 ---> 9f1de3c6ad53

Step 2/9 : MAINTAINER      Fisher "fisher@sudops.com"

 ---> Running in c3cb2314855a

Removing intermediate container c3cb2314855a

 ---> 0b636d52aae7

Step 3/9 : RUN     /bin/echo 'root:123456' |chpasswd

 ---> Running in ed30059f2f19

Removing intermediate container ed30059f2f19

 ---> 974b0c91f900

Step 4/9 : RUN     useradd runoob

 ---> Running in 53590233c07a

Removing intermediate container 53590233c07a

 ---> d6f24b904317

Step 5/9 : RUN     /bin/echo 'runoob:123456' |chpasswd

 ---> Running in b0fe55fdeea9

Removing intermediate container b0fe55fdeea9

 ---> 05dd496ca42d

Step 6/9 : RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local

 ---> Running in 7b67eb2a5f8e

Removing intermediate container 7b67eb2a5f8e

 ---> 803ed0f06699

Step 7/9 : EXPOSE  22

 ---> Running in 8139bbe1ad71

Removing intermediate container 8139bbe1ad71

 ---> 8d2db7a61261

Step 8/9 : EXPOSE  80

 ---> Running in 75b4e8f5d60f

Removing intermediate container 75b4e8f5d60f

 ---> ed01cf55131e

Step 9/9 : CMD     /usr/sbin/sshd -D

 ---> Running in 933cd65d3946

Removing intermediate container 933cd65d3946

 ---> 5bdb04f9d136

Successfully built 5bdb04f9d136

Successfully tagged runoob/centos:6.7




docker tag

==========


sudo docker images

REPOSITORY        TAG              IMAGE ID       CREATED         SIZE

runoob/centos     6.7              5bdb04f9d136   3 minutes ago   191MB


docker tag 5bdb04f9d136 runoob/centos:dev

docker tag 5bdb04f9d136 runoob/centos:bunbun


REPOSITORY        TAG              IMAGE ID       CREATED         SIZE

runoob/centos     6.7              5bdb04f9d136   4 minutes ago   191MB

runoob/centos     bunbun           5bdb04f9d136   4 minutes ago   191MB

runoob/centos     dev              5bdb04f9d136   4 minutes ago   191MB



# 04 docker port


docker run -d -P training/webapp python app.py

# latest: Pulling from training/webapp

# -d Detached mode

# -P docker run [OPTIONS] --publish-all , -P    Publish all exposed ports to random ports

# Docker identifies every port from "Dockerfile EXPOSE" and "--expose 8080"

# port mapped to epehmeral port range usually 32768 to 61000


sudo docker inspect training/webapp | vim - /Exposed

"ExposedPorts": {"5000/tcp": {}}


sudo docker inspect training/webapp | grep tcp

"5000/tcp": {}

"5000/tcp": {}


sudo docker ps

b06e5902f88b 0.0.0.0:49153->5000/tcp, :::49153->5000/tcp

# docker mapped host port 49153 to container port 5000


telnet 127.0.0.1 49153

Connected to 127.0.0.1


telnet 127.0.0.1 5000

Connection refused


firefox 127.0.0.1:49153

Hello world!


sudo docker run -d -p 5000:5000 training/webapp python app.py

sudo docker ps

1a93c1aa7e8d 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp

b06e5902f88b 0.0.0.0:49153->5000/tcp, :::49153->5000/tcp


sudo docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py

sudo docker ps

decf92a9d49c 127.0.0.1:5001->5000/tcp

1a93c1aa7e8d 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp

b06e5902f88b 0.0.0.0:49153->5000/tcp, :::49153->5000/tcp


sudo docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py

acd7d2f46eb7 5000/tcp, 127.0.0.1:5000->5000/udp

decf92a9d49c 127.0.0.1:5001->5000/tcp

1a93c1aa7e8d 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp

b06e5902f88b 0.0.0.0:49153->5000/tcp, :::49153->5000/tcp


sudo docker port strange_mclaren

sudo docker port b06e5902f88b

sudo docker port b06

5000/tcp -> 0.0.0.0:49153

5000/tcp -> :::49153


docker network

==============

sudo docker run -d -P --name runoob training/webapp python app.py

sudo docker ps -l  # latest

sudo docker network create -d bridge test-net

# bridge for bridge, overlay for swarm mode


sudo docker run -itd --name test1 --network test-net ubuntu /bin/bash

sudo docker run -itd --name test2 --network test-net ubuntu /bin/bash


sudo docker exec -it test1 /bin/bash

ping test2

command not found

apt-get update

apt install iputils-ping

ping test2

64 bytes from 87b91bb68862 (172.18.0.2): icmp_seq=1 ttl=64 time=0.031 ms


DNS

===


To be continued












End


沒有留言:

張貼留言

2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...