LSX
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# Docker 把Linux變成Router, root 權限變更 /proc/sys/net/ipv4/ip_forward `echo 1 > /proc/sys/net/ipv4/ip_forward` ## 安裝docker ### Ubuntu 參考: [Install Docker Engine on Ubuntu](https://docs.docker.com/engine/install/ubuntu/) * 如有舊版,移除步驟 ``` for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done ``` * 安裝 ```= # Add Docker's official GPG key: sudo apt-get update sudo apt-get install ca-certificates curl gnupg sudo install -m 0755 -d /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg sudo chmod a+r /etc/apt/keyrings/docker.gpg # Add the repository to Apt sources: echo \ "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update # 最新版docker sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin ``` * 啟動 ``` sudo systemctl start docker sudo systemctl enable docker ``` * 查看版本 ``` docker --version ``` * 測試 ``` sudo docker run hello-world ``` ![image](https://hackmd.io/_uploads/SkujYKN46.png) ### CentOS 參考: [Install Docker Engine on CentOS](https://docs.docker.com/engine/install/centos/) * 如有舊版,移除步驟 ``` sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine ``` * 安裝 ```= sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin ``` * 啟動 ``` sudo systemctl start docker ``` * 查看版本 ``` docker --version ``` * 測試 ``` sudo docker run hello-world ``` ![image](https://hackmd.io/_uploads/ryhk2wVNT.png) --- ## 安裝鏡像&容器 (install image&container) * 安裝鏡像 [docker_centos](https://hub.docker.com/_/centos/tags) `sudo docker pull [image Name]` 個人選擇 7.9.2009 版本 **安裝前須注意系統架構是否支援** `docker pull centos:centos7.9.2009` * 使用 ```dockerfile= [user@centos-vm3 ~]$ sudo docker run centos:centos7.9.2009 echo "hello world" hello world ``` --- ## 刪除容器 (delete container) ```dockerfile= # 查看全部容器 [user@centos-vm3 ~]$ sudo docker ps # 執行中的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [user@centos-vm3 ~]$ sudo docker ps -a # 包含執行完的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 40f9d68ee878 centos:centos7.9.2009 "echo 'hello world'" 13 minutes ago Exited (0) 13 minutes ago optimistic_goldstine eb161a9589ef hello-world "/hello" 19 minutes ago Exited (0) 19 minutes ago determined_noyce # 刪除容器 [user@centos-vm3 ~]$ sudo docker rm optimistic_goldstine # 用 "NAMES" 刪除 optimistic_goldstine [user@centos-vm3 ~]$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eb161a9589ef hello-world "/hello" 22 minutes ago Exited (0) 22 minutes ago determined_noyce [user@centos-vm3 ~]$ sudo docker rm eb161a9589ef #用 "CONTAINER ID" 刪除 (可以只打ID的前幾個代號) eb161a9589ef [user@centos-vm3 ~]$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES # 刪除多個容器 [user@centos-vm3 ~]$ sudo docker ps -a # 刪除前確認 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a230d060b9b3 busybox:0.1 "sh" 6 minutes ago Up 6 minutes b2 1fafafe132ec busybox "sh" 19 minutes ago Exited (0) 6 minutes ago b1 de9f9a8221d4 busybox "sh" 24 minutes ago Exited (0) 19 minutes ago keen_matsumoto c212f43e6813 busybox "sh" 50 minutes ago Exited (0) 20 minutes ago keen_blackburn [user@centos-vm3 ~]$ sudo docker ps -a -q # 顯示ID a230d060b9b3 1fafafe132ec de9f9a8221d4 c212f43e6813 [user@centos-vm3 ~]$ sudo docker rm `sudo docker ps -aq` 1fafafe132ec de9f9a8221d4 c212f43e6813 Error response from daemon: You cannot remove ...... attempting removal or force remove [user@centos-vm3 ~]$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a230d060b9b3 busybox:0.1 "sh" 10 minutes ago Up 10 minutes b2 # 連運作中的container 一併刪除 [user@centos-vm3 ~]$ sudo docker rm -f `sudo docker ps -aq` a230d060b9b3 [user@centos-vm3 ~]$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ``` --- ## 刪除鏡像 (delete image) ```dockerfile= [user@centos-vm3 ~]$ sudo docker images # 查看安裝的鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 9c7a54a9a43c 6 months ago 13.3kB centos centos7.9.2009 eeb6ee3f44bd 2 years ago 204MB [user@centos-vm3 ~]$ sudo docker rmi 9c7a # 用ID刪除 Untagged: hello-world:latest Untagged: hello-world@sha256:c79d06dfdfd3d3eb04cafd0dc2bacab0992ebc243e083cabe208bac4dd7759e0 Deleted: sha256:9c7a54a9a43cca047013b82af109fe963fde787f63f9e016fdc3384500c2823d Deleted: sha256:01bb4fce3eb1b56b05adf99504dafd31907a5aadac736e36b27595c8b92f07f1 [user@centos-vm3 ~]$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos centos7.9.2009 eeb6ee3f44bd 2 years ago 204MB ``` --- ## docker_busybox ```dockerfile= # terminal 1 [user@centos-vm3 ~]$ sudo docker pull busybox Using default tag: latest # 不加版本就抓最新的 latest: Pulling from library/busybox 3f4d90098f5b: Pull complete Digest: sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79 Status: Downloaded newer image for busybox:latest docker.io/library/busybox:latest # -i: 允許向容器輸入 -t: 提供終端界面--這兩者一起使用時,可以在容器內進行交互。 [user@centos-vm3 ~]$ sudo docker run -it busybox sh / # # terminal 2 [user@centos-vm3 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c212f43e6813 busybox "sh" 3 minutes ago Up 3 minutes keen_blackburn ``` **執行中的image(STATUS: up)** ![image](https://hackmd.io/_uploads/BkY6wQiV6.png) **ip check** ![image](https://hackmd.io/_uploads/BkZLdQoEa.png) --- ## back_up image (local) `sudo docker commit -h` ```dockerfile= # terminal 1 [user@centos-vm3 ~]$ sudo docker run -it --name b1 busybox sh / # # terminal 2 [user@centos-vm3 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1fafafe132ec busybox "sh" 36 seconds ago Up 35 seconds b1 ``` ![image](https://hackmd.io/_uploads/rkl8RmjEa.png) ```dockerfile= / # ls bin etc lib proc sys usr dev home lib64 root tmp var / # echo "hi" > hi.txt / # ls bin etc home lib64 root tmp var dev hi.txt lib proc sys usr / # # 如果這時候直接 `exit` =沒存檔直接離開 ``` **儲存image** ```dockerfile= # terminal # sudo docker commit [CONTAINER(Name/ID)] [REPOSITORY:[TAG]] [user@centos-vm3 ~]$ sudo docker commit b1 busybox:0.1 # 這才是完整的ID sha256:33943135d40e0d70d36cd0a0f0ca1c80b9623669678217cc8e2db3e5c839bc3d [user@centos-vm3 ~]$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE busybox 0.1 33943135d40e 7 seconds ago 4.26MB busybox latest a416a98b71e2 4 months ago 4.26MB centos centos7.9.2009 eeb6ee3f44bd 2 years ago 204MB ``` **開啟image** ```dockerfile= # terminal in busybox / # exit [user@centos-vm3 ~]$ sudo docker run -it --name b2 busybox:0.1 sh / # ls bin etc home lib64 root tmp var dev hi.txt lib proc sys usr / # ``` --- ## docker python [python](https://hub.docker.com/_/python/tags) ```dockerfile= # 本地沒有python [user@centos-vm3 ~]$ python3 bash: python3: command not found... Similar command is: 'python' # 在 docker 安裝 python [user@centos-vm3 ~]$ sudo docker pull python:3.9.18-slim # 本地建立目錄 [user@centos-vm3 ~]$ mkdir -p docker_python [user@centos-vm3 ~]$ cd docker_python/ [user@centos-vm3 docker_python]$ vim hello.py [user@centos-vm3 docker_python]$ cat hello.py print("Hello World!") # 開啟容器 # "-v"是做資料夾的映射,把本地的/home/user/docker_python/ 與 容器的/tmp 連結 # 執行容器python:3.9.18-slim 容器執行bash [user@centos-vm3 ~]$ sudo docker run -it -v /home/user/docker_python/:/tmp python:3.9.18-slim bash # python:3.9.18-slim_bash root@8f8a0d73ee69:/# ls bin dev home lib32 libx32 mnt proc run srv tmp var boot etc lib lib64 media opt root sbin sys usr root@8f8a0d73ee69:/# cd /tmp root@8f8a0d73ee69:/tmp# ls hello.py root@8f8a0d73ee69:/tmp# python hello.py Hello World! ``` --- ## 進入執行中的容器 exec ```dockerfile [user@centos-vm3 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bfe569db8cd9 centos:web "/usr/sbin/apachectl…" 51 minutes ago Up 51 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp web [user@centos-vm3 ~]$ sudo docker exec -it bfe bash [root@bfe569db8cd9 /]# ``` --- ## docker web_server & port_building * port building ![photo_2023-11-22_18-47-15](https://hackmd.io/_uploads/HyOH2LsVa.jpg) ```dockerfile= # -d: 讓容器在背景執行並顯示容器ID [user@centos-vm3 ~]$ sudo docker run --name web -d -p 8080:80 centos:web /usr/sbin/apachectl -DFOREGROUND bfe569db8cd9dea6297e82ecbc032f1df35bd05a1045cdc4130d3d07f5539de7 [user@centos-vm3 ~]$ sudo docker ps # 因為啟動著 web server 所以不會直接結束 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bfe569db8cd9 centos:web "/usr/sbin/apachectl…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp web ``` **測試** ```bash= [user@centos-vm3 ~]$ ip a s 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:bc:ca:63 brd ff:ff:ff:ff:ff:ff inet 192.168.68.111/24 brd 192.168.68.255 scope global noprefixroute ens33 ``` ![image](https://hackmd.io/_uploads/Hyk31PsVp.png) * 變更網頁 1. 進入到 container(容器) 修改 ```bash [root@bfe569db8cd9 /]# cd /var/www/html/ [root@bfe569db8cd9 html]# echo "hi" > hi.htm [root@bfe569db8cd9 html]# ls hi.htm ``` :::warning 不建議用這種方法,如果沒有先儲存就離開東西就白做了 ::: 2. 在 terminal 啟動 web server ```bash= # 在本地先建立目錄並寫入 [root@centos-vm3 user]# mkdir -p myweb [root@centos-vm3 user]# cd ./myweb/ [root@centos-vm3 myweb]# vim hi.htm [root@centos-vm3 myweb]# cat hi.htm hihihi # 參數跟相關指令下完才輪到 image ex: docker run -d -p -v ... centos:web ... [root@centos-vm3 myweb]# docker run -d -p 8080:80 -v /home/user/myweb/:/var/www/html centos:web /usr/sbin/apachectl -DFOREGROUND b7b727e0c09ced9902e84d121c1fac50d4086daa24d2b0381c62c9081007a5db [root@centos-vm3 myweb]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b7b727e0c09c centos:web "/usr/sbin/apachectl…" 4 minutes ago Up 4 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp stupefied_poincare ``` --- ## 啟動多個 docker web_server ### 逐行執行 ```dockerfile= # 注意: name 跟 prot number 必須不同 [user@centos-vm3 ~]$ sudo docker run --name web2 -d -p 8081:80 -v /home/user/docker_web/:/var/www/html/ centos:web /usr/sbin/apachectl -DFOREGROUND 7eaa2c96b217635cc39d7c3c66242933fbe003a4c0f05edd0abf07dd4ee8de9f [user@centos-vm3 ~]$ sudo docker run --name web3 -d -p 8082:80 -v /home/user/docker_web/:/var/www/html/ centos:web /usr/sbin/apachectl -DFOREGROUND 4c34a281f0f802047eaea17a6274d6838ab92956f5f146448de6eb6f312c126b [user@centos-vm3 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4c34a281f0f8 centos:web "/usr/sbin/apachectl…" 6 seconds ago Up 5 seconds 0.0.0.0:8082->80/tcp, :::8082->80/tcp web3 7eaa2c96b217 centos:web "/usr/sbin/apachectl…" 54 seconds ago Up 53 seconds 0.0.0.0:8081->80/tcp, :::8081->80/tcp web2 8655e0df747c centos:web "/usr/sbin/apachectl…" 3 hours ago Up 3 hours 0.0.0.0:8080->80/tcp, :::8080->80/tcp web ``` **移除** ```dockerfile [user@centos-vm3 ~]$ sudo docker rm -f `sudo docker ps -aq` 4c34a281f0f8 7eaa2c96b217 8655e0df747c ``` ### 腳本執行 ```dockerfile= # 注意: 對接的 port 要補上, web_name的容許範圍[a-zA-Z0-9][a-zA-Z0-9_.-] [user@centos-vm3 ~]$ cat ./docker_web.sh #!/usr/bin/bash for i in {1..5} do protnum=`expr 8080 + $i` webname=`expr 0 + $i` sudo docker run -d -p $portnum:80 -v /home/user/docker_web/:/var/www/html --name web$webname centos:web /usr/sbin/apachectl -DFOREGROUND done [user@centos-vm3 ~]$ chmod +x ./docker_web.sh [user@centos-vm3 ~]$ ./docker_web.sh ``` ![image](https://hackmd.io/_uploads/S1Gd3_n46.png) --- ## Load_Balancer * 使用的是 haproxy ```dockerfile= [user@centos-vm3 ~]$ sudo yum install haproxy openssl-devel -y [user@centos-vm3 ~]$ cd /etc/haproxy/ [user@centos-vm3 haproxy]$ sudo vim haproxy.cfg [user@centos-vm3 haproxy]$ cat haproxy.cfg defaults mode http # relational setting timeout client 10s timeout connect 5s timeout server 10s timeout http-request 10s frontend myfrontend bind 0.0.0.0:8080 # Load Balancer port default_backend myservers backend myservers balance roundrobin # docker web using port server server1 192.168.68.111:8001 server server2 192.168.68.111:8002 server server3 192.168.68.111:8003 server server4 192.168.68.111:8004 server server5 192.168.68.111:8005 [user@centos-vm3 ~]$ sudo systemctl start haproxy ``` ```b # 檢查 [user@centos-vm3 haproxy]$ sudo netstat -tunlp | grep 8080 tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 13944/haproxy ``` ![1700727025243](https://hackmd.io/_uploads/rJt4tFh46.gif) --- ## back_up image (Cloud) * image copy ```dockerfile= [user@centos-vm3 ~]$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos web c88f50f77c4c 24 hours ago 466MB # 產生一個image, 符合 account/images:tag (推到docker hub的話) [user@centos-vm3 ~]$ sudo docker tag c88 xuanlin123/centos:web [user@centos-vm3 ~]$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos web c88f50f77c4c 24 hours ago 466MB xuanlin123/centos web c88f50f77c4c 24 hours ago 466MB ``` * docker_hub login ```dockerfile= [user@centos-vm3 ~]$ sudo docker login Log in with your Docker ID or email address to push and pull images from ...... ...You can log in with your password or a Personal Access Token (PAT). ... . . . . . . . . . Login Succeeded ``` * upload ```dockerfile= [user@centos-vm3 ~]$ sudo docker push xuanlin123/centos:web The push refers to repository [docker.io/xuanlin123/centos] 1bee866f4fa0: Pushed 174f56854903: Mounted from library/centos web: digest: sha256:cdb93dc8f09127b5a522e043b80a23986d54bb6c9a265dd5715f7064006ba078 size: 742 ``` ![image](https://hackmd.io/_uploads/S1W2Zq3Ea.png) * delete local image ```dockerfile= [user@centos-vm3 ~]$ sudo docker rmi xuanlin123/centos:web Untagged: xuanlin123/centos:web Untagged: xuanlin123/centos@sha256:cdb93dc8f09127b5a522e043b80a23986d54bb6c9a265dd5715f7064006ba078 [user@centos-vm3 ~]$ sudo docker rmi centos:web # 如果報這個錯代表image使用中 Error response from daemon: conflict: unable to remove repository reference "centos:web" (must force) - container e70fab89b3ec is using its referenced image c88f50f77c4c [user@centos-vm3 ~]$ sudo docker rm -f `sudo docker ps -aq` 5e8192f6787a b81946a3070b cf82bdebcac3 755666acfe98 e70fab89b3ec [user@centos-vm3 ~]$ sudo docker rmi centos:web Untagged: centos:web Deleted: sha256:c88f50f77c4c600c198ccf0b39bba232aab4e3a13c58ee6c92c2e10015d38130 Deleted: sha256:fa1a9bee6e2c17f4d5defff4628caa1ce8178bce2f35cc89f62024aed721abfa [user@centos-vm3 ~]$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE ``` * pull cloud image ```dockerfile= [user@centos-vm3 ~]$ sudo docker pull xuanlin/centos:web web: Pulling from xuanlin123/centos 2d473b07cdd5: Already exists 457848f234d3: Pull complete Digest: sha256:cdb93dc8f09127b5a522e043b80a23986d54bb6c9a265dd5715f7064006ba078 Status: Downloaded newer image for xuanlin123/centos:web docker.io/xuanlin123/centos:web ``` --- ## 打包&匯入鏡像 `sudo docker save -h` * 本地的備份 ```bash # 要打包的image: centos:web , 壓縮後的名稱: centos_web.tar [user@centos-vm3 ~]$ sudo docker save centos:web > centos_web.tar [user@centos-vm3 ~]$ ls centos_web.tar ``` ```dockerfile [user@centos-vm3 ~]$ sudo docker load < centos_web.tar Loaded image: centos:web ``` --- ## dockerfile * 建立之後會有新的image ```dockerfile= # 建一個測試目錄 [user@centos-vm3 ~]$ mkdir -p dockerfile [user@centos-vm3 ~]$ cd dockerfile/ ``` ```dockerfile= # 編寫Dockerfile("D"必須大寫) [user@centos-vm3 dockerfile]$ vim Dockerfile [user@centos-vm3 dockerfile]$ echo "test dockerfile" > ./index.html [user@centos-vm3 dockerfile]$ cat index.html test dockerfile [user@centos-vm3 dockerfile]$ cat Dockerfile FROM centos:centos7.9.2009 RUN yum install -y httpd EXPOSE 80 ADD index.html /var/www/html/ ``` ```dockerfile= # 執行Dockerfile 最後面的 . 指的是Dockerfile [user@centos-vm3 dockerfile]$ sudo docker build -t dockerfile:web . # 執行過程 [+] Building 51.8s (8/8) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 129B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/centos:centos7.9.2009 0.0s => [1/3] FROM docker.io/library/centos:centos7.9.2009 0.0s => [internal] load build context 0.0s => => transferring context: 53B 0.0s => [2/3] RUN yum install -y httpd 50.5s => [3/3] ADD index.html /var/www/html/ 0.1s => exporting to image 1.1s => => exporting layers 1.1s => => writing image sha256:b74740fc8f01cc73d3cd3d355d893b5f871a4d09a3431fbb7dc7edf4e625c345 0.0s => => naming to docker.io/library/dockerfile:web # image check [user@centos-vm3 dockerfile]$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE dockerfile web b74740fc8f01 About a minute ago 466MB # run image [user@centos-vm3 dockerfile]$ sudo docker run --name dockerfileweb -d -p 8080:80 dockerfile:web /usr/sbin/apachectl -DFOREGROUND [user@centos-vm3 dockerfile]$ sudo netstat -tunlp | grep 8080 tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 4481/docker-proxy tcp6 0 0 :::8080 :::* LISTEN 4487/docker-proxy ``` ![image](https://hackmd.io/_uploads/H1I8u224p.png) --- ## Something else * start container & enter bash ```bash= [user@docker1 ~]$ sudo docker run -d -p 8080:80 --name www httpd 7cdea508d848daeabdfe838ecb21ac0a4b0c95d3bf7beb00541a86674aa5f31f [user@docker1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7cdea508d848 httpd "httpd-foreground" 5 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp www [user@docker1 ~]$ sudo docker exec -it 7cd bash root@7cdea508d848:/usr/local/apache2# ``` * 直接使用 apache image 的話網頁存放位置與 CentOS 不同 ```bash= root@7cdea508d848:/usr/local/apache2# ls bin build cgi-bin conf error htdocs icons include logs modules root@7cdea508d848:/usr/local/apache2# cd htdocs/ root@7cdea508d848:/usr/local/apache2/htdocs# ls index.html ``` * 暫時離開 & 回到 container bash ```bash= # 同時按 [Ctrl + p + q] root@7cdea508d848:/usr/local/apache2/htdocs# read escape sequence [user@docker1 ~]$ # 回到剛剛的 image , 但是這個 image 本來就在背景執行, 所以沒有反應 [user@docker1 ~]$ sudo docker attach 7cd # 因此要回到 bash 環境下還是執行 exec [user@docker1 ~]$ sudo docker exec -it 7cd bash root@7cdea508d848:/usr/local/apache2# ``` * 儲存 image ```bash= root@7cdea508d848:/usr/local/apache2/htdocs# echo "HIHIHI" > hi.htm root@7cdea508d848:/usr/local/apache2/htdocs# ls hi.htm index.html root@7cdea508d848:/usr/local/apache2/htdocs# read escape sequence [user@docker1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7cdea508d848 httpd "httpd-foreground" 15 minutes ago Up 4 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp www [user@docker1 ~]$ sudo docker commit 7cd httpd:0.1 sha256:eac6a91d6c0e65b6b2b3efb5addf998026ed0a9c525093830be34f1ad3237ba1 [user@docker1 ~]$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE httpd 0.1 eac6a91d6c0e About a minute ago 168MB ``` * 加上 "rm" 在 stop 之後會自動刪除 container ```bash= [user@docker1 ~]$ sudo docker run -d -p 8080:80 --rm httpd:0.1 95e7c0374fa6b7d27684e4ad32e039188e6bf0c3214b75a73a8c1a837716d4f1 [user@docker1 ~]$ sudo docker stop 95e 95e [user@docker1 ~]$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ``` * 上傳到 docker_hub ```bash= [user@docker1 ~]$ sudo docker tag httpd:0.1 xuanlin123/httpd:0.1 [user@docker1 ~]$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE httpd 0.1 eac6a91d6c0e 24 minutes ago 168MB xuanlin123/httpd 0.1 eac6a91d6c0e 24 minutes ago 168MB ``` * web server 目錄連結 ```bash= # 啟動完依照IP連過去看看 [user@docker1 ~]$ sudo docker run --name www --rm -d -p 8080:80 -v /home/user/Docker_Basic_test/docker_web/:/usr/local/apache2/htdocs httpd 271f317e5a6ed92347ed3f4c5a29bbd944386a8f38a9bed6e1122d79241762f5 [user@docker1 ~]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 271f317e5a6e httpd "httpd-foreground" 5 seconds ago Up 5 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp www ``` --- ## log 此為[Docker Network_實作測試](https://hackmd.io/b0ReOSeyS4WC5wubgCBZnQ?both#:~:text=forever%20preferred_lft%20forever-,%E5%AF%A6%E4%BD%9C%E6%B8%AC%E8%A9%A6,-%E7%95%99%E8%A8%80)的log ```bash= [user@docker1 docker_network]$ sudo docker logs 131 * Serving Flask app 'app.py' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses (0.0.0.0) * Running on http://127.0.0.1:5000 * Running on http://172.18.0.3:5000 Press CTRL+C to quit 192.168.68.1 - - [26/Nov/2023 13:34:15] "GET / HTTP/1.1" 200 - 192.168.68.1 - - [26/Nov/2023 13:34:16] "GET / HTTP/1.1" 200 - 192.168.68.1 - - [26/Nov/2023 13:34:17] "GET / HTTP/1.1" 200 - 192.168.68.1 - - [26/Nov/2023 13:34:17] "GET / HTTP/1.1" 200 - 192.168.68.1 - - [26/Nov/2023 13:34:17] "GET / HTTP/1.1" 200 - ``` --- ## Practice 1. 用腳本創建10個httpd docker port:8080~8089 ```bash= # shell [user@docker1 Q1]$ cat Q1.sh #!/usr/bin/bash for i in {1..10} do portnum=$((8079 + $i)) web=$((0 + $i)) sudo docker run -d -p $portnum:80 --name web$web -v "$PWD":/usr/local/apache2/htdocs/ httpd done ----------------------------------------------------------------------------------------------- # result [user@docker1 Q1]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f46c1774bd3 httpd "httpd-foreground" 3 minutes ago Up 3 minutes 0.0.0.0:8089->80/tcp, :::8089->80/tcp web10 da48c0803836 httpd "httpd-foreground" 3 minutes ago Up 3 minutes 0.0.0.0:8088->80/tcp, :::8088->80/tcp web9 815377fea02c httpd "httpd-foreground" 3 minutes ago Up 3 minutes 0.0.0.0:8087->80/tcp, :::8087->80/tcp web8 bf05e079f3c8 httpd "httpd-foreground" 3 minutes ago Up 3 minutes 0.0.0.0:8086->80/tcp, :::8086->80/tcp web7 1506856edba3 httpd "httpd-foreground" 3 minutes ago Up 3 minutes 0.0.0.0:8085->80/tcp, :::8085->80/tcp web6 1d0ff7a2c0ef httpd "httpd-foreground" 3 minutes ago Up 3 minutes 0.0.0.0:8084->80/tcp, :::8084->80/tcp web5 812ef193b394 httpd "httpd-foreground" 3 minutes ago Up 3 minutes 0.0.0.0:8083->80/tcp, :::8083->80/tcp web4 ed36e5ac4c73 httpd "httpd-foreground" 3 minutes ago Up 3 minutes 0.0.0.0:8082->80/tcp, :::8082->80/tcp web3 532196b4a50e httpd "httpd-foreground" 3 minutes ago Up 3 minutes 0.0.0.0:8081->80/tcp, :::8081->80/tcp web2 643e9a2b2176 httpd "httpd-foreground" 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp web1 ``` 2. 用腳本關閉 Q1 的10個 httpd docker ```bash= # shell [user@docker1 Q2]$ cat Q2.sh #!/usr/bin/bash sudo docker stop `sudo docker ps -aq` echo "already stop" sudo docker rm `sudo docker ps -aq` echo "already remove" ------------------------------------ # result [user@docker1 Q2]$ ./Q2.sh 9c40857a1764 6daafb837a55 c285837d7308 ebc51e7288a9 1d545599c60e 36337df08a7c d6d2dac96061 24f1f6043f06 a544fd208442 d5a576dd2684 already stop # 停止提示 9c40857a1764 6daafb837a55 c285837d7308 ebc51e7288a9 1d545599c60e 36337df08a7c d6d2dac96061 24f1f6043f06 a544fd208442 d5a576dd2684 already remove # 刪除提示 ```

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully