Docker安裝Nginx容器配置及重新生成鏡像圖文教程
基本思路:
先下載Nginx鏡像,然后運行一個Nginx容器,在容器中配置相關參數(shù),最后把配置好的容器制作成一個鏡像,后期發(fā)布到服務器上可以省去重復配置。
1、查看是否存在nginx鏡像
docker images

發(fā)現(xiàn)還沒有下載過nginx鏡像
2、搜索可用的nginx鏡像,下載nginx鏡像
首先查詢可用的nginx鏡像:
docker search nginx

就下載第一個nginx:
docker pull nginx

不指定版本號,默認就是最新版本

3、創(chuàng)建并啟動nginx容器
docker run --name mynginx -d -p 8989:80 nginx
# --name 給容器起一個名字
# -d 在后臺運行
# -p 8989:80 把容器內的80端口映射到宿主機的8989端口

查看容器運行狀態(tài):
docker ps

訪問驗證:使用外部端口8989可以訪問該容器nginx

4、進入容器,對相關文件進行配置
進入容器:
docker exec -it mynginx /bin/bash?
# -i: 交互式操作。
# -t: 終端。
# mynginx : nginx鏡像。
# /bin/bash:放在鏡像名后的是命令,這里我們希望有個交互式 Shell,因此用的是 /bin/bash。
C:\Users\Administrator>docker exec -it mynginx /bin/bash
root@1eb487ead85e:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
root@1eb487ead85e:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@1eb487ead85e:/# cd /etc/nginx
root@1eb487ead85e:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
root@1eb487ead85e:/etc/nginx# cd conf.d
root@1eb487ead85e:/etc/nginx/conf.d# ls
default.conf
root@1eb487ead85e:/etc/nginx/conf.d# cat default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
root@1eb487ead85e:/etc/nginx/conf.d# cd /usr/share/nginx/html
root@1eb487ead85e:/usr/share/nginx/html# ls
50x.html index.html可以看到nginx的默認配置是在/etc/nginx/conf.d/default.conf配置文件里,通過配置文件里
location / {<!--{C}%3C!%2D%2D%20%2D%2D%3E-->
root /usr/share/nginx/html;
index index.html index.htm;
}可以知道nginx的html文件目錄,這樣就可以把我們自己的代碼拷貝到html文件夾下:
docker cp d:/html?1eb487ead85e:/usr/share/nginx
# docker cp 本機文件路徑 容器id:容器內的路徑
5、更新鏡像
我們對nginx進行配置后,需要使用當前容器生成一個新的鏡像,
我們可以通過命令 docker commit 來提交容器副本。
docker commit -m="更新配置" -a=lwpoor 1eb487ead85e lwpoor/nginx:1.0
# -m: 提交的描述信息
# -a: 指定鏡像作者
# 1eb487ead85e :容器 ID
# lwpoor/nginx:1.0: 指定要創(chuàng)建的目標鏡像名
C:\Users\Administrator>docker commit -m="更新配置" -a=lwpoor 1eb487ead85e lwpoor/nginx:1.0 sha256:110f8f64ca1ea47ba61b3c773b3fe5a07c13492a17e6378455dc6d254f17177e C:\Users\Administrator>docker images REPOSITORY TAG IMAGE ID CREATED SIZE lwpoor/nginx 1.0 110f8f64ca1e 9 seconds ago 224MB
6、將由容器生成的鏡像push到鏡像倉庫docker hub
首先需要登錄 docker hub:
docker login?
推送到鏡像倉庫:
docker push?lwpoor/nginx:1.0
C:\Users\Administrator>docker login Authenticating with existing credentials... Login Succeeded Logging in with your password grants your terminal complete access to your account. For better security, log in with a limited-privilege personal access token. Learn more at https://docs.docker.com/go/access-tokens/ C:\Users\Administrator>docker push lwpoor/nginx:1.0 The push refers to repository [docker.io/lwpoor/nginx] c269977a08d5: Pushed d874fd2bc83b: Mounted from library/nginx 32ce5f6a5106: Mounted from library/nginx f1db227348d0: Mounted from library/nginx b8d6e692a25e: Mounted from library/nginx e379e8aedd4d: Mounted from library/nginx 2edcec3590a4: Mounted from library/nginx 1.0: digest: sha256:fe4d4e8f68cace0f19cc7d070c84030487b31a585cdcd5969afe6f8848f80aca size: 1782
可以看到已經推送到鏡像倉庫了:

總結
到此這篇關于Docker安裝Nginx容器配置及重新生成鏡像的文章就介紹到這了,更多相關Docker安裝Nginx容器配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
docker network_mode: "host" 網絡配置方式
這篇文章主要介紹了docker network_mode: "host" 網絡配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
解決docker啟動容器失敗:cannot?access‘/docker-entrypoint-initdb.d/‘:
這篇文章主要介紹了解決docker啟動容器失敗:cannot?access‘/docker-entrypoint-initdb.d/‘:Operation?not?permitted,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
docker?build運行報錯source:?not?found解決分析
這篇文章主要為大家介紹了docker?build運行報錯source:?not?found解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
docker-swarm之使用Docker secret管理敏感數(shù)據(jù)
就Docker Swarm集群服務而言,secret 是塊狀數(shù)據(jù),例如密碼、SSH私鑰、SSL證書或其他不應通過網絡傳輸或未加密存儲在Dockerfile或應用程序源代碼中的數(shù)據(jù),我們可以使用Docker secret 集中管理這些數(shù)據(jù),所以接下來就帶大家了解一下如何使用Docker secret 管理敏感數(shù)據(jù)2023-08-08
docker中run、start和create命令的區(qū)別
對于 Docker 初學者來說,docker start、docker run 和 docker create 等術語可能會令人困惑,本文就來介紹一下docker中run、start和create命令的區(qū)別,感興趣的可以了解一下2023-11-11

