怎麼使用dockerfile建置nginx映像
dockerfile介紹
docker透過讀取dockerfile裡面的內容可以自動build image,dockerfile是一個包含了build過程中需要執行的所有指令的文字檔。也可以理解為dockfile是一種被docker程式解釋的腳本,由一條一條的指令組成,每個指令對應linux系統下面的一條指令,由docker程式將這些dockerfile指令翻譯成真正的linux指令。 dockerfile有自己書寫格式和支援的指令,docker程式解決這些指令間的依賴關係,類似makefile。
docker程式將讀取dockerfile,根據指令產生客製化的image。相較於image這種黑盒子,dockerfile這種顯而易見的腳本更容易被使用者接受,它明確的顯示image是怎麼產生的。有了dockerfile,當我們需要自訂自己額外的需求時,只需在dockerfile上新增或修改指令,重新產生image即可,省去了敲指令的麻煩。
docker建構映像的方法: commit、dockerfile
1、使用commit來建立映像:
commit是基於原有映像基礎上建置有映像基礎上建構有映像基礎上的鏡像,使用此方法建立鏡像的目的:保存鏡像裡的一些配置資訊和修改的資訊。相當於一個鏡像的快照。
2、使用dockerfile來建立映像:
dockerfile是快速建置所需(自訂)映像。
dockerfile的指令:
from:指定基礎映像(from是必備的指令,並且必須為第一個指令)。
run: 用來執行命令列命令。其基本格式:
shell格式: run ,輸入在bash環境中的命令即可,一個dockerfile允許使用run不得超過127層,所以,使用一次run, 使用' \'換行,使用' && '執行下一指令。一般使用此種格式;
exec格式:run ,此種方式像是函數呼叫中的格式;
copy: 複製檔案。其基本格式:
格式1:copy ...
」格式2:copy [## 發格式2:copy [“"]
add: 更高級的複製文件,在copy的基礎上增加了一些功能,如果複製的是壓縮包的話,會直接解壓,而不需要在使用run解壓縮;
cmd:容器啟動指令。其基本格式:
shell格式: cmd
exec格式: cmd ["可執行檔", "參數1", "參數2"...]
參數清單格式: cmd [「參數1」, 「參數2」...],在指定了entrypoint指令後,用cmd指定特定的參數
entrypoint: 入口點。其基本格式分為exec和shell,
entrypoint的目的和cmd一樣,都是在指定容器啟動程式及參數。 entrypoint在運作上可以替代,不過比cmd繁瑣,需要透過docker run 的參數--entrypoint 來指定。當指定了entrypoint後,cmd的含義就發生了改變,不在是直接執行其命令,而是將cmd的內容作為參數傳遞給entrypoint指令。其執行時就變成了:
# env: 設定環境變數。 (都可以使用這裡使用的變數)其基本格式:
格式1:env
行格式:26;
arg: 建置參數。建構參數和env的效果一樣,都是設定環境變量,不同的是arg所建構的環境變數在將來容器執行時是不存在的。其基本格式:
格式1: arg [=]
# 格式/設定 此預設值-build-arg = 來覆寫
volume: 定義匿名磁碟區。其基本格式:
格式1:volume ["", ""...]
## 路徑2>"...]##
## expose: 暴露埠。 expose指令是聲明運行時容器所提供的端口,在啟動容器時不會在因為這個聲明而開啟端口。其基本格式: 格式1: expose [...]# workdir:指定工作目錄。其基本格式: 格式1: workdir user: 指定目前使用者。 user是幫助你切換到指定使用者。其基本格式:格式1: user
healtcheck: 健康檢查,判斷容器的狀態是否正常。其基本格式:
格式1: healtcheck [選項] cmd :設定檢查容器健康狀況的指令
## 與健康指令 2: 〔健康指令格式。 ,使用此格式可以屏蔽掉其健康檢查指令建置nginx映像:
建立一個目錄,在該目錄中編寫dockerfile:[root@docker ~]# mkdir mynginx [root@docker ~]# cd mynginx/ [root@docker mynginx]# pwd /root/mynginx [root@docker mynginx]#
[root@docker ~]# wget -p /root/mynginx/ http://nginx.org/download/nginx-1.15.2.tar.gz
[root@docker mynginx]# vi dockerfile
from centos run ping -c 1 www.baidu.com run yum -y install gcc make pcre-devel zlib-devel tar zlib add nginx-1.15.2.tar.gz /usr/src/ run cd /usr/src/nginx-1.15.2 \ && mkdir /usr/local/nginx \ && ./configure --prefix=/usr/local/nginx && make && make install \ && ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ \ && nginx run rm -rf /usr/src/nginx-1.15.2 expose 80
[root@docker mynginx]# docker build -t nginx:v3 . sending build context to docker daemon 1.029mb step 1/7 : from centos ---> 5182e96772bf step 2/7 : run ping -c 1 www.baidu.com ---> using cache ---> 2f70f8abaf2a step 3/7 : run yum -y install gcc make pcre-devel zlib-devel tar zlib ---> using cache ---> dbdda4b7ae6f step 4/7 : add nginx-1.15.2.tar.gz /usr/src/ ---> using cache ---> 18ace6285668 step 5/7 : run cd /usr/src/nginx-1.15.2 && mkdir /usr/local/nginx && ./configure --prefix=/usr/local/nginx && make && make install && ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ && nginx ---> using cache ---> 99629488ede9 step 6/7 : run rm -rf /usr/src/nginx-1.15.2 ---> using cache ---> 869fbad71879 step 7/7 : expose 80 ---> using cache ---> 384bed72ea6f successfully built 384bed72ea6f successfully tagged nginx:v3
[root@docker ~]# docker run -dit -p 80:80 --name nginx nginx:v3 ecaafe1190447878b98dfb0198e92439db60ff7dab57a1674e0e9e7282a9c858 [root@docker ~]# docker ps -a container id image command created status ports names ecaafe119044 nginx:v3 "/bin/bash" 3 seconds ago up 2 seconds 0.0.0.0:80->80/tcp nginx
[root@docker ~]# docker run -dit -p 80:80 --name nginx nginx:v3 ecaafe1190447878b98dfb0198e92439db60ff7dab57a1674e0e9e7282a9c858 [root@docker ~]# docker ps -a container id image command created status ports names ecaafe119044 nginx:v3 "/bin/bash" 3 seconds ago up 2 seconds 0.0.0.0:80->80/tcp nginx
[root@docker ~]# curl 192.168.100.22 curl: (7) failed connect to 192.168.100.22:80; 拒绝连接 [root@docker ~]# elinks --dump 192.168.100.22 elinks: 拒绝连接
[root@docker ~]# docker exec -it nginx bash [root@ecaafe119044 /]# nginx [root@ecaafe119044 /]# exit exit
[root@docker ~]# curl 192.168.100.22 <!doctype html> <html> <head> <title>welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: tahoma, verdana, arial, sans-serif; } </style> </head> <body> <h1 id="welcome-nbsp-to-nbsp-nginx">welcome to nginx!</h1> <p>if you see this page, the nginx web server is successfully installed and working. further configuration is required.</p> <p>for online documentation and support please refer to <a href="http://nginx.org/" rel="external nofollow" >nginx.org</a>.<br/> commercial support is available at <a href="http://nginx.com/" rel="external nofollow" >nginx.com</a>.</p> <p><em>thank you for using nginx.</em></p> </body> </html>
以上是怎麼使用dockerfile建置nginx映像的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

可以通過以下步驟查詢 Docker 容器名稱:列出所有容器(docker ps)。篩選容器列表(使用 grep 命令)。獲取容器名稱(位於 "NAMES" 列中)。

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

Docker 容器啟動步驟:拉取容器鏡像:運行 "docker pull [鏡像名稱]"。創建容器:使用 "docker create [選項] [鏡像名稱] [命令和參數]"。啟動容器:執行 "docker start [容器名稱或 ID]"。檢查容器狀態:通過 "docker ps" 驗證容器是否正在運行。

可以查詢 Nginx 版本的方法有:使用 nginx -v 命令;查看 nginx.conf 文件中的 version 指令;打開 Nginx 錯誤頁,查看頁面的標題。

在 Docker 中創建容器: 1. 拉取鏡像: docker pull [鏡像名] 2. 創建容器: docker run [選項] [鏡像名] [命令] 3. 啟動容器: docker start [容器名]

在雲服務器上配置 Nginx 域名的方法:創建 A 記錄,指向雲服務器的公共 IP 地址。在 Nginx 配置文件中添加虛擬主機塊,指定偵聽端口、域名和網站根目錄。重啟 Nginx 以應用更改。訪問域名測試配置。其他注意事項:安裝 SSL 證書啟用 HTTPS、確保防火牆允許 80 端口流量、等待 DNS 解析生效。

啟動 Nginx 服務器需要按照不同操作系統採取不同的步驟:Linux/Unix 系統:安裝 Nginx 軟件包(例如使用 apt-get 或 yum)。使用 systemctl 啟動 Nginx 服務(例如 sudo systemctl start nginx)。 Windows 系統:下載並安裝 Windows 二進製文件。使用 nginx.exe 可執行文件啟動 Nginx(例如 nginx.exe -c conf\nginx.conf)。無論使用哪種操作系統,您都可以通過訪問服務器 IP
