{%hackmd JQfftjblR62Jj4uOI9cDBQ %} --- ## Download Nginx Image 在啟動Nginx之前,需要先將Nginx的映象檔下載到VM主機中,可以先執行以下指令 ```bash # 搜尋有nginx字眼的image $ podman search nginx --filter=is-official NAME DESCRIPTION docker.io/library/nginx Official build of Nginx. docker.io/library/unit Official build of NGINX Unit: Universal Web... ``` 找到相關的映象檔,接著可以再執行指令下載映像檔 ```bash $ podman pull docker.io/library/nginx Trying to pull docker.io/library/nginx:latest... Getting image source signatures Copying blob sha256:f46d7b05649a846d7e24418b6ecea3b1efbdac88d361631e849e9c41917ba776 Copying blob sha256:76579e9ed380849b4d22696f292770963b5ea917a45ef77336a1a0a782b410b5 Copying blob sha256:faef57eae888cbe4a5613eca6741b5e48d768b83f6088858aee9a5a2834f8151 Copying blob sha256:cf707e2339551222cafe3bf835fddfb859f26bf59058b3487de2b7659309b6b7 Copying blob sha256:4b962717ba558b7dfabe88c40e20ac86844b132015b66002deac49010cc96be1 Copying blob sha256:91bb7937700d7d3496cf43cb0012e5f818064fecb766bd01041db23c127ab219 Copying blob sha256:103501419a0aecf94398ffcc7404f22931d9b89bbb6021391c2cd4a286f37ca9 Copying config sha256:021283c8eb95be02b23db0de7f609d603553c6714785e7a673c6594a624ffbda Writing manifest to image destination Storing signatures ``` ## Starting Nginx 啟動方式有兩種,分別如下 1. 直接啟動Container 2. 先建立[Pod](#Creating-Pod),再啟動Container到Pod內 :::danger **TODO** 待補充Pod與Container關係 ::: ### Creating Pod 執行以下指令建立一個Pod ```bash $ podman pod create --name nginxapp -p 8080:80 ``` :::success 參數說明 ~ \-\-name nginxapp:設定Pod的名稱為"nginxapp" ~ -p 8080:80:設定端口轉發,將主機的8080端口映射到Pod內的80端口,這樣可以通過主機的8080端口訪問Pod內的應用 ::: 完成後可以執行以下指令,查詢剛建立名稱為nginxapp的Pod ```bash # 以下指令與podman pod ls相同 $ podman pod ps POD ID NAME STATUS CREATED INFRA ID # OF CONTAINERS 5beb433d10fa nginxapp Created 9 seconds ago 21950aaa7dcd 1 ``` ### Creating Container 若是直接啟動Container的話,請跳至[直接建立並啟動Container](#直接建立並啟動Container)章節;若是已有執行[Creating Pod](#/Creating-Pod),則接下來要在nginxapp這個Pod中建立並啟動新的Container,請跳至[已建立Pod後,啟動Container](#已建立Pod後,啟動Container)章節 #### 直接建立並啟動Container 執行以下指令 ```bash $ podman run --name nginx-server -d nginx:latest ``` :::success 參數說明 ~ \-\-name:設定Container的名稱為"nginx-server" ~ -d:以分離模式運行容器,即容器在後台運行。 ~ nginx:latest:指定要運行的容器映像,這裡使用的是nginx映像的最新版本 ::: #### 已建立Pod後,啟動Container 執行以下指令 ```bash $ podman run --pod nginxapp --name nginx-server -d nginx:latest ``` :::success 參數說明 ~ \-\-pod:將Container加到指定的Pod之中 ~ \-\-name:設定Container的名稱為"nginx-server" ~ -d:以分離模式運行容器,即容器在後台運行。 ~ nginx:latest:指定要運行的容器映像,這裡使用的是nginx映像的最新版本 ::: ### Check instance if its running 成功啟動Container後,執行以下指令,也可以直接開啟瀏覽器連上 http://localhost:8080 ,將會顯示Nginx預設網頁 ```bash $ podman ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e1c57fad24ce localhost/podman-pause:4.5.0-1681486976 About a minute ago Up 7 seconds 0.0.0.0:8080->80/tcp 8e782f9df27b-infra 263bf9405062 docker.io/library/nginx:latest nginx -g daemon o... 7 seconds ago Up 7 seconds 0.0.0.0:8080->80/tcp nginx-server ```  ### Adding Custom HTML file to be uploaded to Nginx 想要把靜態網頁加入容器中,有兩種方法: 1. 透過Podman手動複製檔案 2. 設定volume掛載到Container 在此之前需要先確認兩點,分別為 1. Nginx預設網頁路徑 > 透過指令進入Nginx容器,確認Nginx預設的網頁資料夾路徑在`/usr/share/nginx/html` > > ```bash > $ podman exec -it nginx-server bash > root@nginxapp:/# ls usr/share/nginx/html -lh > total 8.0K > -rw-r--r-- 1 root root 497 Jun 13 15:08 50x.html > -rw-r--r-- 1 root root 615 Jun 13 15:08 index.html > ``` 2. 在Podman VM主機中靜態網頁的檔案路徑 > PPodman VM(Podman Machine)在啟動時,會將硬碟掛載到根目錄底下的mnt之中,我們可以透過指令連進VM主機確認 > > ```bash > $ podman machine ssh > Connecting to vm podman-machine-default. To close connection, use `~.` or `exit` > Warning: Permanently added '[localhost]:2549' (ECDSA) to the list of known hosts. > Last login: Fri Jul 7 15:22:15 2023 from 127.0.0.1 > ### 執行下面指令 > [user@COMPUTERNAME ~]$ ls /mnt -lh > total 0 > drwxrwxrwx 1 user user 4.0K Jul 3 15:58 c > drwxrwxrwt 2 root root 60 Jul 6 14:01 wsl > drwxrwxrwt 7 root root 300 Jul 6 14:02 wslg > ``` #### 透過Podman手動複製檔案 接下來我們就可以透過指令把檔案複製到Container之中 <div style="font-size: 28px; color: #FF0000">注意!目前仍在VM主機中</div> <p></p> ```bash [user@COMPUTERNAME ~]$ podman cp /mnt/c/<檔案路徑>/<檔案名稱>.html <ContainerId>:usr/share/nginx/html ``` 完成後再次開啟瀏覽器連上 http://localhost:8080/<檔案名稱>.html ,即可載入客製的靜態頁面  #### 設定volume掛載到Container 範例以本機資料夾: D:\container\html掛載至Container內 ```powershell PS > podman run -d -v d:/container/html:/usr/share/nginx/html -p 8080:80 --name nginx nginx ``` > 由於Windows GitBash會轉換路徑,導致無法找到對應的檔案或資料夾,所以請使用==PowerShell==來執行 ## 疑問 ### 設定Port Forwarding低於1024,如主機Port 80映射至Container Port 80 ```bash $ podman pod create --name nginxapp -p 80:80 ``` ```bash $ podman run --pod nginxapp --name nginx-server -d nginx:latest Error: starting some containers: internal libpod error ``` 若跳出上面的錯誤訊息,根據啟動VM時的系統訊息 > This machine is currently configured in rootless mode. **If your containers > require root permissions ****(e.g. ports < 1024)**, or if you run into compatibility > issues with non-podman clients, you can switch using the following command: > podman machine set --rootful 原因為使用port低於1024時,必須將權限設為root,否則在啟動Container時將會發生錯誤 ```bash $ podman machine set --rootful ``` 執行以上指令後,請重新建立Pod及Container,即可用 http://localhost:80 連到網站
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.