当前位置:   article > 正文

Docker容器与虚拟化技术:OpenEuler 部署 docker容器应用

Docker容器与虚拟化技术:OpenEuler 部署 docker容器应用

目录

一、实验

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备注
LinuxopenEuler22.03 LTS SP2

192.168.204.144(动态)

192.168.204.141(静态)

192.168.204.142(静态)

docker25.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

  1. [docker-ce-stable]
  2. name=Docker CE Stable - $basearch
  3. baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/$basearch/stable
  4. enabled=1
  5. gpgcheck=1
  6. gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
  7. [docker-ce-stable-debuginfo]
  8. name=Docker CE Stable - Debuginfo $basearch
  9. baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/debug-$basearch/stable
  10. enabled=0
  11. gpgcheck=1
  12. gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
  13. [docker-ce-stable-source]
  14. name=Docker CE Stable - Sources
  15. baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/source/stable
  16. enabled=0
  17. gpgcheck=1
  18. gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
  19. [docker-ce-test]
  20. name=Docker CE Test - $basearch
  21. baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/$basearch/test
  22. enabled=0
  23. gpgcheck=1
  24. gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
  25. [docker-ce-test-debuginfo]
  26. name=Docker CE Test - Debuginfo $basearch
  27. baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/debug-$basearch/test
  28. enabled=0
  29. gpgcheck=1
  30. gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
  31. [docker-ce-test-source]
  32. name=Docker CE Test - Sources
  33. baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/source/test
  34. enabled=0
  35. gpgcheck=1
  36. gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
  37. [docker-ce-nightly]
  38. name=Docker CE Nightly - $basearch
  39. baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/$basearch/nightly
  40. enabled=0
  41. gpgcheck=1
  42. gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
  43. [docker-ce-nightly-debuginfo]
  44. name=Docker CE Nightly - Debuginfo $basearch
  45. baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/debug-$basearch/nightly
  46. enabled=0
  47. gpgcheck=1
  48. gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
  49. [docker-ce-nightly-source]
  50. name=Docker CE Nightly - Sources
  51. baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/source/nightly
  52. enabled=0
  53. gpgcheck=1
  54. 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)查看版本信息

  1. [root@localhost ~]# sudo docker version
  2. [root@localhost ~]# sudo docker --version

2.镜像加速

(1)配置国内的镜像源

[root@localhost ~]#  sudo vim /etc/docker/daemon.json

  1. {
  2. "registry-mirrors": ["http://hub-mirror.c.163.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn"]
  3. }

(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)复制

  1. [root@localhost ~]# cp /usr/local/php/lib/php.ini /lamp/php/
  2. [root@localhost ~]# cd /lamp/php
  3. [root@localhost php]# ls

(7)修改时区

[root@localhost php]# vim php.ini +$


(8)查找

[root@localhost conf]# sudo find / -name 'httpd.conf'

(9)复制

  1. [root@localhost conf]# cp /usr/local/apache/conf/httpd.conf /lamp/apache/conf/
  2. [root@localhost conf]# cd /lamp/apache/conf/
  3. [root@localhost conf]# ls

(9)修改httpd配置文件

[root@localhost conf]# vim httpd.conf

将httpd.conf里的下面三处注释去除(下面IP必须填写PHP容器真实的IP,fpm端口号默认9000,不用变化)

  1. LoadModule proxy_module modules/mod_proxy.so
  2. LoadModule proxy_http_module modules/mod_proxy_http.so
  3. LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

