# 第二次社課 ###### tags: `1092` :::success 簽到: B073040047 楊志璿 B093040044 蔡明軒 B093012033 王勤 B093040016 高聖傑 B073040030 林秉承 B073040031 葉星佑 B083040021 嚴宇同 93140010 王品媛 M093010066 謝佳翰 B063040010 Steve M083140001 stavhaygn M093140014 Arikoi M093140005 黃婷筠 M083140005 yctseng M052030005 pg ::: 講者:SCC [下載資料](http://140.117.169.217/0310/docker.zip) // 內含投影片 main.pdf 講題:Docker ## Container ![](https://i.imgur.com/k3959Dy.png) 概念:用一個箱子打包。 ### Container和virtual Machine的差別 * Docker * 在同一個 OS 上運行 * Docker 只是負責管理不同的 APP * Virtual Machine * 在完全獨立的OS上運行 * 時間較慢,消耗資源多 ## Shared Kernel ## Dockerfile 定義: ![](https://i.imgur.com/UjtdyAU.png) 如同程式碼,告訴 Docker 應該如何做的檔案稱為 Dockerfile。 如同編譯後的程式碼,在 Docker 當中稱作 Docker Image。 如同執行中的程式會叫做 process,執行中的 image 被稱為 Container。 https://hub.docker.com/ ### 語法 ![](https://i.imgur.com/k3GPkd9.png) ```bash $ docker images # 查看有哪些 images $ docker ps # 查看有哪些 container # Build # cd ./docker/src/dockerfile101 $ docker build -t [自訂image名稱] . # Run $ docker run <-d> [自訂image名稱] # -d 為背景執行 $ docker inspect [Container_ID] | grep "IPAddress" # 找IP資訊 # -i: interactive 互動模式 # -t: 指定 terminal (此例為 bash) # -u: 指定 user (此例為root) $ docker exec <-u root> -it [Container_ID] bash # Stop $ docker stop [Container_ID] # 刪除 image $ docker images rm [image_ID] ``` DockerFile ```dockerfile= FROM ubuntu ENV KFC=EGG_TART # x11vnc 可以提供畫面渲染 RUN apt update && apt install -y x11vnc xvfb firefox RUN useradd -m user1 --uid=1000 && \ echo "user1:Ch@ng3_m3" | chpasswd USER user1:1000 WORKDIR /home/user1 RUN bash -c 'echo "firefox" >> /home/user1/.bashrc' && \ mkdir ~/.vnc && \ x11vnc -storepasswd nsysuisc ~/.vnc/passwd EXPOSE 5900 CMD ["x11vnc", "-forever", "-usepw", "-create"] ``` ## Docker-compose's Lab ```bash= $ git clone https://github.com/dockersamples/example-voting-app.git $ cd example-voting-app ``` docker swarm ``` $ sudo docker swarm init $ sudo docker stack deploy --compose-file docker-stack.yml vote ``` docker compose ``` $ vim docker-compose.yml # 在第1行增加version: "2.1" $ sudo docker-compose up -d --build # 啟動 $ sudo docker-compose down # 關閉 ``` 來投票 http://localhost:5000/ 看結果 http://localhost:5001/ ## Docker Command ### Exercises 1. Write a "hello world program" in a ubuntu docker container. - Dockerfile - 以 ubuntu 為基底 - 執行 apt update - 安裝 vim, gcc - `-y` 表示針對安裝過程的yes/no全部都選yes ```dockerfile= FROM ubuntu RUN apt update && apt install vim gcc -y ``` ```bash $ docker build -t hello . # 將當前目錄下的Dockerfile build起來 $ docker run -it hello bash (Docker)$ vim hello.c # Write "hello world program" ``` 2. Execute the "hello world program". ```bash (Docker)$ gcc hello.c -o hello.elf # compile (Docker)$ ./hello.elf # execute ``` 3. Export the container to a gzip, which is named foo.tar.gz . ```bash $ docker ps -a # 查看 Contain ID $ docker export [Container_ID] | gzip > foo.tar.gz ``` 4. Stop the container which had printed the "hello world program". ```bash $ docker ps # 檢查Container是否還在運作 $ docker stop [Container_ID] ``` 5. Import the foo.tar.gz as an image, which is named foo. ```bash $ cat foo.tar.gz | docker import - foo $ docker images # 檢查是否有foo ``` 6. Run the "hello world program" from image foo. ```bash $ docker run -it foo bash (Docker)$ ./hello.elf ``` 7. Commit the previous step container as an image, which is named goo. ```bash $ docker commit [New_Container_ID] goo $ docker images # 確認是否成功commit ``` 8. Show me this figure. ```bash $ docker run goo /hello.elf ``` ## Docker CLI ## Tiny project --- 頁尾交流區 - 感謝大大筆記 :monkey: