Docker docker-compose 配置lnmp开发环境

1、安装docker

yum -y install docker
systemctl start docker
systemctl enable docker

安装docker-compose

https://docs.docker.com/compose/install/

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 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.12 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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> 
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 bash
root@32de01dbee49:/# cd /var/www/html/&&ls
index.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.conf
phpfpm:
  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

 

 

posted @ 2018-08-14 10:38  caocong  阅读(9468)  评论(0编辑  收藏  举报