docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > docker容器服务随容器自动启动

docker容器内服务随容器自动启动的几种方案

作者:石小千

Docker是一种轻量级的容器化平台,可以帮助开发者快速部署、运行和管理应用程序,这篇文章主要给大家介绍了关于docker容器内服务随容器自动启动的几种方案,需要的朋友可以参考下

1. 背景

使用docker运行带有nginx服务的容器。容器启动后,每次都需要手动进入容器内启动nginx服务。修改容器,让nginx服务随容器自动启动。

2. 准备工作

[root@centos7-10 ~]# docker run -it nginx-sj:latest /bin/bash
root@4eb5280856a3:/# 
root@4eb5280856a3:~# pwd
/root
root@4eb5280856a3:~# ls -a
.  ..  .bash_history  .bashrc  .profile  .viminfo

3. 方式一,直接修改.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. 方式二,编写启动脚本加入.bashrc文件(文明一点)

root@4eb5280856a3:/# vim /root/start_nginx.sh
#!/bin/bash

service nginx start 
#service mysql start    //也可以添加其它服务
root@4eb5280856a3:/# chmod +x /root/start_nginx.sh
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. 导出容器制作镜像

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 ~]#
[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 ~]# 
[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 ~]# 
[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. 测试导出容器,nginx服务随容器自动启动

[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 ~]# 
[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

7. 方式三,修改镜像默认值,COMMIT生成新镜像(正规方式)

修改镜像默认值,方式详见官方文档:Overriding image defaults

[root@centos7-10 ~]# docker run -itd -p 9090:80 nginx-sj:latest  nginx -g "daemon off;"
c90c3a7d8e56ea15017fdfa2dfe9b88d398dcfe16f76b9723f0eb884208d6999
[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
[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": {}
        },
 ......
        }
    }
]
[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
[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  

8. 方式四,与方案三类似

[root@centos7-10 ~]# docker run -itd --entrypoint /usr/sbin/nginx nginx-sj:latest -g "daemon off;"

ENTRYPOINT 与 CMD 关系及使用方式详见官方文档:Understand how CMD and ENTRYPOINT interact

说明:因为docker run 命令中–entrypoint 只支持string,所以建议将服务启动命令写成shell脚本(同方案二),然后使用–entrypoint 引入更合理

9. 方式五,使用DOCKERFILE重新BUILTD新镜像(永久使用方式)

[root@centos7-10 ~]# mkdir nginx-sj
[root@centos7-10 ~]# cd nginx-sj
[root@centos7-10 ~]# vim dockerfile-nginx
FROM nginx-sj:latest
MAINTAINER shijin
CMD ["nginx","-g","daemon off;"]
~
~
wq!
[root@centos7-10 ~]# docker build -f dockerfile-nginx -t nginx-sj:2024022601 .
[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 -

总结:

到此这篇关于docker容器内服务随容器自动启动的几种方案的文章就介绍到这了,更多相关docker容器服务随容器自动启动内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文