docker 容器内服务随容器自动启动
- 背景
- 准备工作
- 方案一,直接修改.bashrc文件(简单粗暴)
- 方案二,编写启动脚本加入.bashrc文件(文明一点)
- 制作nginx服务自启动镜像
- 测试新镜像,nginx服务随容器自动启动
背景
使用docker运行带有nginx服务的容器。容器启动后,每次都需要手动进入容器内启动nginx服务。修改容器内./bashrc文件,让nginx服务随容器自动启动。
容器启动和Linux系统一样,都会随系统启动加载~/.bashrc,所以修改此文件,加入服务启动项。
准备工作
- 使用原nginx-sj: latest镜像文件运行一个容器
nginx-sj: latest 是我之前使用,已修改的官方nginx镜像文件。
[root@centos7-10 ~]# docker run -itd nginx-sj:latest /bin/bash
root@4eb5280856a3:/#
- 查看用户目录下.bashrc文件
root@4eb5280856a3:~# pwd
/root
root@4eb5280856a3:~# ls -a
. .. .bash_history .bashrc .profile .viminfo
方案一,直接修改.bashrc文件(简单粗暴)
- 直接将服务启动命名写入.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
方案二,编写启动脚本加入.bashrc文件(文明一点)
- 编辑一个start_nginx.sh文件
root@4eb5280856a3:/# vim /root/start_nginx.sh
- 写入以下内容,并保存
#!/bin/bashservice nginx start
#service mysql start //也可以添加其它服务
- 添加start_nginx.sh脚本执行权限
root@4eb5280856a3:/# chmod +x /root/start_nginx.sh
- 将启动脚本写入.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
制作nginx服务自启动镜像
- 将当前容器(4eb5280856a3)导出成文件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 ~]#
- 删除当前容器(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:
4eb5280856a32b83046e8e3be0393028a1a3f328887a11c0ccff15384660f86eTotal 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 ~]#
- 导入创建新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 ~]#
测试新镜像,nginx服务随容器自动启动
- 启动容器,宿主机发布端口:9090,容器内服务端口: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 ~]#
- 宿主机已监听端口9090
- 宿主机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 1000link/ether 00:1c:42:ae:b6:41 brd ff:ff:ff:ff:ff:ffinet 10.211.55.10/24 brd 10.211.55.255 scope global noprefixroute dynamic enp0s5valid_lft 1140sec preferred_lft 1140secinet6 fdb2:2c26:f4e4:0:233e:38df:2cbd:cec1/64 scope global noprefixroute dynamic valid_lft 2591662sec preferred_lft 604462secinet6 fe80::7e0c:1902:e1ca:4324/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft foreverinet6 fe80::567a:248b:5e94:5d19/64 scope link noprefixroute valid_lft forever preferred_lft forever
- 访问宿主机HTTP://10.211.55.10:9090,成功!