日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

docker之手动构建新的镜像

發(fā)布時間:2024/1/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 docker之手动构建新的镜像 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

查看本地現(xiàn)有鏡像:

[root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c59f17fe53b0 4 days ago 108MB ubuntu latest 747cb2d60bbe 3 weeks ago 122MB centos latest 196e0ce0c9fb 6 weeks ago 197MB

現(xiàn)在利用基礎(chǔ)鏡像centos,在此基礎(chǔ)上手動構(gòu)建一個web服務(wù),這里采用nginx

啟動一個container并進入到容器內(nèi):

[root@docker ~]# docker run -it --name=web centos /bin/bash [root@bab3b6991467 /]#

然后在容器內(nèi)進行安裝nginx服務(wù):

[root@bab3b6991467 /]# cd /usr/local/src/ [root@bab3b6991467 src]# yum install wget vim

這里采用編譯安裝nginx,所以下載nginx源碼包,并安裝好編譯環(huán)境:

[root@bab3b6991467 src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

編譯環(huán)境:

[root@bab3b6991467 src]# yum install gcc gcc-c++ glibc make autoconf openssl openssl-devel

安裝nginx的一些依賴包:

[root@bab3b6991467 src]# yum install libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel

然后開支執(zhí)行安裝:

[root@bab3b6991467 src]# ll total 960 -rw-r--r--. 1 root root 981687 Oct 17 13:20 nginx-1.12.2.tar.gz [root@bab3b6991467 src]# tar xf nginx-1.12.2.tar.gz [root@bab3b6991467 src]# cd nginx-1.12.2 [root@bab3b6991467 nginx-1.12.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module

創(chuàng)建需要用到的用戶:

useradd -M -s /sbin/nologin nginx

繼續(xù)編譯:

make && make install chown -R nginx:nginx /usr/local/nginx/

這里需要介紹nginx命令的一個參數(shù):

[root@bab3b6991467 ~]# /usr/local/nginx/sbin/nginx -h -g directives : set global directives out of configuration file

-g:為nginx的配置文件設(shè)置指令

現(xiàn)在退出container,回到host本機

[root@bab3b6991467 ~]# exit exit

查看此時容器的狀態(tài):

[root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bab3b6991467 centos "/bin/bash" 37 minutes ago Exited (0) 21 seconds ago web

利用docker diff查看該容器進行了哪些修改,由于輸出太多,這里不給予顯示了

利用docker commit將web容器進行加層成一個新鏡像:

[root@docker ~]# docker commit --helpUsage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]Create a new image from a container's changes-m, --message string Commit message-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")

現(xiàn)在開始commit:

[root@docker ~]# docker commit -m "compile nginx on centos" web wadeson/centos_nginx:v1 sha256:210a202d37b8d2c31155c29adf0c7c0b49cfab7ff38234109919de7f4e76d1de

查看本地鏡像:

[root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE wadeson/centos_nginx v1 210a202d37b8 33 seconds ago 464MB nginx latest c59f17fe53b0 4 days ago 108MB ubuntu latest 747cb2d60bbe 3 weeks ago 122MB centos latest 196e0ce0c9fb 6 weeks ago 197MB

可以看見剛剛docker commit的新鏡像了,現(xiàn)在由此鏡像進行啟動一個container提供nginx服務(wù):

[root@docker ~]# docker run -d -p80:80 wadeson/centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;" c12669357e2b09a05a396ac480a04dd1956303b784f894b615d4edb889a737ab

然后查看container:

[root@docker ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c12669357e2b wadeson/centos_nginx:v1 "/usr/local/nginx/..." 41 seconds ago Up 40 seconds 0.0.0.0:80->80/tcp thirsty_murdock

可以看見nginx服務(wù)已經(jīng)開啟了,于是進行訪問:

?

?于是整個手動構(gòu)建就成功了

?

針對上面的一些命令做下解釋:

  docker run -d -p80:80 wadeson/centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"

后面運行的命令都是旨在container的命令,由于沒有進行環(huán)境變量設(shè)置,所以全路徑,而nginx -g這個參數(shù)是指可以在外面添加指令到nginx的配置文件中,daemon off是指nginx服務(wù)不運行在后端而是在前臺運行(container中的服務(wù)必須運行在前臺)

利用docker top可以查看container的運行進程:

[root@docker ~]# docker top c12669357e2b UID PID PPID C STIME TTY TIME CMD root 35468 35451 0 02:55 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -g daemon off; 1000 35489 35468 0 02:55 ? 00:00:00 nginx: worker process

利用docker exec進入到容器內(nèi):

[root@docker ~]# docker exec -it c12669357e2b /bin/bash [root@c12669357e2b /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 06:55 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -g daemon off; nginx 5 1 0 06:55 ? 00:00:00 nginx: worker process root 6 0 1 07:01 pts/0 00:00:00 /bin/bash root 20 6 0 07:01 pts/0 00:00:00 ps -ef

而使用ctrl+p+q可以將該容器置于后臺,而不是馬上exited

總結(jié)

以上是生活随笔為你收集整理的docker之手动构建新的镜像的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。