docker容器內(nèi)服務(wù)隨容器自動(dòng)啟動(dòng)的幾種方案
1. 背景
使用docker運(yùn)行帶有nginx服務(wù)的容器。容器啟動(dòng)后,每次都需要手動(dòng)進(jìn)入容器內(nèi)啟動(dòng)nginx服務(wù)。修改容器,讓nginx服務(wù)隨容器自動(dòng)啟動(dòng)。
2. 準(zhǔn)備工作
- 使用原nginx-sj: latest鏡像文件運(yùn)行一個(gè)容器
nginx-sj: latest 是我之前使用,已修改的官方nginx鏡像文件。
[root@centos7-10 ~]# docker run -it nginx-sj:latest /bin/bash root@4eb5280856a3:/#
- 查看用戶目錄下.bashrc文件
root@4eb5280856a3:~# pwd /root root@4eb5280856a3:~# ls -a . .. .bash_history .bashrc .profile .viminfo
3. 方式一,直接修改.bashrc文件(簡單粗暴)
- 直接將服務(wù)啟動(dòng)命名寫入.bashrc文件,見下
root@4eb5280856a3:~# vim /root/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells. # Note: PS1 and umask are already set in /etc/profile. You should not # need this unless you want different defaults for root. # PS1='${debian_chroot:+($debian_chroot)}\h:\w\$ ' # umask 022 # You may uncomment the following lines if you want 'ls' to be colorized: # export LS_OPTIONS='--color=auto' # eval "$(dircolors)" # alias ls='ls $LS_OPTIONS' # alias ll='ls $LS_OPTIONS -l' # alias l='ls $LS_OPTIONS -lA' # # Some more alias to avoid making mistakes: # alias rm='rm -i' # alias cp='cp -i' # alias mv='mv -i' ############################################ # start nginx if [ -f /etc/init.d/nginx ]; then /etc/init.d/nginx start fi
4. 方式二,編寫啟動(dòng)腳本加入.bashrc文件(文明一點(diǎn))
- 編輯一個(gè)start_nginx.sh文件
root@4eb5280856a3:/# vim /root/start_nginx.sh
- 寫入以下內(nèi)容,并保存
#!/bin/bash service nginx start #service mysql start //也可以添加其它服務(wù)
- 添加start_nginx.sh腳本執(zhí)行權(quán)限
root@4eb5280856a3:/# chmod +x /root/start_nginx.sh
- 將啟動(dòng)腳本寫入.bashrc文件
root@4eb5280856a3:~# vim /root/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells. # Note: PS1 and umask are already set in /etc/profile. You should not # need this unless you want different defaults for root. # PS1='${debian_chroot:+($debian_chroot)}\h:\w\$ ' # umask 022 # You may uncomment the following lines if you want 'ls' to be colorized: # export LS_OPTIONS='--color=auto' # eval "$(dircolors)" # alias ls='ls $LS_OPTIONS' # alias ll='ls $LS_OPTIONS -l' # alias l='ls $LS_OPTIONS -lA' # # Some more alias to avoid making mistakes: # alias rm='rm -i' # alias cp='cp -i' # alias mv='mv -i' ############################################ # start nginx if [ -f /root/start_nginx.sh ]; then /root/start_nginx.sh fi
5. 導(dǎo)出容器制作鏡像
- 將當(dāng)前容器(4eb5280856a3)導(dǎo)出成文件nginx-sj_20240222.tar
root@4eb5280856a3:~# exit exit [root@centos7-10 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4eb5280856a3 nginx-sj:latest "/bin/bash" 32 minutes ago Up 30 minutes vibrant_mcnulty [root@centos7-10 ~]# docker export -o nginx-sj_20240222.tar 4eb5280856a3 [root@centos7-10 ~]#
- 刪除當(dāng)前容器(4eb5280856a3)
[root@centos7-10 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4eb5280856a3 nginx-sj:latest "/bin/bash" 34 minutes ago Up 32 minutes vibrant_mcnulty [root@centos7-10 ~]# docker stop 4eb5280856a3 4eb5280856a3 [root@centos7-10 ~]# docker container prune WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N] y Deleted Containers: 4eb5280856a32b83046e8e3be0393028a1a3f328887a11c0ccff15384660f86e Total reclaimed space: 7.293kB [root@centos7-10 ~]#
- 刪除舊nginx-sj:latest鏡像
[root@centos7-10 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx-sj latest 3e97da40406a About an hour ago 216MB ubuntu22 latest caac235feb32 About an hour ago 338MB busybox latest beae173ccac6 2 years ago 1.24MB redis latest 7614ae9453d1 2 years ago 113MB [root@centos7-10 ~]# docker rmi nginx-sj:latest Untagged: nginx-sj:latest Deleted: sha256:3e97da40406a0daaa4d8c0d948b4c3a8a3d099b3aadb0d9fe8a2be4389bd52e6 [root@centos7-10 ~]#
- 導(dǎo)入創(chuàng)建新nginx-sj: latest鏡像,新鏡像ID:283bb24f8ff4
[root@centos7-10 ~]# docker import nginx-sj_20240222.tar nginx-sj:latest sha256:283bb24f8ff40c67a5ff9d33386847182567f688d7b1b4b109c17054e661b947 [root@centos7-10 ~]# docker images -a nginx-sj:latest REPOSITORY TAG IMAGE ID CREATED SIZE nginx-sj latest 283bb24f8ff4 About a minute ago 216MB [root@centos7-10 ~]#
6. 測(cè)試導(dǎo)出容器,nginx服務(wù)隨容器自動(dòng)啟動(dòng)
- 啟動(dòng)容器,宿主機(jī)發(fā)布端口:9090,容器內(nèi)服務(wù)端口:80
[root@centos7-10 ~]# docker run -itd --rm -p 9090:80 nginx-sj:latest /bin/bash 562e64af48bbb26f95f3bf3fd01a3550898ca05292f8d95b9bf604c2000d2953 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 562e64af48bb nginx-sj:latest "/bin/bash" 3 minutes ago Up 3 minutes 0.0.0.0:9090->80/tcp, :::9090->80/tcp nervous_golick [root@centos7-10 ~]#
- 宿主機(jī)已監(jiān)聽端口9090
- 宿主機(jī)IP 10.211.55.10
[root@centos7-10 ~]# netstat -ntlp | grep 9090 tcp 0 0 0.0.0.0:9090 0.0.0.0:* LISTEN 17044/docker-proxy tcp6 0 0 :::9090 :::* LISTEN 17050/docker-proxy [root@centos7-10 ~]# ip a show enp0s5 2: enp0s5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:1c:42:ae:b6:41 brd ff:ff:ff:ff:ff:ff inet 10.211.55.10/24 brd 10.211.55.255 scope global noprefixroute dynamic enp0s5 valid_lft 1140sec preferred_lft 1140sec inet6 fdb2:2c26:f4e4:0:233e:38df:2cbd:cec1/64 scope global noprefixroute dynamic valid_lft 2591662sec preferred_lft 604462sec inet6 fe80::7e0c:1902:e1ca:4324/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft forever inet6 fe80::567a:248b:5e94:5d19/64 scope link noprefixroute valid_lft forever preferred_lft forever
- 訪問宿主機(jī)HTTP://10.211.55.10:9090,成功!
7. 方式三,修改鏡像默認(rèn)值,COMMIT生成新鏡像(正規(guī)方式)
修改鏡像默認(rèn)值,方式詳見官方文檔:Overriding image defaults
- 修改鏡像Cmd默認(rèn)值(之前是null)
- nginx -g “daemon off;”
[root@centos7-10 ~]# docker run -itd -p 9090:80 nginx-sj:latest nginx -g "daemon off;" c90c3a7d8e56ea15017fdfa2dfe9b88d398dcfe16f76b9723f0eb884208d6999
- 提交生成新鏡像:nginx-sj:1
[root@centos7-10 ~]# docker commit -m "nginx start" c9 nginx-sj:1 sha256:94fa4087e73dd3c5440f7538d57dcd2f80938e0f9e8f87d48a866f7542f3d685 [root@centos7-10 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx-sj 1 94fa4087e73d 12 seconds ago 216MB nginx-sj 2 355dbfe22182 30 minutes ago 216MB nginx-sj latest 283bb24f8ff4 25 hours ago 216MB
- 查看新鏡像nginx-sj:1信息
- “Cmd”: [ “nginx”, “-g”, “daemon off;” ],
[root@centos7-10 ~]# docker image inspect nginx-sj:1 [ { "Id": "sha256:94fa4087e73dd3c5440f7538d57dcd2f80938e0f9e8f87d48a866f7542f3d685", "RepoTags": [ "nginx-sj:1" ], "RepoDigests": [], "Parent": "sha256:283bb24f8ff40c67a5ff9d33386847182567f688d7b1b4b109c17054e661b947", "Comment": "nginx start", "Created": "2024-02-23T09:55:38.972444927Z", "Container": "c90c3a7d8e56ea15017fdfa2dfe9b88d398dcfe16f76b9723f0eb884208d6999", "ContainerConfig": { "Hostname": "c90c3a7d8e56", ...... "Config": { "Hostname": "c90c3a7d8e56", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} }, "Tty": true, "OpenStdin": true, "StdinOnce": false, "Env": null, "Cmd": [ "nginx", "-g", "daemon off;" ], "Image": "nginx-sj:latest", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, ...... } } ]
- 運(yùn)行容器進(jìn)行驗(yàn)證
[root@centos7-10 ~]# docker run -itd -p 9000:80 nginx-sj:1 eae91339bf57739cec9fbbd63890afd8949977eae0a561226109b1f02fd66051 [root@centos7-10 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eae91339bf57 nginx-sj:1 "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 0.0.0.0:9000->80/tcp, :::9000->80/tcp modest_cartwright
- 宿主機(jī)已經(jīng)監(jiān)聽9000端口–>容器內(nèi)nginx服務(wù)80端口
[root@centos7-10 ~]# netstat -ntlp | grep 9000 tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 14995/docker-proxy tcp6 0 0 :::9000 :::* LISTEN 15001/docker-proxy
- 外部訪問HTTP://10.211.55.10:9000,成功。
8. 方式四,與方案三類似
- 修改鏡像默認(rèn)值,也可以組合修改ENTRYPOINT 和 CMD。修改方式如下:
[root@centos7-10 ~]# docker run -itd --entrypoint /usr/sbin/nginx nginx-sj:latest -g "daemon off;"
ENTRYPOINT 與 CMD 關(guān)系及使用方式詳見官方文檔:Understand how CMD and ENTRYPOINT interact
- 生存新鏡像方式同方案三
說明:因?yàn)閐ocker run 命令中–entrypoint 只支持string,所以建議將服務(wù)啟動(dòng)命令寫成shell腳本(同方案二),然后使用–entrypoint 引入更合理
9. 方式五,使用DOCKERFILE重新BUILTD新鏡像(永久使用方式)
- 創(chuàng)建一個(gè)dockerfile文件
[root@centos7-10 ~]# mkdir nginx-sj [root@centos7-10 ~]# cd nginx-sj [root@centos7-10 ~]# vim dockerfile-nginx
- 輸入以下內(nèi)容并保存
- FROM nginx-sj:latest # 基礎(chǔ)鏡像
- MAINTAINER # 維護(hù)信息
- CMD [“nginx”,“-g”,“daemon off;”] # 容器啟動(dòng)執(zhí)行的默認(rèn)命令
FROM nginx-sj:latest MAINTAINER shijin CMD ["nginx","-g","daemon off;"] ~ ~ wq!
- 使用dockerfile文件創(chuàng)建新鏡像
- -f dockerfile-nginx dockerfile文件名稱(注意路徑,測(cè)試在當(dāng)前目錄下)
- -t nginx-sj:2024022601 鏡像的名稱與tag
- . 指定鏡像構(gòu)建過程中的上下文環(huán)境
[root@centos7-10 ~]# docker build -f dockerfile-nginx -t nginx-sj:2024022601 .
- 查看構(gòu)建鏡像結(jié)果
[root@centos7-10 nginx-sj]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx-sj 2024022601 64b3c38d4483 36 minutes ago 216MB
[root@centos7-10 nginx-sj]# docker history nginx-sj:2024022601 IMAGE CREATED CREATED BY SIZE COMMENT 64b3c38d4483 53 minutes ago CMD ["nginx" "-g" "daemon off;"] 0B buildkit.dockerfile.v0 <missing> 53 minutes ago MAINTAINER shijin 0B buildkit.dockerfile.v0 <missing> 53 minutes ago 216MB Imported from -
總結(jié):
- 如果臨時(shí)使用,可以采用方式一、二,從容器中導(dǎo)出;
- 如果不頻繁變更,可以采用方式三、四,直接commit生成新鏡像;
- 如果要長期或永久使用,建議采用方式五,徹底重新build新鏡像。
到此這篇關(guān)于docker容器內(nèi)服務(wù)隨容器自動(dòng)啟動(dòng)的幾種方案的文章就介紹到這了,更多相關(guān)docker容器服務(wù)隨容器自動(dòng)啟動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Docker Cloud實(shí)現(xiàn)部署應(yīng)用操作詳解
這篇文章主要介紹了Docker Cloud實(shí)現(xiàn)部署應(yīng)用操作,較為詳細(xì)的分析了Docker Cloud部署應(yīng)用的步驟、命令、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-06-06Docker Nginx容器制作部署實(shí)現(xiàn)方法
這篇文章主要介紹了Docker Nginx容器制作部署實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Docker創(chuàng)建enrollment token錯(cuò)誤異常解析及解決方案
部署完kibana,需要通過enrollment token方式來連接elasticsearch,此時(shí)需要在elasticsearch中創(chuàng)建enrollment token,這篇文章主要介紹了Docker創(chuàng)建enrollment token錯(cuò)誤異常解析及解決方案,需要的朋友可以參考下2024-04-04Docker實(shí)現(xiàn)Mariadb分庫分表及讀寫分離功能
這篇文章主要給大家介紹了關(guān)于Docker實(shí)現(xiàn)Mariadb分庫分表及讀寫分離功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Docker容器導(dǎo)致磁盤空間不足無法訪問的解決方法
本文主要介紹了Docker容器導(dǎo)致磁盤空間不足無法訪問的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Docker快速部署gitlab+gitlab-runner實(shí)例
這篇文章主要介紹了Docker快速部署gitlab+gitlab-runner實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06