docker 安装lnmp 环境
docker login 登录
REPOSITORY TAG IMAGE ID CREATED SIZE [root@iZbp12ha1tmdxjd9tzmcjbZ ~]# clear [root@iZbp12ha1tmdxjd9tzmcjbZ ~]# docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: zzs10086 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 [root@iZbp12ha1tmdxjd9tzmcjbZ ~]#
docker 拉取mysql镜像
[root@iZbp12ha1tmdxjd9tzmcjbZ ~]# docker pull mysql:5.6 5.6: Pulling from library/mysql 80369df48736: Pull complete e8f52315cb10: Pull complete cf2189b391fc: Pull complete cc98f645c682: Pull complete 27a27ac83f74: Pull complete f3f8d5bb9498: Pull complete bbfacbe5a185: Pull complete 9db7adff1e15: Pull complete f8402500c236: Pull complete b0319efc9cd8: Pull complete d19ab308a635: Pull complete Digest: sha256:411cb46904b646d1260e075505ee75d7457511412011eb03928e956eac1c0bf9 Status: Downloaded newer image for mysql:5.6 docker.io/library/mysql:5.6 [root@iZbp12ha1tmdxjd9tzmcjbZ ~]#
启动mysql 容器
参数说明 -d 让容器在后台运行 -p 添加主机到容器的端口映射 -e 设置环境变量,这里是设置mysql的root用户的初始密码,这个必须设置 –name 容器的名字,随便取,但是必须唯一
[root@iZbp12ha1tmdxjd9tzmcjbZ ~]# docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=zzs123456 --name mysql mysql:5.6 860b2fbabd5849f297488b5036bd1966bf023c24c24b307b488b457078c7830f [root@iZbp12ha1tmdxjd9tzmcjbZ ~]#
接下来我们就可以通过命令docker ps -a 查看我们刚刚创建的容器
[root@iZbp12ha1tmdxjd9tzmcjbZ ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8cfa08abc730 mysql:5.6 "docker-entrypoint.s…" 6 seconds ago Up 4 seconds 0.0.0.0:3307->3306/tcp mysql
这里我们可以看到我的容器状态的Up状态,表示容器正在运行,并且把可以看到主机和容器的端口映射关系。
接下来,我们就可以进入到我们刚刚创建的容器中,输入命令
docker exec -it xy_mysql /bin/bash
参数说明
-t 在容器里生产一个伪终端
-i 对容器内的标准输入 (STDIN) 进行交互
容器中默认是没有vim的,所以我们首先要安装vim,需要注意的是安装前记得先执行apt update命令,不然安装会出现问题。
进入到mysql容器后,我们通过创建一个远程可以访问的用户,这样我们就能从别的主机访问到我们的数据库了。
[root@iZbp12ha1tmdxjd9tzmcjbZ /]# docker exec -it zzs_mysql /bin/bash root@98ad485b0f73:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.28 MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.11 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'zzs'@'%' IDENTIFIED BY 'zzs123456' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.12 sec)
外面通过端口3307 ,用户名zzs 密码zzs123456就可以访问容器里的mysql数据库了
二、docker安装php-fpm
同样首先我们拉取php-fpm的镜像
[root@iZbp12ha1tmdxjd9tzmcjbZ ~]# docker pull php:7.2-fpm 7.2-fpm: Pulling from library/php 000eee12ec04: Pull complete 8ae4f9fcfeea: Pull complete 60f22fbbd07a: Pull complete ccc7a63ad75f: Pull complete acc239e0adcb: Pull complete 6b41b73291d8: Pull complete a613b7ec35f9: Pull complete 006215261eb0: Pull complete 7c1f0f4a32d8: Pull complete 4dbbc05b41ce: Pull complete b3f5c36331f7: Pull complete Digest: sha256:f2508e97216aed30aaabc80012cc2799904b141e01d7893dbf21bc2c055451f4 Status: Downloaded newer image for php:7.2-fpm docker.io/library/php:7.2-fpm [root@iZbp12ha1tmdxjd9tzmcjbZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE php 7.2-fpm 9c8426048349 2 days ago 398MB mysql 5.7 cd3ed0dfff7e 5 weeks ago 437MB [root@iZbp12ha1tmdxjd9tzmcjbZ ~]#
再创建一个phpfpm容器
[root@iZbp12ha1tmdxjd9tzmcjbZ ~]# docker run -d -v /code:/var/www/html -p 9000:9000 --link zzs_mysql:mysql --name zzs_phpfpm php:7.2-fpm 5e80638107ac2cf2ae7fab92db9775c93526a6302f76b3beb6c2bafcace465c2 [root@iZbp12ha1tmdxjd9tzmcjbZ ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5e80638107ac php:7.2-fpm "docker-php-entrypoi…" 3 seconds ago Up 3 seconds 0.0.0.0:9000->9000/tcp zzs_phpfpm 98ad485b0f73 mysql:5.7 "docker-entrypoint.s…" 47 minutes ago Up 10 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp zzs_mysql [root@iZbp12ha1tmdxjd9tzmcjbZ ~]# ls nginx [root@iZbp12ha1tmdxjd9tzmcjbZ ~]# docker exec -it zzs_phpfpm /bin/bash root@5e80638107ac:/var/www/html# ls
参数说明
-d 让容器在后台运行
-p 添加主机到容器的端口映射
-v 添加目录映射,即主机上的/code和容器中/var/www/html目录是同步的
–name 容器的名字
–link 与另外一个容器建立起联系,这样我们就可以在当前容器中去使用另一个容器里的服务。
这里如果不指定–link参数其实也是可以得,因为容易本身也是有ip的且唯一,所以我们也可以直接利用ip去访问容器。
然后进入到我们的容器,然后我们在/var/www/html目录下新建一个index.php文件
root@5e80638107ac:/var/www/html# touch index.php root@5e80638107ac:/var/www/html# ls index.php
我们可以看到该目录下新建了一个php文件
接下来我们回到我们的主机上面,访问一下我们主机上/code
[root@iZbp12ha1tmdxjd9tzmcjbZ code]# pwd /code [root@iZbp12ha1tmdxjd9tzmcjbZ code]# ls index.php [root@iZbp12ha1tmdxjd9tzmcjbZ code]#
我们发现我们在容器里的/var/www/html目录中新建的文件也在主机的/code目录中,因为在创建容器的时候,我们已经把主机中的目录挂载到了容器中去了。
因为后面我要使用pdo模块进行测试,所以我需要自己安装pdo_mysql模块,在docker容器中可以这样来安装
root@5e80638107ac:/var/www/html# docker-php-ext-install pdo_mysql Configuring for: PHP Api Version: 20170718 Zend Module Api No: 20170718 Zend Extension Api No: 320170718 checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking for a sed that does not truncate output... /bin/sed checking for cc... cc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether cc accepts -g... yes
然后我们可以通过命令php -m查看我们的php的所有扩展模块,我们可以去看到我们刚刚安装的pdo_mysql扩展也在里面
root@5e80638107ac:/var/www/html# php -m [PHP Modules] Core ctype curl date dom fileinfo filter ftp hash iconv json libxml mbstring mysqlnd openssl pcre PDO pdo_mysql pdo_sqlite Phar posix readline Reflection session SimpleXML sodium SPL sqlite3 standard tokenizer xml xmlreader xmlwriter zlib [Zend Modules]
三、docker安装nginx
首先,我们从仓库里去拉取一个nginx镜像
[root@iZbp12ha1tmdxjd9tzmcjbZ code]# docker pull nginx:1.17 1.17: Pulling from library/nginx 000eee12ec04: Already exists eb22865337de: Pull complete bee5d581ef8b: Pull complete Digest: sha256:50cf965a6e08ec5784009d0fccb380fc479826b6e0e65684d9879170a9df8566 Status: Downloaded newer image for nginx:1.17 docker.io/library/nginx:1.17 [root@iZbp12ha1tmdxjd9tzmcjbZ code]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.17 231d40e811cd 2 days ago 126MB php 7.2-fpm 9c8426048349 2 days ago 398MB mysql 5.7 cd3ed0dfff7e 5 weeks ago 437MB [root@iZbp12ha1tmdxjd9tzmcjbZ code]#
接下来运行nginx容器
[root@iZbp12ha1tmdxjd9tzmcjbZ code]# docker run -d -p 80:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx/ -v /data/nginx/conf/conf.d/:/etc/nginx/conf.d/ --link zzs_phpfpm:phpfpm --name zzs_nginx nginx:1.17 [root@iZbp12ha1tmdxjd9tzmcjbZ code]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b220a5d08864 nginx:1.17 "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp zzs_nginx 5e80638107ac php:7.2-fpm "docker-php-entrypoi…" 15 minutes ago Up 15 minutes 0.0.0.0:9000->9000/tcp zzs_phpfpm 98ad485b0f73 mysql:5.7 "docker-entrypoint.s…" About an hour ago Up 26 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp zzs_mysql [root@iZbp12ha1tmdxjd9tzmcjbZ code]#
参数说明:
-d 让容器在后台运行
-p 添加主机到容器的端口映射
-v 添加目录映射,这里最好nginx容器的根目录最好写成和php容器中根目录一样。但是不一点非要一模一样,如果不一样在配置nginx的时候需要注意
–name 容器的名字
–link 与另外一个容器建立起联系
然后进入nginx容器,修改nginx的配置文件让它支持php