---
# System prepended metadata

title: 2022/11/08 Srcmesh Lesson
tags: [K8S Lession]

---

# 2022/11/08 Srcmesh Lesson 
###### tags: `K8S Lession`
https://hackmd.io/1hsfRIupReS65KFacDDzqQ

### Asciinema.org 操作demo
- https://asciinema.org/a/Wsg3glksIOuYXIi84tUAAZvU5

### Workshop Github 
- https://github.com/srcmesh-workshop/container-hands-on

### Github issue - homework reply
- https://github.com/srcmesh-workshop/container-hands-on/issues

### SSH to GKE (k8s環境)
```
Account:    ssh user-<name>@workshop.srcmesh.dev
Password:   srcmesh-123456789
web: http://workshop.srcmesh.dev:<port-number>
```
---
### Create source code file

- [Local] ./Dockerfile  (expose要改成對應port號)

        FROM ubuntu:16.04

        RUN apt update -y
        RUN apt install -y nginx

        COPY index.html /var/www/tutorial/
        COPY demo.conf /etc/nginx/sites-enabled/

        EXPOSE 80??
        ENTRYPOINT ["nginx", "-g", "daemon off;"]


- [Local] ./index.html
    Dockerfile會將目標COPY到 Container內Path -> /var/www/tutorial/index.html

        <!doctype html>
        <html>
        <head>
            <meta charset="utf-8">
            <title>Hello, Nginx!</title>
        </head>
        <body>
            <h1>Hello, Nginx!</h1>
            <p>We have just configured our Nginx web server on Ubuntu Server!</p>
        </body>
        </html>

- [Local] ./demo.conf
    Dockerfile會將目標COPY到 Container內Path -> /etc/nginx/sites-enabled/demo.conf
    
        server {
           listen 81;
           listen [::]:81;

           server_name example.ubuntu.com;

           root /var/www/tutorial;
           index index.html;

           location / {
                   try_files $uri $uri/ =404;
           }
        }

---

### Docker command


- 查看目前運行之容器清單
    ```
    docker ps -a
    ```
- 查看目前映像檔(image)清單
    ```
    docker images
    ``` 
- 建置image
    ```
    docker build -t <image-name>:<tag> .
    ```
- 重新變更image名稱:tag
    ```
    docker tag <target-image-name>:<tag> <rename-image>:<re-tag>
    ```
- 暫停目標運行中容器
    ```
    docker stop <container-name> or <container-id>
    ```
- 刪除容器清單之目標容器
    ```
    docker rm <container-name> or <container-id>
    ```
- 刪除目標image(建議用container-id刪除較準確)
    ```
    docker rmi <image-name>:<tag> or <image-id>
    ```
- 運行容器
    ```
    docker run -d -p 80??:81 --name <container-name> <image-name>:<tag>
    
        -d: 背景運行
        -p: expose port
        --name: 對該container取名
        --rm: process過後直接刪除
    ```
- 確認容器狀態 (可用來確認系統發生什麼問題)
    ```
    docker logs -f <container-name> 
    ```

### Homework流程
##### 本次作業主要為建置一變更過port之nginx image，並將該image push至dockerhub上。

    Set :
        <image-name>:<tag>         -> user-99-nginx:1.0.2
        <rename-image>:<re-tag>    -> user-99-kenny:1.0.2
        <container-name>           -> container-99-nginx 
        <dockerhub-name>           -> xxooxox
    
1. Create source code file.
    於Local資料夾內建置前述三份檔案。
    
3. Build image
    ```
    docker build -t <image-name>:<tag> .
   
    ex:
        docker build -t user-99-nginx:1.0.2 .
    ```
3. Create container and check container status is running.
    ```
    docker run -d -p 80??:81 --name <container-name> <image-name>:<tag>
    
    ex:
        docker run -d -p 8099:81 --name container-99-nginx user-99-nginx:1.0.2
    ```
    確認container是否建置成功。
    ```
    docker ps -a | grep  <container-name>
    
    ex:
        docker ps -a | grep  container-99-nginx
    ```
4. Check web get something.
    ```
    curl http://workshop.srcmesh.dev:80??
    
    ex:
        curl http://workshop.srcmesh.dev:8099
    ```
    
6.  Check docker login success. (需要有dockerhub帳號)
    ```
    docker login
    
    > username: xxxxxx
    > password: xxxxxx
    ```
5.  Re-tag image name. (<dockerhub-name>為dockerhub帳號)
    ```
    docker tag <image-name>:<tag> <dockerhub-name>/<rename-image>:<re-tag>
    
    ex.
        docker tag user-99-nginx:1.0.2 xxooxox/user-99-kenny:1.0.2
    ```
6. Push to dockerhub and check it in dockerhub.
    ```
    docker push <rename-image>:<re-tag>
    
    ex.
        docker push xxooxox/user-99-kenny:1.0.2
    ```
7. Pull image to local and check it can run by new one.(要記得本機端不能有該名稱image)
    ```
    docker pull <rename-image>:<tag>
    
    ex.
        docker pull xxooxox/user-99-kenny:1.0.2
    ```
    執行前要先確保先前 [步驟3] 的container已經刪除。(相同port or name之容器是無法執行的，未刪除請先執行docker delete 進行刪除)
    ```
    docker run -d -p 80??:81 --name <container-name-check> <rename-image>:<tag>
    
    ex:
        docker run -d -p 8099:81 --name container-99-nginx-check xxooxox/user-99-kenny:1.0.2
    ```
    確認container建置後，測試網站是否成功架起。
    ```
    curl http://workshop.srcmesh.dev:80??
    
    ex:
        curl http://workshop.srcmesh.dev:8099
    ```

8. Go to Github issue homework to reply.