> 前言,因为宝塔面板的便捷性,Docker实战封装宝塔面板+自建验证服务器 ## 操作平台 ### Docker 版本 ```shell root@coly:~# docker -v Docker version 18.09.8, build bfed4f5 ``` ### 宿主机操作系统版本 ```shell root@coly:~# cat /proc/version Linux version 3.10.102 (root@build3) (gcc version 4.9.3 20150311 (prerelease) (crosstool-NG 1.20.0) ) #15284 SMP Sat May 19 04:44:02 CST 2018 ``` ## Docker常用命令 ### 运行一个helloworld容器 ```shell root@coly:~# docker pull hello-world Using default tag: latest latest: Pulling from library/hello-world b8dfde127a29: Pull complete Digest: sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c Status: Downloaded newer image for hello-world:latest root@coly:~# docker images | grep hello-world hello-world latest d1165f221234 3 months ago 13.3kB root@coly:~# docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ ps:若是得到如下结果就是运行成功了! ``` ### 查看所有镜像 ```shell root@coly:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE php-cgi_php latest 5b63658cde0f 2 days ago 1.3GB mysql 5.6 3045d3a6e718 3 weeks ago 303MB 770166678/bt latest e79bf6a8184d 2 months ago 6.06GB centos centos8 300e315adb2f 6 months ago 209MB auska/docker-aria2 latest d7c36894faa5 12 months ago 31.7MB nginx 1.17.0 719cd2e3ed04 24 months ago 109MB centos 7.5.1804 cf49811e3cdb 2 years ago 200MB php 7.0-fpm 29e9a8718c7b 2 years ago 357MB ``` ### 列出所有容器 ```shell root@coly:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 44b59347d119 nginx:1.17.0 "nginx -g 'daemon of…" 47 hours ago Up 3 hours 0.0.0.0:8880->80/tcp, 0.0.0.0:8443->443/tcp nginx ee63acb080b8 php-cgi_php "docker-php-entrypoi…" 47 hours ago Up 3 hours 0.0.0.0:9000->9000/tcp php 0be6c28d91a6 mysql:5.6 "docker-entrypoint.s…" 47 hours ago Up 3 hours 0.0.0.0:3306->3306/tcp mysql ``` ### 停止一个容器 ```shell root@coly:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 44b59347d119 nginx:1.17.0 "nginx -g 'daemon of…" 47 hours ago Up 3 hours 0.0.0.0:8880->80/tcp, 0.0.0.0:8443->443/tcp nginx ee63acb080b8 php-cgi_php "docker-php-entrypoi…" 47 hours ago Up 3 hours 0.0.0.0:9000->9000/tcp php 0be6c28d91a6 mysql:5.6 "docker-entrypoint.s…" 47 hours ago Up 3 hours 0.0.0.0:3306->3306/tcp mysql root@coly:~# docker stop 44b59347d119 44b59347d119 root@coly:~# docker ps -a | grep nginx 44b59347d119 nginx:1.17.0 "nginx -g 'daemon of…" 47 hours ago Exited (0) 47 seconds ago ps:终止了容器id:44b59347d119 的这个nginx容器。 ``` ### 启动一个容器 ```shell root@coly:~# docker run auska/docker-aria2 [s6-init] making user provided files available at /var/run/s6/etc...exited 0. [s6-init] ensuring user provided files have correct perms...exited 0. [fix-attrs.d] applying ownership & permissions fixes... [fix-attrs.d] done. [cont-init.d] executing container initialization scripts... [cont-init.d] 01-envfile: executing... [cont-init.d] 01-envfile: exited 0. [cont-init.d] 10-adduser: executing... usermod: no changes ps: 启动名称为 auska/docker-aria2 这个镜像 ``` ### 基本操作 ```shell docker run -d -p 物理端口1:容器端口1 -p 物理端口2:物理端口2 --name 容器名 : docker exec -it 容器名/ID bash ``` ### 磁盘挂载 ```shell docker run -d -p 8080:80 -v 本机路径:容器路径 --name 容器名 : ``` ### 容器打包镜像 ```shell docker commit -a "作者" -m "备注" 容器ID : ``` ### 物理机拷贝到容器 ```shell docker cp test.txt 容器ID:/var/www/html ``` ### 容器拷贝到物理机 ```shell docker cp 容器ID:/var/www/html/test.txt 物理机路径 ``` ### 查看容器 COMMAND ```shell docker ps -a --no-trunc ``` ### 停止所有容器 ```shell docker stop $(dokcer ps -aq) ``` ### 将容器打包成规范的镜像 ```shell docker commit /[:] ``` ### 将镜像修改成规范的镜像 ```shell docker tag /[:] ``` ### 登录 Docker Hub ```shell docker login ``` ### 上传推送镜像到公共仓库 ```shell docker push /: ``` ### 当前目录的 Dockerfile 创建镜像 ```shell docker build -t : . ``` ### 指定文件构建镜像 ```shell docker build -f /path/to/a/Dockerfile -t : . ``` ### 将镜像保存 tar 包 ```shell docker save -o image-name.tar : ``` ### 导入 tar 镜像 ```shell docker load --input image-name.tar ``` ### docker-compose 命令相关 ```shell ## 基本操作 docker-compose up -d ## 关闭并删除容器 docker-compose down ## 开启|关闭|重启已经存在的由docker-compose维护的容器 docker-compose start|stop|restart ## 运行当前内容,并重新构建 docker-compose up -d --build ``` ## 封装宝塔面板 ### 安装官方宝塔面板 ```shell 1.命令行输入: yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh ==================等待出现这段代码============================= Congratulations! Installed successfully! ================================================================== 外网面板地址: http://xxx.xxx.xxx.xxx:8888/3f3fb68d username: blyx0a34 password: 214e6ee5 If you cannot access the panel, release the following panel port [8888] in the security group 若无法访问面板,请检查防火墙/安全组是否有放行面板[8888]端口 ==================等待出现这段代码============================= ``` ### 打包新的镜像 ```shell root@coly:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 94cb73c43a8f centos:7.5.1804 "/bin/bash" 4 minutes ago Up 4 minutes centos7 ee63acb080b8 php-cgi_php "docker-php-entrypoi…" 47 hours ago Up 4 hours 0.0.0.0:9000->9000/tcp php 0be6c28d91a6 mysql:5.6 "docker-entrypoint.s…" 47 hours ago Up 4 hours 0.0.0.0:3306->3306/tcp mysql root@coly:~# docker commit 94cb73c43a8f bt:v1 sha256:80d63d8534ccdefacf7abb7a420e6dbb16f79d645f9d2d409b67e19b190c91fe ps: 找到刚刚我们拿centos7镜像改的containerid:94cb73c43a8f 打包新的一个镜像。 ``` ### 编辑镜像 ```shell root@coly:~# docker run --name bt -it -d -p 6666:8888 bt:v1 e1d8c34b513f6cefbe0c2646179d99292f102fa072f1a40b117414fbf4d1d8de root@coly:~# docker exec -it bt bash [root@e1d8c34b513f /]# ls bin dev etc home install.sh lib lib64 media mnt opt proc root run sbin srv sys tmp usr var www ``` ### 修改堡塔关键配置 ![](https://img.2smile.cn/Note/_image/2021-06-08/2021-06-08-14-22-44.png) ### 核心文件 ```python [root@f7539de78b96 panel]# find ./ -regex ".*\.py\|.*\.js\|.*\.html" -mmin -240 |xargs ls -lta -rw------- 1 root root 273352 Jun 8 19:06 ./BTPanel/static/js/public_backup.js -rw------- 1 root root 76359 Jun 8 19:04 ./BTPanel/static/js/config.js -rw------- 1 root root 11783 Jun 8 19:02 ./class/panelAuth.py -rw------- 1 root root 195884 Jun 8 18:58 ./BTPanel/static/js/soft.js -rw------- 1 root root 46337 Jun 8 18:45 ./class/ajax.py -rw------- 1 root root 92542 Jun 8 18:24 ./class/panelPlugin.py -rw------- 1 root root 221032 Jun 8 18:19 ./BTPanel/static/js/public.js -rw------- 1 root root 56972 Jun 8 18:18 ./BTPanel/static/js/index.js -rw------- 1 root root 10633 Jun 8 18:08 ./BTPanel/templates/default/index.html -rw------- 1 root root 21835 Jun 8 18:06 ./task.py -rw------- 1 root root 24904 Jun 8 18:06 ./tools.py -rw------- 1 root root 41180 Jun 8 18:03 ./class/system.py -rw------- 1 root root 11430 Jun 8 17:43 ./BTPanel/templates/default/layout.html ``` ### 打包镜像到阿里云 1. 获取访问域名,基于当前的网络环境,选择对应的专有网络、公网或经典网络域名。 2. 在终端中输入访问凭证,登录Registry实例 ```shell $ sudo docker login --username= registry.cn-qingdao.aliyuncs.com root@coly:~# sudo docker login --username=<你到阿里云用户名> registry.cn-qingdao.aliyuncs.com Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded ps:登陆成功! root@coly:~# docker push registry.cn-qingdao.aliyuncs.com/xxxx/xxxx:xxxx 2cfdad8c42ce: Pushed dfe0d61bfaa5: Pushed 4826cdadf1ef: Pushed v1: digest: sha256:12340f9591457f265ee8369fcxxxxxbba44bba3332cab425851519 ps: 上传成功! ``` ## 最终效果 ![](https://img.2smile.cn/Note/_image/2021-06-08/2021-06-08%2020.31.24.gif) 最后修改:2021 年 06 月 08 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