docker-compose

以lnmp为例讲解docker-compose使用

1,在宿主机/root目录下创建连个文件夹

1
2
3
4
5
cd /root
# 在宿主机创建配置文件夹,当前路径在 /root
mkdir conf
# 创建html文件夹
mkdir html

2,在html 目录中创建测试文件 index.html test.php mysql.php

  • 创建index.html

    1
    vi index.html

    输入如下内容

    1
    hello world
  • 创建test.php

    1
    vi test.php

    输入如下内容

    1
    2
    3
    <?php
    phpinfo();
    ?>
  • 创建mysql.php

    1
    vi mysql.php

    输入如下内容

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php
    $servername = "mysql";
    $username = "root";
    $password = "123456";


    $conn = mysqli_connect($servername, $username, $password);
    if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    }
    echo "Connection finished!";
    ?>

3,在conf创建nginx配置文件

  • 创建nginx.conf

    1
    vi nginx.conf

    添加内容

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    # 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;
    #}
    }
    }

4,创建docker-compose的配置文件 docker-compose.yml

  • 创建yml配置文件

    1
    2
    cd /root
    vi docker-compose.yml

    输入如下内容

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    version: "3"
    services:
    nginx:
    image: nginx:alpine
    ports:
    - 80:80
    volumes:
    - /root/html:/usr/share/nginx/html
    - /root/conf/nginx.conf:/etc/nginx/nginx.conf
    php:
    image: devilbox/php-fpm:5.2-work-0.89
    volumes:
    - /root/html:/var/www/html
    mysql:
    image: mysql:5.6
    environment:
    - MYSQL_ROOT_PASSWORD=123456

5,启动docker

1
2
3
4
5
cd /root
# 启动docker
docker-compose up -d
# 停止docker
docker-compose down

6,结构图