Installation docker-compose arranged lnmp

1, the installation docker-compose

Make sure you have installed the docker
sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

docker-compose --version
 

 

2, install mysql

Mirror Pull

docker pull mysql

Run 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

Command Description

-p 3306: 3306: 3306 of the container port mapping to the host port 3306 
-v PWD / mysql / data: / var / lib / mysql: mysql the current directory in the host / data folder mounted to the container / var under / lib / mysql, mysql data generated in the container will be stored in native mysql / data directory 
-e MYSQL_ROOT_PASSWORD = passwd: initializing the root password 
-d background container 
--name alias to a container 
--privileged = true centos7 may run into permission issues, we need to add parameters

Into the container

docker exec -it mysql_test /bin/bash

Check into the database

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, install 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 \
Mirror configuration 
docker build -t = "php-fpm5.6 / v2".

The image using starting container

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
Parameter parsing 
-v php code on the disk mount docker local environment, the corresponding directory is docker / var / WWW / HTML / --name name of the new container-with-MySQL php --link linked containers, links the vessel name: alias in the container, the container is a run, docker automatically added a host ip container identification linked --privileged = true rights issue

 

Into the container

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

 

4, the mirror mounting nginx

vim default.conf

{Server 
        the listen 80; 
        server_name localhost; 
    
        LOCATION / { 
            the root / var / WWW / HTML; 
            index the index.php index.html index.htm; # increase the index.php 
        } 
    
        #error_page 404 /404.html; 
    
        # error Server Pages to the redirect static Page /50x.html The 
        # 
        error_page 500 502 503 504 /50x.html; 
        LOCATION = {/50x.html 
            the root / var / WWW / HTML; 
        } 
        LOCATION ~ \ $ {.php 
            the root / var / WWW / HTML; # Code directory 
            fastcgi_pass phpfpm: 9000; # modify phpfpm container 
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; # 修改为$document_root
            include        fastcgi_params;
        }
    }

Run container

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

Parameter analysis

--link php-with-mysql: phpfpm the link to nginx php container to container, phpfpm is nginx container alias. 
--volumes-from php-with-mysql php-with-mysql the container mount of nginx also mounted to the vessel 
-v /var/www/nginx/conf/default.conf:/etc/nginx/conf .d / default.conf will replace nginx configuration files, configuration files written to mount local 

Docker -it nginx Exec-PHP bash 
root @ 32de01dbee49: / # cd / var / the 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 \

 

 

Execution docker-compose

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

 

Guess you like

Origin www.cnblogs.com/liuxd/p/11579117.html