目录
一、实验
1.环境
2.OpenEuler 安装 docker
2.镜像加速
3.docker部署LAMP
二、安装docker报错
2.docker如何快速删除容器与镜像
3.docker创建mysql容器失败
4.docker创建apache容器失败
5.docker创建php-fpm容器失败
6. 80端口与php访问失败
7.httpd容器进入不了
一、实验
1.环境
(1)主机
表1 主机
系统 | 架构 | 版本 | IP | 备注 |
Linux | openEuler | 22.03 LTS SP2 | 192.168.204.144(动态) 192.168.204.141(静态) 192.168.204.142(静态) | |
docker | 25.0.3 |
(2)查看系统版本
[root@localhost ~]# cat /etc/os-release
(3) 查看网络ip
[root@localhost ~]# ip addr
2.OpenEuler 安装 docker
(1)查看当前镜像仓库
[root@localhost ~]# ls /etc/yum.repos.d/
(2)建立yum源文件
添加docker仓库
[root@localhost ~]# sudo vim /etc/yum.repos.d/docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/source/stable
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/debug-$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/source/test
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/debug-$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
(3)安装docker
[root@localhost ~]# sudo dnf install -y docker-ce docker-ce-cli containerd.io
(4)启动docker服务
[root@localhost ~]# sudo systemctl enable --now docker
(5)查看服务状态
[root@localhost ~]# sudo systemctl status docker
(6)查看版本信息
[root@localhost ~]# sudo docker version
[root@localhost ~]# sudo docker --version
2.镜像加速
(1)配置国内的镜像源
[root@localhost ~]# sudo vim /etc/docker/daemon.json
{
"registry-mirrors": ["http://hub-mirror.c.163.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn"]
}
(2)重启服务
[root@localhost ~]# sudo systemctl restart docker
(3) 查看docker信息
[root@localhost ~]# sudo docker info
3.docker部署LAMP
(1)查看当前镜像
[root@localhost ~]# docker images
(2)拉取相关的镜像
[root@localhost ~]# docker pull mysql:8.0.35 && docker pull httpd:latest && docker pull bitnami/php-fpm:latest
(3) 再次查看镜像
[root@localhost ~]# docker images
(4)建立挂载目录
[root@localhost ~]# mkdir -p /lamp/mysql /lamp/php /lamo/www /lamp/apache/conf
查看
[root@localhost ~]# cd /lam
[root@localhost lamp]# ls
(5) 查找文件php.ini
[root@localhost ~]# sudo find / -name 'php.ini'
(6)复制
[root@localhost ~]# cp /usr/local/php/lib/php.ini /lamp/php/
[root@localhost ~]# cd /lamp/php
[root@localhost php]# ls
(7)修改时区
[root@localhost php]# vim php.ini +$
(8)查找
[root@localhost conf]# sudo find / -name 'httpd.conf'
(9)复制
[root@localhost conf]# cp /usr/local/apache/conf/httpd.conf /lamp/apache/conf/
[root@localhost conf]# cd /lamp/apache/conf/
[root@localhost conf]# ls
(9)修改httpd配置文件
[root@localhost conf]# vim httpd.conf
将httpd.conf里的下面三处注释去除(下面IP必须填写PHP容器真实的IP,fpm端口号默认9000,不用变化)
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
末尾添加虚拟主机
<VirtualHost *:80>
ServerAdmin 7jjw@163.com
DocumentRoot "/usr/local/apache2/htdocs"
ServerName localhost
<Directory "/usr/local/apache2/htdocs">
Options None
Require all granted
</Directory>
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.204.141:9000/var/www/$1
</VirtualHost>
(10)创建mysql容器
[root@localhost conf]# docker run -d -p 3306:3306 --restart always -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=Root_321 -v /lamp/mysql:/var/lib/mysql mysql:8.0.35
(11)查看进程
mysql容器为3d开头
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3d95d8d9d54d mysql:8.0.35 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp amazing_goodall
(12)登录建立admin账号
进入3d开头的容器,密码为mysql容器创建时指定的密码Root_321
[root@localhost conf]# docker exec -it 3d mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.35 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create user admin identified by 'admin';
Query OK, 0 rows affected (0.05 sec)
mysql> grant all on *.* to admin;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@localhost conf]#
(13)创建apache容器
[root@localhost conf]# docker run -d -p 80:80 --restart always -e TZ=Asia/Shanghai -v /lamp/www:/usr/local/apache2/htdocs -v /lamp/apache/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf httpd:latest
(14)查看进程
apache容器为21开头
[root@localhost conf]# docker ps |grep httpd
21d4521e77b0 httpd:latest "httpd-foreground" 16 minutes ago Up 16 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp goofy_aryabhata
(15) 创建php-fpm 容器
[root@localhost conf]# docker run -d -p 9000:9000 --restart always -e TZ=Asia/Shanghai -v /lamp/www:/var/www -v /lamp/php/php.ini:/opt/bitnami/php/etc/php.ini bitnami/php-fpm:latest
(16)查看进程
php-fpm容器为31开头
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21d4521e77b0 httpd:latest "httpd-foreground" 14 minutes ago Up 14 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp goofy_aryabhata
313e17d021ac bitnami/php-fpm:latest "php-fpm -F --pid /o…" 2 hours ago Up 2 hours 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp friendly_jones
3d95d8d9d54d mysql:8.0.35 "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp amazing_goodall
(17)创建网页
php网页
[root@localhost conf]# cd /lamp/www
[root@localhost www]# ls
[root@localhost www]# sudo vim index.php
<?php
phpinfo();
html网页
[root@localhost www]# sudo vim index.html
<h1>hello linux apache mysql php</h1>
连接数据库的php
[root@localhost www]# sudo vim db.php
<?php
#输出php版本号
echo PHP_VERSION;
printf("<br>");
echo date('Y-m-d H:i:s');
printf("<br>");
/*循环格式化输出*/
for ($i = 1; $i <= 5; $i++) {
printf("<h3>%03d</h3>", $i);
}
//---pdo 连接数据库
$dsn = 'mysql:host=192.168.204.141;dbname=mysql';
$username = 'admin';
$password = 'admin';
try {
$db = new PDO($dsn, $username, $password);
// 设置错误模式为异常
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "成功连接到数据库";
} catch (PDOException $e) {
echo "连接数据库失败: " . $e->getMessage();
} catch (Exception $e) {
echo "系统未知异常:" . $e->getMessage();
}
$st = $db->query("show databases");
while ($row = $st->fetch(3)) {
echo $row[0] . '<br>';
}
//print_r($st->fetchAll(PDO::FETCH_NUM));
(18)测试
http://192.168.204.141
http://192.168.204.141/index.php
http://192.168.204.141/db.php
二、安装docker报错
(1)报错
[root@localhost ~]# sudo dnf install -y docker-ce docker-ce-cli containerd.io
Warning: failed loading '/etc/yum.repos.d/docker-ce.repo', skipping.
OS 12 kB/s | 2.4 kB 00:00
everything 13 kB/s | 2.5 kB 00:00
EPOL 13 kB/s | 2.5 kB 00:00
debuginfo 13 kB/s | 2.5 kB 00:00
source 14 kB/s | 2.4 kB 00:00
update 12 kB/s | 2.2 kB 00:00
update-source 12 kB/s | 2.2 kB 00:00
No match for argument: docker-ce
No match for argument: docker-ce-cli
No match for argument: containerd.io
Error: Unable to find a match: docker-ce docker-ce-cli containerd.io
(2)原因分析
yum源配置文件左侧未缩进。
(3)解决方法
修改yum源文件。
修改前:
修改后:
2.docker如何快速删除容器与镜像
(1)删除容器
docker rm -f $(docker ps -aq)
(2)删除镜像
docker rmi -f $(docker images -aq)
3.docker创建mysql容器失败
(1)报错
docker: Error response from daemon: driver failed programming external connectivity on endpoint friendly_shockley (29577e66bcdcb65b184d53162ec7dfcb8bd3ecc7003617f204acd01b5ce71051): Error starting userland proxy: listen tcp4 0.0.0.0:3306: bind: address already in use.
(2)原因分析
主机3306端口被占用。
[root@localhost conf]# netstat -antlp |grep 3306
(3)解决方法
关闭主机3306端口
[root@localhost conf]# sudo systemctl stop mysqld
[root@localhost conf]# netstat -antlp |grep 3306
成功:
[root@localhost conf]# docker run -d -p 3306:3306 --restart always -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=Root_321 -v /lamp/mysql:/var/lib/mysql mysql:8.0.35
4.docker创建apache容器失败
(1)报错
docker: Error response from daemon: driver failed programming external connectivity on endpoint admiring_villani (9f76049975b903328e794986b627848d4f3d2d73ebf52dbb1cacf49ee0127371): Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use.
(2) 原因分析
主机80端口被占用。
[root@localhost conf]# netstat -antlp |grep 80
(3)解决方法
关闭主机80端口
[root@localhost conf]# sudo systemctl stop mnginx
[root@localhost conf]# netstat -antlp |grep 80
成功:
[root@localhost conf]# docker run -d -p 80:80 --restart always -e TZ=Asia/Shanghai -v /lamp/www:/usr/local/apache2/htdocs -v /lamp/apache/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf httpd:latest
5.docker创建php-fpm容器失败
(1)报错
docker: Error response from daemon: driver failed programming external connectivity on endpoint infallible_mcclintock (ae7987f77b68a095c9eb4160e921abec6bc5e8b26f0e5a30ada73496e8a8c5e2): Error starting userland proxy: listen tcp4 0.0.0.0:9000: bind: address already in use.
(2) 原因分析
主机9000端口被占用。
[root@localhost conf]# netstat -antlp |grep 9000
(3)解决方法
关闭主机9000端口
[root@localhost conf]# sudo systemctl stop php-fpm
[root@localhost conf]# netstat -antlp |grep 9000
成功:
[root@localhost conf]# docker run -d -p 9000:9000 --restart always -e TZ=Asia/Shanghai -v /lamp/www:/var/www -v /lamp/php/php.ini:/opt/bitnami/php/etc/php.ini bitnami/php-fpm:latest
6. 80端口与php访问失败
(1)报错
(2)原因分析
httpd配置文件错误;
主机与docker容器的80端口未映射成功。
(3)解决方法
查看进程与端口,80端口未启动
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3673ab642039 httpd:latest "httpd-foreground" 4 seconds ago Restarting (1) Less than a second ago pedantic_dhawan
313e17d021ac bitnami/php-fpm:latest "php-fpm -F --pid /o…" 16 minutes ago Up 16 minutes 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp friendly_jones
3d95d8d9d54d mysql:8.0.35 "docker-entrypoint.s…" 33 minutes ago Up 33 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp amazing_goodall
[root@localhost conf]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 6717/docker-proxy
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4648/docker-proxy
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 1604/smbd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 940/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1273/sshd: /usr/sbi
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 1604/smbd
tcp 0 0 192.168.204.141:22 192.168.204.1:49993 ESTABLISHED 2986/sshd: root [pr
tcp 0 272 192.168.204.141:22 192.168.204.1:49988 ESTABLISHED 2977/sshd: root [pr
tcp6 0 0 :::9000 :::* LISTEN 6724/docker-proxy
tcp6 0 0 :::3306 :::* LISTEN 4655/docker-proxy
tcp6 0 0 :::139 :::* LISTEN 1604/smbd
tcp6 0 0 :::111 :::* LISTEN 940/rpcbind
tcp6 0 0 :::22 :::* LISTEN 1273/sshd: /usr/sbi
tcp6 0 0 :::445 :::* LISTEN 1604/smbd
删除容器
[root@localhost conf]# docker rm -f 36
新建容器再进入容器查看(容器内安装目录为apache2)
[root@localhost conf]# docker run -d -p 80:80 --restart always -e TZ=Asia/Shanghai -v /lamp/www:/usr/local/apache/htdocs -v /lamp/apache/conf/httpd.conf:/usr/local/apache/conf/httpd.conf httpd:latest
a8a1837942a4d6cbc4f51d965589a1123c9bce6d72f469f8d3103834c962417d
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8a1837942a4 httpd:latest "httpd-foreground" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp stupefied_lehmann
313e17d021ac bitnami/php-fpm:latest "php-fpm -F --pid /o…" 22 minutes ago Up 22 minutes 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp friendly_jones
3d95d8d9d54d mysql:8.0.35 "docker-entrypoint.s…" 40 minutes ago Up 40 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp amazing_goodall
[root@localhost www]# docker exec -it a8 /bin/bash
root@a8a1837942a4:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
root@a8a1837942a4:/usr/local/apache2# ll
bash: ll: command not found
root@a8a1837942a4:/usr/local/apache2# cd ..
root@a8a1837942a4:/usr/local# ls
apache apache2 bin etc games include lib man sbin share src
root@a8a1837942a4:/usr/local# cd apache
root@a8a1837942a4:/usr/local/apache# ls
conf htdocs
root@a8a1837942a4:/usr/local/apache# cd ..
root@a8a1837942a4:/usr/local# cd apache2
root@a8a1837942a4:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
root@a8a1837942a4:/usr/local/apache2# exit
exit
删除容器
[root@localhost conf]# docker rm -f a8
修改httpd配置文化
[root@localhost conf]# vim httpd.conf
修改后:apache修改为apache2
添加index.php
成功:
7.httpd容器进入不了
(1)报错
[root@localhost conf]# docker exec -it 44 /bin/bash
Error response from daemon: Container 44f25fb5f75a2efd17473356ef9dd0d0a427e1986f986d6b87cef54df59e7d60 is restarting, wait until the container is running
[root@localhost conf]# netstat -antpl |grep 80
[root@localhost conf]# lsof -i:80
[root@localhost conf]# docker logs --tail 50 --follow --timestamps httpd
Error response from daemon: No such container: httpd
[root@localhost conf]# docker logs --tail 50 --follow --timestamps 44f25fb5f75a
2024-02-27T16:50:45.522110444Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:50:46.019256165Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:50:46.611193271Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:50:47.399696297Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:50:48.602454008Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:50:50.598589705Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:50:54.215519088Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:51:00.969961812Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:51:14.167343075Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:51:40.167081942Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:52:31.750892286Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:53:32.206977683Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:54:32.618810032Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:55:33.047916170Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:56:33.451544959Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:57:33.846002884Z AH00534: httpd: Configuration error: No MPM loaded.
2024-02-27T16:58:34.242933943Z AH00534: httpd: Configuration error: No MPM loaded.
(2)原因分析
在没有明确声明 MPM 模块的情况下覆盖文件 /usr/local/apache2/conf/httpd.conf
,所以在此提交之后,容器开始失败。
(3)解决方法
配置文件添加
LoadModule mpm_event_module modules/mod_mpm_event.so
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
#LoadModule mpm_worker_module modules/mod_mpm_worker.so
成功:
[root@localhost conf]# docker exec -it 21 /bin/bash
root@21d4521e77b0:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules