博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Docker docker-compose 配置lnmp开发环境
阅读量:6819 次
发布时间:2019-06-26

本文共 5482 字,大约阅读时间需要 18 分钟。

1、安装docker

yum -y install docker systemctl start docker systemctl enable docker

安装docker-compose

git clone https://github.com/docker/compose.git chmod u+x compose ./compose or pip install docker-compose docker-compose -version

 

2、安装mysql

拉取镜像

docker pull mysql

运行MySQL

docker run -p 3306:3306 --name mysql_test -v $PWD/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=passwd -d --privileged=true mysql

命令说明

-p 3306:3306:将容器的3306端口映射到主机的3306端口-v PWD/mysql/data:/var/lib/mysql:将主机当前目录下的mysql/data文件夹挂载到容器的/var/lib/mysql 下,在mysql容器中产生的数据就会保存在本机mysql/data目录下-e MYSQL_ROOT_PASSWORD=passwd:初始化root用户的密码-d 后台运行容器--name 给容器指定别名--privileged=true centos7 可能会碰到权限问题,需要加参数

进入容器

docker exec -it mysql_test /bin/bash

进入数据库查看

root@39e0abed7609:/# mysql -u root -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 8Server version: 8.0.12 MySQL Community Server - GPLCopyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || sys                |+--------------------+4 rows in set (0.02 sec)mysql>

 

3、安装PHP

vim Dockerfile FROM  php:5.6-fpm RUN apt-get update && apt-get install -y \ libfreetype6-dev \ libjpeg62-turbo-dev \ libpng12*-dev \ vim \ && docker-php-ext-install pdo_mysql \ && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ && docker-php-ext-install gd \
构造镜像 docker build -t="php-fpm5.6/v2" .

使用该镜像启动容器

docker run -d -p 9000:9000 -v /var/www/html/:/var/www/html/ --name php-with-mysql --link mysql_test:mysql  --volumes-from mysql_test --privileged=true php-fpm5.6/v2
参数解析 -v 将本地磁盘上的php代码挂载到docker 环境中,对应docker的目录是 /var/www/html/ --name 新建的容器名称 php-with-mysql--link 链接的容器,链接的容器名称:在该容器中的别名,运行这个容器是,docker中会自动添加一个host识别被链接的容器ip--privileged=true 权限问题

 

进入容器

docker exec -it php-with-mysql /bin/bash cd /var/www/html/ && ls

 

4、安装nginx 镜像

vim default.conf

server {        listen       80;        server_name  localhost;            location / {            root   /var/www/html;            index  index.html index.htm index.php; # 增加index.php        }            #error_page  404              /404.html;            # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   /var/www/html;        }        location ~ \.php$ {            root           /var/www/html; # 代码目录            fastcgi_pass   phpfpm:9000;    # 修改为phpfpm容器            fastcgi_index  index.php;            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; # 修改为$document_root            include        fastcgi_params;        }    }

运行容器

docker run -d --link php-with-mysql:phpfpm --volumes-from php-with-mysql -p 80:80 -v /var/www/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf --name nginx-php --privileged=true  nginx

参数解析

--link php-with-mysql:phpfpm 将php容器链接到nginx容器里来,phpfpm是nginx容器里的别名。--volumes-from php-with-mysql 将php-with-mysql 容器挂载的文件也同样挂载到nginx容器中-v /var/www/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf  将nginx 的配置文件替换,挂载本地编写的配置文件docker exec -it nginx-php bashroot@32de01dbee49:/# cd /var/www/html/&&lsindex.php  mysql.php  testmysql.php  webview

 

 

 

 

docker-compose

[root@cc home]# tree compose-lnmp/compose-lnmp/|-- docker-compose.yml|-- html |   |-- index.html |-- mysql |   `-- Dockerfile|-- nginx|   |-- conf|   |   `-- default.conf|   `-- Dockerfile`-- phpfpm    `-- Dockerfile
[root@cc compose-lnmp]# cat docker-compose.yml nginx:  build: ./nginx  ports:    - "80:80"  links:    - "phpfpm"  volumes:    - /home/compose-lnmp/html/:/var/www/html/    - /home/compose-lnmp/nginx/conf/default.conf:/etc/nginx/conf.d/default.confphpfpm:  build: ./phpfpm  ports:    - "9000:9000"  volumes:    - ./html/:/var/www/html/  links:    - "mysql"mysql:  build: ./mysql  ports:    - "3306:3306"  volumes:    - /home/compose-lnmp/mysql/data/:/var/lib/mysql/  environment:    MYSQL_ROOT_PASSWORD : 123456

[root@cc compose-lnmp]# cat mysql/Dockerfile

FROM mysql:5.6

 

[root@cc compose-lnmp]# cat nginx/Dockerfile

FROM nginx:latest
RUN apt-get update && apt-get install -y vim

[root@cc compose-lnmp]# cat nginx/conf/default.conf

server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html index.htm index.php; # 增加index.php
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location ~ \.php$ {
root /var/www/html; # 代码目录
fastcgi_pass phpfpm:9000; # 修改为phpfpm容器
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 修改为$document_root
include fastcgi_params;
}
}

 

 

[root@cc compose-lnmp]# cat phpfpm/Dockerfile

FROM php:5.6-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng12*-dev \
vim \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install gd \

 

 

执行docker-compose

[root@cc compose-lnmp]# docker-compose up -d

 

 

转载于:https://www.cnblogs.com/blogscc/p/9473123.html

你可能感兴趣的文章
ZooKeeper应用案例
查看>>
springboot(二):thymeleaf模板开发
查看>>
Android Bluetooth HID实现详解
查看>>
高通camera架构
查看>>
PHP运行模式
查看>>
leetcode Remove Duplicates from Sorted Array
查看>>
spring cloud 与dubbo
查看>>
Linux零拷贝函数SendFile应用
查看>>
linux下创建文件或文件夹快捷方式一个简单地方法
查看>>
SDPhotoBrowser图片浏览器
查看>>
php 使用DOMDocument 解析xml
查看>>
如何7步实现根据源码包创建rpm包
查看>>
hadoop2.0集群搭建详解
查看>>
java调用oracle存储过程
查看>>
Spring Cloud Alibaba基础教程:Nacos配置的多环境管理
查看>>
极乐小程序榜单(第六期)
查看>>
使用Log4j为项目配置日志输出应用详细总结及示例演示.
查看>>
Lua-5.3.2 安装 luasocket 的正确姿势
查看>>
freeswitch实战经验1:服务器向成员主动发起会议邀请
查看>>
python转换文本编码和windows换行符
查看>>