末尾添加虚拟主机

  1. <VirtualHost *:80>
  2. ServerAdmin 7jjw@163.com
  3. DocumentRoot "/usr/local/apache2/htdocs"
  4. ServerName localhost
  5. <Directory "/usr/local/apache2/htdocs">
  6. Options None
  7. Require all granted
  8. </Directory>
  9. ProxyRequests Off
  10. ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.204.141:9000/var/www/$1
  11. </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开头

  1. [root@localhost conf]# docker ps
  2. CONTAINER ID   IMAGE          COMMAND                   CREATED              STATUS              PORTS                                                  NAMES
  3. 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

  1. [root@localhost conf]# docker exec -it 3d mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 8
  5. Server version: 8.0.35 MySQL Community Server - GPL
  6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> create user admin identified by 'admin';
  12. Query OK, 0 rows affected (0.05 sec)
  13. mysql> grant all on *.* to admin;
  14. Query OK, 0 rows affected (0.00 sec)
  15. mysql> exit
  16. Bye
  17. [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开头

  1. [root@localhost conf]# docker ps |grep httpd
  2. 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开头

  1. [root@localhost conf]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 21d4521e77b0 httpd:latest "httpd-foreground" 14 minutes ago Up 14 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp goofy_aryabhata
  4. 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
  5. 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网页

  1. [root@localhost conf]# cd /lamp/www
  2. [root@localhost www]# ls
  3. [root@localhost www]# sudo vim index.php

  1. <?php
  2. phpinfo();

html网页

[root@localhost www]# sudo vim index.html

<h1>hello linux apache mysql php</h1>

连接数据库的php

[root@localhost www]# sudo vim db.php

  1. <?php
  2. #输出php版本号
  3. echo PHP_VERSION;
  4. printf("<br>");
  5. echo date('Y-m-d H:i:s');
  6. printf("<br>");
  7. /*循环格式化输出*/
  8. for ($i = 1; $i <= 5; $i++) {
  9. printf("<h3>%03d</h3>", $i);
  10. }
  11. //---pdo 连接数据库
  12. $dsn = 'mysql:host=192.168.204.141;dbname=mysql';
  13. $username = 'admin';
  14. $password = 'admin';
  15. try {
  16. $db = new PDO($dsn, $username, $password);
  17. // 设置错误模式为异常
  18. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  19. echo "成功连接到数据库";
  20. } catch (PDOException $e) {
  21. echo "连接数据库失败: " . $e->getMessage();
  22. } catch (Exception $e) {
  23. echo "系统未知异常:" . $e->getMessage();
  24. }
  25. $st = $db->query("show databases");
  26. while ($row = $st->fetch(3)) {
  27. echo $row[0] . '<br>';
  28. }
  29. //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)报错

  1. [root@localhost ~]# sudo dnf install -y docker-ce docker-ce-cli containerd.io
  2. Warning: failed loading '/etc/yum.repos.d/docker-ce.repo', skipping.
  3. OS 12 kB/s | 2.4 kB 00:00
  4. everything 13 kB/s | 2.5 kB 00:00
  5. EPOL 13 kB/s | 2.5 kB 00:00
  6. debuginfo 13 kB/s | 2.5 kB 00:00
  7. source 14 kB/s | 2.4 kB 00:00
  8. update 12 kB/s | 2.2 kB 00:00
  9. update-source 12 kB/s | 2.2 kB 00:00
  10. No match for argument: docker-ce
  11. No match for argument: docker-ce-cli
  12. No match for argument: containerd.io
  13. 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端口

  1. [root@localhost conf]# sudo systemctl stop mysqld
  2. [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端口

  1. [root@localhost conf]# sudo systemctl stop mnginx
  2. [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端口

  1. [root@localhost conf]# sudo systemctl stop php-fpm
  2. [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端口未启动

  1. [root@localhost conf]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 3673ab642039 httpd:latest "httpd-foreground" 4 seconds ago Restarting (1) Less than a second ago pedantic_dhawan
  4. 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
  5. 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
  6. [root@localhost conf]# netstat -antlp
  7. Active Internet connections (servers and established)
  8. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  9. tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 6717/docker-proxy
  10. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4648/docker-proxy
  11. tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 1604/smbd
  12. tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 940/rpcbind
  13. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1273/sshd: /usr/sbi
  14. tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 1604/smbd
  15. tcp 0 0 192.168.204.141:22 192.168.204.1:49993 ESTABLISHED 2986/sshd: root [pr
  16. tcp 0 272 192.168.204.141:22 192.168.204.1:49988 ESTABLISHED 2977/sshd: root [pr
  17. tcp6 0 0 :::9000 :::* LISTEN 6724/docker-proxy
  18. tcp6 0 0 :::3306 :::* LISTEN 4655/docker-proxy
  19. tcp6 0 0 :::139 :::* LISTEN 1604/smbd
  20. tcp6 0 0 :::111 :::* LISTEN 940/rpcbind
  21. tcp6 0 0 :::22 :::* LISTEN 1273/sshd: /usr/sbi
  22. tcp6 0 0 :::445 :::* LISTEN 1604/smbd

 删除容器

[root@localhost conf]# docker rm -f 36

新建容器再进入容器查看(容器内安装目录为apache2)

  1. [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
  2. a8a1837942a4d6cbc4f51d965589a1123c9bce6d72f469f8d3103834c962417d
  3. [root@localhost conf]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. a8a1837942a4 httpd:latest "httpd-foreground" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp stupefied_lehmann
  6. 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
  7. 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

  1. [root@localhost www]# docker exec -it a8 /bin/bash
  2. root@a8a1837942a4:/usr/local/apache2# ls
  3. bin build cgi-bin conf error htdocs icons include logs modules
  4. root@a8a1837942a4:/usr/local/apache2# ll
  5. bash: ll: command not found
  6. root@a8a1837942a4:/usr/local/apache2# cd ..
  7. root@a8a1837942a4:/usr/local# ls
  8. apache apache2 bin etc games include lib man sbin share src
  9. root@a8a1837942a4:/usr/local# cd apache
  10. root@a8a1837942a4:/usr/local/apache# ls
  11. conf htdocs
  12. root@a8a1837942a4:/usr/local/apache# cd ..
  13. root@a8a1837942a4:/usr/local# cd apache2
  14. root@a8a1837942a4:/usr/local/apache2# ls
  15. bin build cgi-bin conf error htdocs icons include logs modules
  16. root@a8a1837942a4:/usr/local/apache2# exit
  17. exit

删除容器

[root@localhost conf]# docker rm -f a8


修改httpd配置文化

[root@localhost conf]# vim httpd.conf


修改后:apache修改为apache2

添加index.php

成功:

7.httpd容器进入不了

(1)报错

  1. [root@localhost conf]# docker exec -it 44 /bin/bash
  2. Error response from daemon: Container 44f25fb5f75a2efd17473356ef9dd0d0a427e1986f986d6b87cef54df59e7d60 is restarting, wait until the container is running
  3. [root@localhost conf]# netstat -antpl |grep 80
  4. [root@localhost conf]# lsof -i:80
  5. [root@localhost conf]# docker logs --tail 50 --follow --timestamps httpd
  6. Error response from daemon: No such container: httpd
  7. [root@localhost conf]# docker logs --tail 50 --follow --timestamps 44f25fb5f75a
  8. 2024-02-27T16:50:45.522110444Z AH00534: httpd: Configuration error: No MPM loaded.
  9. 2024-02-27T16:50:46.019256165Z AH00534: httpd: Configuration error: No MPM loaded.
  10. 2024-02-27T16:50:46.611193271Z AH00534: httpd: Configuration error: No MPM loaded.
  11. 2024-02-27T16:50:47.399696297Z AH00534: httpd: Configuration error: No MPM loaded.
  12. 2024-02-27T16:50:48.602454008Z AH00534: httpd: Configuration error: No MPM loaded.
  13. 2024-02-27T16:50:50.598589705Z AH00534: httpd: Configuration error: No MPM loaded.
  14. 2024-02-27T16:50:54.215519088Z AH00534: httpd: Configuration error: No MPM loaded.
  15. 2024-02-27T16:51:00.969961812Z AH00534: httpd: Configuration error: No MPM loaded.
  16. 2024-02-27T16:51:14.167343075Z AH00534: httpd: Configuration error: No MPM loaded.
  17. 2024-02-27T16:51:40.167081942Z AH00534: httpd: Configuration error: No MPM loaded.
  18. 2024-02-27T16:52:31.750892286Z AH00534: httpd: Configuration error: No MPM loaded.
  19. 2024-02-27T16:53:32.206977683Z AH00534: httpd: Configuration error: No MPM loaded.
  20. 2024-02-27T16:54:32.618810032Z AH00534: httpd: Configuration error: No MPM loaded.
  21. 2024-02-27T16:55:33.047916170Z AH00534: httpd: Configuration error: No MPM loaded.
  22. 2024-02-27T16:56:33.451544959Z AH00534: httpd: Configuration error: No MPM loaded.
  23. 2024-02-27T16:57:33.846002884Z AH00534: httpd: Configuration error: No MPM loaded.
  24. 2024-02-27T16:58:34.242933943Z AH00534: httpd: Configuration error: No MPM loaded.

(2)原因分析

在没有明确声明 MPM 模块的情况下覆盖文件 /usr/local/apache2/conf/httpd.conf,所以在此提交之后,容器开始失败。

(3)解决方法

配置文件添加

  1. LoadModule mpm_event_module modules/mod_mpm_event.so
  2. #LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
  3. #LoadModule mpm_worker_module modules/mod_mpm_worker.so

成功:

  1. [root@localhost conf]# docker exec -it 21 /bin/bash
  2. root@21d4521e77b0:/usr/local/apache2# ls
  3. bin build cgi-bin conf error htdocs icons include logs modules

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/179243
推荐阅读
相关标签
  

闽ICP备14008679号