IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> Docker 创建LNMP镜像 -> 正文阅读

[系统运维]Docker 创建LNMP镜像

基于centos镜像编译部署LNMP

环境准备

拉取centos官方镜像

[root@docker ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

[root@docker ~]# docker images    //查看
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
httpd        latest    ad17c88403e2   13 days ago    143MB
tomcat       latest    904a98253fbf   13 days ago    680MB
nginx        latest    ea335eea17ab   2 weeks ago    141MB
centos       latest    5d0da3dc9764   2 months ago   231MB

Nginx

运行一个nginx容器

[root@docker ~]# docker run -d --name nginx -it centos /bin/bash
461402a331c25c9afa02933bafce6762120e7d7dc83a31d3f4748bc614dd2db1

[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                   PORTS     NAMES
461402a331c2   centos    "/bin/bash"              15 seconds ago   Up 13 seconds                      nginx

容器正常运行后,将nginx源码包从宿主机上传输到容器中

[root@docker ~]# docker cp /usr/src/nginx-1.20.1.tar.gz 461402a331c2:/usr/local

进入容器查看

[root@docker ~]# docker exec -it 461402a331c2 /bin/bash
[root@461402a331c2 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  start.sh  sys  tmp  usr  var
[root@461402a331c2 /]# cd /usr/local/
[root@461402a331c2 local]# ls
bin  etc  games  include  lib  lib64  libexec   nginx-1.20.1.tar.gz  sbin  share  src

下载编译工具

[root@461402a331c2 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make

创建nginx用户

[root@461402a331c2 ~]# useradd -r -M -s /sbin/nologin nginx

创建nginx日志目录

[root@461402a331c2 ~]# mkdir -p /var/log/nginx
[root@461402a331c2 ~]# chown -R nginx.nginx /var/log/nginx/

编译安装nginx

[root@461402a331c2 ~]# cd /usr/local/
[root@461402a331c2 local]# ls
bin  etc  games  include  lib  lib64  libexec  nginx-1.20.1.tar.gz  sbin  share  src
[root@461402a331c2 local]# tar xf nginx-1.20.1.tar.gz 
[root@461402a331c2 local]# ls
bin  etc  games  include  lib  lib64  libexec  nginx-1.20.1  nginx-1.20.1.tar.gz  sbin  share  src
[root@461402a331c2 local]# cd nginx-1.20.1
[root@461402a331c2 nginx-1.20.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@461402a331c2 nginx-1.20.1]# ./configure \
 --prefix=/usr/local/nginx \
 --user=nginx \
 --group=nginx \
 --with-debug \
 --with-http_ssl_module \
 --with-http_realip_module \
 --with-http_image_filter_module \
 --with-http_gunzip_module \
 --with-http_gzip_static_module \
 --with-http_stub_status_module \
 --http-log-path=/var/log/nginx/access.log \
 --error-log-path=/var/log/nginx/error.log


[root@461402a331c2 nginx-1.20.1]# make && make install

配置环境变量

[root@461402a331c2 nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@461402a331c2 nginx-1.20.1]# source /etc/profile.d/nginx.sh

启动服务

[root@461402a331c2 nginx-1.20.1]# nginx   
[root@461402a331c2 nginx-1.20.1]# ss -anlt
State               Recv-Q              Send-Q                           Local Address:Port                           Peer Address:Port             Process              
LISTEN              0                   128                                    0.0.0.0:80                                  0.0.0.0:*     

修改nginx配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
        ......
        #access_log  logs/host.access.log  main;

        location / {
            root     html;  
            index  index.php index.html  index.htm;  #在此行中添加index.php
        }

        #error_page  404              /404.html;
       ......



        location ~ \.php$ {       
            root           /var/www/html;  指定index.php 目录存放位置(php主机容器上)
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;   #将/script改为$document_root
            include        fastcgi_params;
        }

编译一个自启动脚本

[root@461402a331c2 ~]# cd /
[root@461402a331c2 /]# vi start.sh
#! /bin/sh
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/bin/bash

[root@461402a331c2 /]# chmod +x start.sh 
[root@461402a331c2 /]# exit

设置完成后,将此容器制作成一个镜像(并指定默认进程)

[root@docker ~]# docker commit -p -c 'CMD ["/bin/bash","/start.sh"]' 461402a331c2 xm17671855780/nginx:v0.1
sha256:178fc2a97021930dee4fd7b48abe181806dbedb603cd541c8876d42ff2b0c19f

[root@docker ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED              SIZE
xm17671855780/nginx   v0.1      178fc2a97021   About a minute ago   549MB
xm17671855780/php     v0.1      bec4eea1d025   55 minutes ago       1.46GB
xm17671855780/php     v0.2      bec4eea1d025   55 minutes ago       1.46GB
xm17671855780/mysql   v0.1      1a3e53bdee96   2 hours ago          3.8GB
xm17671855780/mysql   v0.2      1a3e53bdee96   2 hours ago          3.8GB

使用新创建的镜像运行容器进行验证

[root@docker ~]# docker run -dit --name testnginx 178fc2a97021
8df283af091dc1a4255abeaa7f947b5597b01256f0ba2572ebc113f8cb96a176

[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE                   COMMAND                 CREATED          STATUS          PORTS     NAMES
8df283af091d   178fc2a97021            "/bin/bash /start.sh"   6 seconds ago    Up 4 seconds    80/tcp    testnginx
8948fd64a00d   xm17671855780/i1:v0.3   "/bin/bash /start.sh"   30 minutes ago   Up 30 minutes   80/tcp    nginx
fff249b8ff9f   bec4eea1d025            "bin/bash /start.sh"    42 minutes ago   Up 42 minutes             testphp
8287f0933b3c   centos                  "/bin/bash"             2 hours ago      Up 2 hours                php
f5c74efdcf21   1a3e53bdee96            "/bin/bash /start.sh"   2 hours ago      Up 2 hours                test
d4f0ff27b5f8   centos                  "/bin/bash"             3 hours ago      Up 3 hours                mysql


[root@docker ~]# docker exec -it testnginx /bin/bash
[root@8df283af091d /]# ss -anlt
State             Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port            Process            
LISTEN            0                 128                                0.0.0.0:80                              0.0.0.0:*                                  

Mysql

运行一个mysql容器

[root@docker ~]# docker run -dit --name mysql centos
d4f0ff27b5f8ff8bcc6bb14ab37578085f20057452df9803999145d5fdb056d6

[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
d4f0ff27b5f8   centos    "/bin/bash"   20 seconds ago   Up 19 seconds             mysql

将mysql源码包上传至mysql容器中

[root@docker ~]# docker cp /usr/src/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz mysql:/usr/src

进入容器查看

[root@docker ~]# docker exec -it mysql /bin/bash
[root@d4f0ff27b5f8 /]# cd /usr/src/
[root@d4f0ff27b5f8 src]# ls
debug  kernels  mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz

下载依赖包

[root@d4f0ff27b5f8 src]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs numactl libaio*

创建用户和组

[root@d4f0ff27b5f8 src]# groupadd -r  mysql
[root@d4f0ff27b5f8 src]# useradd -r -M -s /sbin/nologin -g mysql mysql

解压并链接

[root@d4f0ff27b5f8 src]# tar xf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@d4f0ff27b5f8 src]# cd /usr/local/
[root@d4f0ff27b5f8 local]# ln -sv mysql-5.7.35-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.35-linux-glibc2.12-x86_64/'

修改目录/usr/local/mysql的属主属组

[root@d4f0ff27b5f8 local]# chown -R mysql.mysql /usr/local/mysql

添加环境变量

[root@d4f0ff27b5f8 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@d4f0ff27b5f8 local]# . /etc/profile.d/mysql.sh
[root@d4f0ff27b5f8 local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

创建数据存放目录

[root@d4f0ff27b5f8 local]# mkdir -p /opt/data
[root@d4f0ff27b5f8 local]# chown -R mysql.mysql /opt/data/

初始化数据库

[root@d4f0ff27b5f8 local]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
2021-12-03T10:07:30.154113Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-12-03T10:07:30.942127Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-12-03T10:07:31.015793Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-12-03T10:07:31.074855Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: d3ccdeac-5420-11ec-93d2-0242ac110002.
2021-12-03T10:07:31.076482Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-12-03T10:07:31.883696Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-12-03T10:07:31.883720Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-12-03T10:07:31.884305Z 0 [Warning] CA certificate ca.pem is self signed.
2021-12-03T10:07:32.201880Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

配置mysql

[root@d4f0ff27b5f8 local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql' -> '/usr/local/mysql/include/'
[root@d4f0ff27b5f8 local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@d4f0ff27b5f8 local]# ldconfig

生成配置文件

[root@d4f0ff27b5f8 ~]# vi /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

配置mysql.server

[root@d4f0ff27b5f8 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@d4f0ff27b5f8 ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /usr/local/mysql/support-files/mysql.server
[root@d4f0ff27b5f8 ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /usr/local/mysql/support-files/mysql.server

启动

[root@d4f0ff27b5f8 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/d4f0ff27b5f8.err'.
 SUCCESS! 

[root@d4f0ff27b5f8 ~]# ss -anlt
State             Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port            Process            
LISTEN            0                 80                                       *:3306                                  *:*                                        

设置新密码

[root@d4f0ff27b5f8 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.



mysql> set password = password('xu1!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

编写一个启动脚本

[root@d4f0ff27b5f8 ~]# cd /
[root@d4f0ff27b5f8 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

[root@d4f0ff27b5f8 /]# vi start.sh
#!/bin/sh
/etc/init.d/mysqld start
/bin/bash

[root@d4f0ff27b5f8 /]# chmod +x start.sh 

设置完成后,将此容器制作成一个镜像(并指定默认进程)

[root@docker ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED        SIZE
xm17671855780/i1   v0.2      81869f360a9b   25 hours ago   549MB
busybox            latest    d23834f29b38   3 days ago     1.24MB
httpd              latest    ad17c88403e2   2 weeks ago    143MB
tomcat             latest    904a98253fbf   2 weeks ago    680MB
nginx              latest    ea335eea17ab   2 weeks ago    141MB
centos             latest    5d0da3dc9764   2 months ago   231MB

[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
d4f0ff27b5f8   centos    "/bin/bash"   34 minutes ago   Up 34 minutes             mysql

[root@docker ~]# docker commit -p -c 'CMD ["/bin/bash","/start.sh"]' d4f0ff27b5f8 xm17671855780/mysql:v0.1
sha256:1a3e53bdee9624539586dbe485ad016ba9caf74c96631e7d04c908dabf91c6e4

[root@docker ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED              SIZE
xm17671855780/mysql   v0.1      1a3e53bdee96   About a minute ago   3.8GB
xm17671855780/i1      v0.2      81869f360a9b   25 hours ago         549MB
busybox               latest    d23834f29b38   3 days ago           1.24MB
httpd                 latest    ad17c88403e2   2 weeks ago          143MB
tomcat                latest    904a98253fbf   2 weeks ago          680MB
nginx                 latest    ea335eea17ab   2 weeks ago          141MB
centos                latest    5d0da3dc9764   2 months ago         231MB

[root@docker ~]# docker tag 1a3e53bdee96 xm17671855780/mysql:v0.2
[root@docker ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
xm17671855780/mysql   v0.1      1a3e53bdee96   2 minutes ago   3.8GB
xm17671855780/mysql   v0.2      1a3e53bdee96   2 minutes ago   3.8GB
xm17671855780/i1      v0.2      81869f360a9b   25 hours ago    549MB
busybox               latest    d23834f29b38   3 days ago      1.24MB
httpd                 latest    ad17c88403e2   2 weeks ago     143MB
tomcat                latest    904a98253fbf   2 weeks ago     680MB
nginx                 latest    ea335eea17ab   2 weeks ago     141MB
centos                latest    5d0da3dc9764   2 months ago    231MB

使用新创建的镜像运行容器进行验证

[root@docker ~]# docker run -dit --name testmysql 1a3e53bdee96
cc2a3658c32bc26748deb1f30b54a28109519d07d5aaee55e84f95c75d5454ef

[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE                   COMMAND                 CREATED          STATUS          PORTS     NAMES
cc2a3658c32b   1a3e53bdee96            "/bin/bash /start.sh"   7 seconds ago    Up 6 seconds              testmysql
8df283af091d   178fc2a97021            "/bin/bash /start.sh"   13 minutes ago   Up 13 minutes   80/tcp    testnginx
8948fd64a00d   xm17671855780/i1:v0.3   "/bin/bash /start.sh"   44 minutes ago   Up 44 minutes   80/tcp    nginx
fff249b8ff9f   bec4eea1d025            "bin/bash /start.sh"    56 minutes ago   Up 56 minutes             testphp

[root@docker ~]# docker exec -it testmysql /bin/bash
[root@cc2a3658c32b /]# ss -anlt
State             Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port            Process            
LISTEN            0                 80                                       *:3306                                  *:*                                  

PHP

创建一个php容器

[root@docker ~]# docker run -dit --name php centos /bin/bash
8287f0933b3c32501ffba8e5a72ec1244b8478813436c83213c941d6acdaa4f1

[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                 CREATED          STATUS          PORTS     NAMES
8287f0933b3c   centos         "/bin/bash"             7 seconds ago    Up 6 seconds              php
f5c74efdcf21   1a3e53bdee96   "/bin/bash /start.sh"   5 minutes ago    Up 5 minutes              test
d4f0ff27b5f8   centos         "/bin/bash"             46 minutes ago   Up 46 minutes             mysql

将php源码包传输到php容器中

[root@docker ~]# docker cp /usr/src/php-7.4.25.tar.gz php:/usr/src

进入容器中查看

[root@docker ~]# docker exec -it php /bin/bash
[root@8287f0933b3c /]# cd /usr/src/
[root@8287f0933b3c src]# ls
debug  kernels  php-7.4.25.tar.gz

安装 epel-release

yum -y install epel-release

下载编译工具

[root@8287f0933b3c php-7.4.25]# yum -y install gcc gcc-c++ make

下载依赖包

[root@8287f0933b3c src]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

因为libcurl-minimal-7.61.1-18.el8.x86_64 与 libcurl-7.61.1-22.el8.x86_64 提供的 libcurl(x86-64) 冲突 (所以得分开下载)

[root@8287f0933b3c src]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel  libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp   gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt      libxslt-devel mhash mhash-devel php-mysqlnd libzip-devel libsqlite3x libsqlite3x-devel oniguruma   libzip-devel

再 下载libcurl-devel libcurl就会作为依赖包被安装

[root@8287f0933b3c src]#yum -y install libcurl-devel 

编译PHP

[root@8287f0933b3c ~]# cd /usr/src/
[root@8287f0933b3c src]# ls
debug  kernels   php-7.4.25.tar.gz
[root@8287f0933b3c src]# tar xf php-7.4.25.tar.gz
[root@8287f0933b3c src]# ls
debug  kernels  php-7.4.25  php-7.4.25.tar.gz
[root@8287f0933b3c src]# cd php-7.4.25 
[root@8287f0933b3c php-7.4.25]# ./configure --prefix=/usr/local/php  \
--with-config-file-path=/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix

[root@8287f0933b3c php-7.4.25]# make install

配置环境变量

[root@8287f0933b3c php-7.4.25]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh 
[root@8287f0933b3c php-7.4.25]# source /etc/profile.d/php.sh

生成配置文件

[root@8287f0933b3c php-7.4.25]# cp php.ini-production  /etc/php.ini
cp: overwrite '/etc/php.ini'? yes
[root@8287f0933b3c php-7.4.25]# cp sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm
[root@8287f0933b3c php-7.4.25]# chmod +x /etc/rc.d/init.d/php-fpm
[root@8287f0933b3c php-7.4.25]# cd /usr/local/php/
[root@8287f0933b3c php]# ls
bin  etc  include  lib  php  sbin  var
[root@8287f0933b3c php]# cd etc/
[root@8287f0933b3c etc]# cp php-fpm.conf.default  php-fpm.conf
[root@8287f0933b3c etc]# cd php-fpm.d/
[root@8287f0933b3c php-fpm.d]# cp www.conf.default  www.conf

创建/var/www/html/目录并添加测试文件

[root@f332b17928ba /]# mkdir -p /var/www/html
[root@f332b17928ba /]# cd /var/www/html/
[root@f332b17928ba html]# vi index.php
<?php
        phpinfo();
?>

启动服务

[root@8287f0933b3c ~]# /usr/local/php/sbin/php-fpm 
[root@8287f0933b3c ~]# ss -anlt
State             Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port            Process            
LISTEN            0                 128                              127.0.0.1:9000                            0.0.0.0:*                                  

编写一个脚本

[root@8287f0933b3c /]# vi start.sh
#! /bin/bash
/usr/local/php/sbin/php-fpm
/bin/bash

[root@8287f0933b3c /]# chmod +x start.sh 

测试

[root@8287f0933b3c /]# pkill php-fpm
[root@8287f0933b3c /]# ss -anlt
State            Recv-Q            Send-Q                       Local Address:Port                        Peer Address:Port            Process            

[root@8287f0933b3c /]# ./start.sh 
[root@8287f0933b3c /]# ss -anlt
State             Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port            Process            
LISTEN            0                 128                              127.0.0.1:9000                            0.0.0.0:*                                  

设置完成后,将此容器制作成一个镜像(并指定默认进程)

[root@docker ~]# docker commit -p -c 'CMD ["bin/bash","/start.sh"]' 8287f0933b3c xm17671855780/php:v0.2
sha256:bec4eea1d025e8145539336f63c01d37477b5dccd9e94b46a13fdf5ca646364e

[root@docker ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
xm17671855780/php     v0.1      bec4eea1d025   4 minutes ago   1.46GB
xm17671855780/mysql   v0.1      1a3e53bdee96   2 hours ago     3.8GB
xm17671855780/mysql   v0.2      1a3e53bdee96   2 hours ago     3.8GB
xm17671855780/i1      v0.2      81869f360a9b   27 hours ago    549MB
busybox               latest    d23834f29b38   3 days ago      1.24MB
httpd                 latest    ad17c88403e2   2 weeks ago     143MB
tomcat                latest    904a98253fbf   2 weeks ago     680MB
nginx                 latest    ea335eea17ab   2 weeks ago     141MB
centos                latest    5d0da3dc9764   2 months ago    231MB

[root@docker ~]# docker tag bec4eea1d025 xm17671855780/php:v0.2
[root@docker ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
xm17671855780/php     v0.2      bec4eea1d025   9 minutes ago   1.46GB

使用新的镜像,创建一个容器,验证

[root@docker ~]# docker run -dit --name testphp bec4eea1d025
fff249b8ff9f0b04d2f05c6f836e72f186ded013e3bf8d5d5e2a41c11f802237
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                 CREATED         STATUS         PORTS     NAMES
fff249b8ff9f   bec4eea1d025   "bin/bash /start.sh"    7 seconds ago   Up 6 seconds             testphp
8287f0933b3c   centos         "/bin/bash"             2 hours ago     Up 2 hours               php
f5c74efdcf21   1a3e53bdee96   "/bin/bash /start.sh"   2 hours ago     Up 2 hours               test
d4f0ff27b5f8   centos         "/bin/bash"             2 hours ago     Up 2 hours               mysql

[root@docker ~]# docker exec -it fff249b8ff9f /bin/bash
[root@fff249b8ff9f /]# ss -anlt
State             Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port            Process            
LISTEN            0                 128                              127.0.0.1:9000                            0.0.0.0:*                                  

当所有镜像都生成完成后进行如下操作:

使用nginx镜像创建一个容器并进行端口映射

[root@docker ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED             SIZE
xm17671855780/ngin    v0.2      25f7843775cf   About an hour ago   549MB
xm17671855780/nginx   v0.1      178fc2a97021   3 hours ago         549MB
xm17671855780/php     v0.1      bec4eea1d025   4 hours ago         1.46GB
xm17671855780/php     v0.2      bec4eea1d025   4 hours ago         1.46GB
xm17671855780/mysql   v0.1      1a3e53bdee96   5 hours ago         3.8GB
xm17671855780/mysql   v0.2      1a3e53bdee96   5 hours ago         3.8GB
xm17671855780/i1      v0.3      8be6aa7baa79   14 hours ago        549MB
xm17671855780/i1      v0.2      81869f360a9b   31 hours ago        549MB
busybox               latest    d23834f29b38   3 days ago          1.24MB
httpd                 latest    ad17c88403e2   2 weeks ago         143MB
tomcat                latest    904a98253fbf   2 weeks ago         680MB
nginx                 latest    ea335eea17ab   2 weeks ago         141MB
centos                latest    5d0da3dc9764   2 months ago        231MB

[root@docker ~]# docker run -dit --name nginx -p 80:80 25f7843775cf 
f332b17928bad2664acef007b1267241467390353d7964dde2008d9b64e0ee87

[root@f332b17928ba html]# ss -anlt
State             Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port            Process            
LISTEN            0                 128                                0.0.0.0:80                              0.0.0.0:*                                  

使用mysql镜像创建一个容器并用容器模式运行

[root@docker ~]# docker run -dit --name mysql --network container:nginx 1a3e53bdee96
495677c948cc85309b459daac2a2de8dd4df3d2b4a772df73a8a031b4fbd0b70

[root@f332b17928ba /]# ss -anlt
State             Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port            Process            
LISTEN            0                 128                                0.0.0.0:80                              0.0.0.0:*                                  
LISTEN            0                 80                                       *:3306                                  *:*                                  

使用php镜像创建一个容器并用容器模式运行

[root@docker ~]# docker run -it --name php --network container:nginx bec4eea1d025

[root@f332b17928ba /]# ss -anlt  //此时发现三个服务端口已启动
State             Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port            Process            
LISTEN            0                 128                                0.0.0.0:80                              0.0.0.0:*                                  
LISTEN            0                 128                              127.0.0.1:9000                            0.0.0.0:*                                  
LISTEN            0                 80                                       *:3306                                  *:*                                  

到宿主机查看端口(发现有80端口监听)

[root@docker ~]# ss -anlt
State               Recv-Q               Send-Q                             Local Address:Port                             Peer Address:Port              
LISTEN              0                    128                                      0.0.0.0:80                                    0.0.0.0:*                 
LISTEN              0                    128                                      0.0.0.0:22                                    0.0.0.0:*                 
LISTEN              0                    128                                         [::]:80                                       [::]:*                 
LISTEN              0                    128                                         [::]:22                                       [::]:*                 

访问网站测试
宿主机ip+port
在这里插入图片描述

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-12-04 13:48:55  更:2021-12-04 13:50:08 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/16 2:42:29-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码