docker编排LAMP

锺离明煦
2023-12-01

docker编排

  1. 首先创建基础的文件:
    [root@localhost lamp]# tree
    ├── apache
    │   ├── authorized_keys
    │   ├── Dockerfile
    │   └── run.sh
    ├── conf
    │   └── my.cnf
    ├── docker-compose.yml
    ├── log
    

│   ├── apache
│   └── mysql
├── mysql
└── www
├── index.html
└── test.php


2. ```
因为使用密钥登陆,所以将服务器的授权密钥放到apache目录中,到时候会用到:
[root@localhost lamp]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:xRgdpwF2Cd/TkJR1PXtMciZi9T1B0q75LTMBrrEBaJk root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|        =++++*=oo|
|       . *o==o+=B|
|       +. =.o.oO=|
|      E ..  .. o+|
|     .  S. . .o .|
|          o .o.  |
|           =  ...|
|          o   +..|
|               + |
+----[SHA256]-----+
[root@localhost lamp]# cat /root/.ssh/id_rsa.pub > /LAMP/docker/lamp/apache/authorized_keys
[root@localhost lamp]# 
[root@localhost lamp]# cat /docker/lamp/apache/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLgo3vqwx2PkXBRy7kvEeqkn/bsglbLECMyH+8etNjf4RrSIlw+1RNLh5clN3lxmhnf9Fi2uuSKgJXE/SWSFspAlxHARr2QpmEjn/V15tpW8cLJ7GvLXriAogsOl79AXTH6nyxfdJTijUW8ns3LnM70RFfIM0AkgTLsaAQUCd8yLR7z70uExWjM08HDZlk8ZdvO8AGZ31p0bJAMdyCUhw6cjfYso7eso05e7Z3oYwkq5i6D/+TFNzFIDghYx3H7++OgG3F0kSFTG56nI+UBwx2MtFroyhTBvN3Esagtflh2GPQwoqpRI38T0ydylFXgAjUXno9g422WpgQrTOjNUoL root@localhost.localdomain
  1. 创建dockerfile文件,添加如下内容:
    
    FROM centos:7.7.1908
    MAINTAINER from zhao@201106
    RUN rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm --force
    RUN rpm -ivh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm  --force
    RUN yum install epel-release -y
    RUN yum install httpd httpd-devel -y
    RUN yum install openssh-server -y
    RUN yum install sudo -y
    RUN yum install net-tools -y
    RUN yum install wget -y
    RUN yum install -y php70w php70w-mysql php70w-mbstring php70w-mcrypt php70w-gd  php70w-imap php70w-ldap php70w-odbc php70w-pear php70w-xml   php70w-xmlrpc php70w-pdo
    RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
    RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
    RUN ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key
    RUN ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
    RUN mkdir -p /var/run/sshd
    RUN mkdir -p /root/.ssh
    ADD authorized_keys /root/.ssh/authorized_keys
    ADD run.sh /run.sh
    RUN chmod 777 /run.sh
    EXPOSE 22 80
    CMD ["/run.sh"]
    

~


4. ```
编辑run.sh(为dockerfile文件提供):
[root@localhost apache]# vim run.sh 

#!/bin/bash
/usr/sbin/httpd -D DFOREGROUND
/usr/sbin/sshd -D
/bin/bash
  1. 配置conf目录下的mysql的配置文件:
    [root@localhost conf]# vim my.cnf
    
    [mysqld]
    user=mysql
    default-storage-engine=INNODB
    character-set-server=utf8
    init_connect='SET NAMES utf8'
    
    [client]
    default-character-set=utf8
    
    [mysql]
    default-character-set=utf8
    
  2. LAMP环境测试文件:
    [root@localhost www]# vim index.php 
    <?php
        #这个是测试数据库连接情况的,判断类型语句
        #使用自定义docker driver网络,可以通过容器名称通信
        $link = mysqli_connect("mymysql","root","123456.com");
        if(!$link)
            die('Could not connect: ' . mysql_error());
        else
            echo "mysql database is ok";
        mysql_close($link);
    ?>
    ————————————————————————————————————————————————————————————————————————————
    [root@localhost www]# vim test.php
    ##这算是一个php的测试文件,测试php有没有运行起来。
    <?php
      phpinfo();
    ?>
    
  3. 安装docker-cmpose:
    ##由于下载太慢,别人传过来的。
    [root@localhost bin]# chmod +x /usr/local/bin/docker-compose 
    [root@localhost bin]# docker-compose  version
    docker-compose version 1.25.0, build 0a186604
    docker-py version: 4.1.0
    CPython version: 3.7.4
    OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019
    
  4. 编辑docker-compose.yml文件:
    version: '3'
    services:
      apache:
        build: ./apache
        container_name: apache
        ports:
          - "8080:80"
          - "9000:22"
        volumes:
          - ./www:/var/www/html
          - ./log/apache:/var/log/apache2
        networks:
          - lamp-network
    
      mysql:
        image: mysql:5.7
        container_name: mysql
        ports:
          - "3306:3306"
        volumes:
          - ./conf/my.cnf:/etc/my.cnf
          - ./mysql:/var/lib/mysql
          - ./log/mysql:/var/log/mysql
        environment:
          - MYSQL_ROOT_PASSWORD=123456.com
        networks:
          - lamp-network
    networks:
      lamp-network:
        driver: bridge
    
  5. 运行命令up起来即可
    防火墙放行端口或者关闭后访问页面即可
    
  6. [root@localhost lamp]# docker-compose ps
     Name              Command             State                   Ports                
    ------------------------------------------------------------------------------------
    apache   /run.sh                       Up      0.0.0.0:9000->22/tcp,                
                                                   0.0.0.0:8000->80/tcp                 
    mysql    docker-entrypoint.sh mysqld   Up      0.0.0.0:3306->3306/tcp, 33060/tcp    
    
  7. 尝试远程连接成功:
    [root@localhost lamp]# ssh 192.168.136.130 -p 9000
    The authenticity of host '[192.168.136.130]:9000 ([192.168.136.130]:9000)' can't be established.
    ECDSA key fingerprint is SHA256:xVEspDOE4bCM4ZBvlGQ+PO8j0RH/jTfgxQWRYFITF6k.
    ECDSA key fingerprint is MD5:0c:eb:df:62:c2:c6:c3:9d:e2:31:a2:6b:71:e4:bb:1b.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '[192.168.136.130]:9000' (ECDSA) to the list of known hosts.
    [root@d26f8b5ae130 ~]# 
    
    

    番外篇:此次没有给httpd做数据卷,因为他依赖php文件

    如果想做的话,先不做挂载,然后进入容器的httpd配置文件,找到php的文件,放到宿主机上,这样,就可以实现php和http在宿主机上,并且可以做成同步数据卷了。
    

 类似资料: