Docker compose
简介
Compose
是用于定义和运行多容器 Docker 应用程序的工具。通过 Compose
,您可以使用 YML 文件来配置应用程序需要的所有服务。然后,使用一个命令,就可以从 YML 文件配置中创建并启动所有服务。
使用docker compose
搭建一个 lnmp
准备工作:
1. 选择一个系统,本文章使用系统为`win10 商店里的 Ubuntu`, 也可以使用虚拟机等其他方案
2. 安装`docker`
创建配置文件夹
创建一个配置文件夹,存放docker lnmp
的配置文件,本文放在当前用户下
cd ~
#创建配置文件夹
mkdir config
#创建 html 文件夹
mkdir html
创建 nginx
配置文件
cd ~/config
vim nginx.conf
# 添加内容
# Default server configuration
#
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /usr/share/nginx/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
# With php-fpm (or other unix sockets):
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
}
创建 docker-compose
配置文件
cd ~
vim docker-compose.yml
# 输入一下内容
version: "3"
services:
nginx:
image: nginx:alpine
ports:
- 80:80
volumes:
- ~/html:/usr/share/nginx/html
- ~/conf/nginx.conf:/etc/nginx/nginx.conf
php:
image: devilbox/php-fpm:5.2-work-0.89
volumes:
- ~/html:/var/www/html
mysql:
image: mysql:5.6
environment:
- MYSQL_ROOT_PASSWORD=123456
创建测试 html
页面
在 html
文件夹中创建各类文件,用来测试,主要有 index.html,info.php,sql_conn.php
cd ~/html
#创建 index.html 用来测试 nginx
echo "hello word" > index.html
#创建 info.php 用来测试 php -e 代表转义换行符
echo -e "<?php \n\r echo phpinfo();\n?>" > info.php
创建 sql_conn.php
,测试php
连接mysql
vim sql_conn.php
# 输入一下测试代码<?php
$host = "mysql";
$user = "root";
$password = "123456";
$conn = mysqli_connect($host,$user,$password);
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
echo "Connection success!"
?>
启动 docker
cd ~
# 启动docker
docker-compose up -d
# 停止docker
docker-compose down
参考视频: