用Docker搭建LNMP环境

人类的本质是复读机。

安装docker

查看内核版本

1
2
$ uname -r
3.10.0-957.el7.x86_64

更新yum包

1
$ yum update

添加包源

1
2
3
4
5
$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

查看版本

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
$ yum list docker-ce --showduplicates | sort -r
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror, langpacks
docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
Available Packages

安装

1
$ yum install docker-ce-18.03.1.ce

启动

1
$ systemctl start docker

设置开机启动 (可选)

1
2
$ systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

查看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ docker version
Client:
Version: 18.03.1-ce
API version: 1.37
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:20:16 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm

Server:
Engine:
Version: 18.03.1-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:23:58 2018
OS/Arch: linux/amd64
Experimental: false

安装docker-compose

从github安装

1
$ curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

给予执行权限

1
$ chmod +x /usr/local/bin/docker-compose

查看

1
2
$ docker-compose --version
docker-compose version 1.24.1, build 4667896b

搭建LNMP

这里直接用yeszao/dnmp的配置。

选取目录

选择一个存放容器配置的目录(只是配置文件位置,镜像与容器在/var/lib/docker目录中)

1
$ cd ~ && mkdir dnmp && cd dnmp

拉取配置

1
2
$ git clone git@github.com:yeszao/dnmp.git # 这里用的是v2.0.4的tag
$ cp env.sample .env && cp docker-compose.sample.yml docker-compose.yml

修改配置

这里docker-compose.yml 只开启了nginxphpmysql8redis.env 给php加了一些扩展。

1
2
3
4
5
6
# .env
PHP_VERSION=7.2.19
PHP_PHP_CONF_FILE=./conf/php.ini
PHP_FPM_CONF_FILE=./conf/php-fpm.conf
PHP_LOG_DIR=./log/php
PHP_EXTENSIONS=pdo_mysql,mysqli,mbstring,gd,curl,opcache,redis,imap,swoole,xhprof,acpu

所有配置文件都在~/dnmp/services/目录中,所有日志都在~/dnmp/log中,具体见yeszao/dnmp

执行构建

在配置文件处执行

1
2
3
4
5
6
$ docker-compose up -d
Creating network "dnmp_default" with the default driver
Creating redis ... done
Creating mysql ... done
Creating php ... done
Creating nginx ... done

初始构建需要一定时间,如果构建时无法下载组件,需要给容器指定dns服务器。

  1. 查看宿主机nameserver

    1
    2
    3
    4
    5
    6
    7
    8
    $ cat /etc/resolv.conf
    # Your system has been configured with 'manage-resolv-conf' set to true.
    # As a result, cloud-init has written this file with configuration data
    # that it has been provided. Cloud-init, by default, will write this file
    # a single time (PER_ONCE).
    #
    nameserver 183.60.83.19
    nameserver 183.60.82.98
  2. 新建或修改/etc/default/docker

    1
    DOCKER_OPTS="--dns 183.60.83.19"
  3. 重启docker

    1
    systemctl restart docker

查看容器

1
2
3
4
5
6
7
$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------
mysql docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp
nginx nginx -g daemon off; Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
php docker-php-entrypoint php-fpm Up 9000/tcp, 9501/tcp
redis redis-server /etc/redis.conf Up 0.0.0.0:6379->6379/tcp

配置命令别名

bash.alias.sample提取需要的别名到~/.bashrc~/.zshrc中,然后重新source生效

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
alias dnginx='docker exec -it nginx /bin/sh'
alias dphp='docker exec -it php /bin/sh'
alias dmysql='docker exec -it mysql /bin/bash'
alias dredis='docker exec -it redis /bin/sh'

php () {
tty=
tty -s && tty=--tty
docker run \
$tty \
--interactive \
--rm \
--volume $PWD:/www:rw \
--workdir /www \
dnmp_php php "$@"
}

composer() {
tty=
tty -s && tty=--tty
docker run \
$tty \
--interactive \
--rm \
--user www-data:www-data \
--volume ~/dnmp/data/composer:/tmp/composer \
--volume $(pwd):/app \
--workdir /app \
dnmp_php composer "$@"
}

References

Get Docker CE for CentOS
Install Docker Compose
yeszao/dnmp