docker-compose understand:
Each service must specify mirror or build instructions (Dockerfile need) or the like through the image generation instruction to automatically build the mirror.
The first directly specify the mirror to build a simple LNMP, as an entry docker-compose learning
Actual operations are necessarily Dockerfile need to configure in order to meet their individual needs
surroundings:
visualbox ubuntu 1804 local mysql deleted
1, the installation docker-compose
Official website
https://docs.docker.com/compose/
a nice portal
https://docker_practice.gitee.io/compose/compose_file.html
-
Pre-set about Ali cloud Docker accelerate
again copy it, save always forget, each time to find
the ** below https://2xxxxxxx.mirror.aliyuncs.com is the exclusive address of my application accelerator, remember into their own the address of1. 安装/升级Docker客户端 推荐安装1.10.0以上版本的Docker客户端,参考文档 docker-ce 2. 配置镜像加速器 针对Docker客户端版本大于 1.10.0 的用户 您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://2xxxxxxx.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
Screenshot below:
-
Docker-compose the installation
according to the official website operationsudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Too slow! So, they went a little domestic mirroring
domestic mirrored reference article
https://blog.csdn.net/huiyanghu/article/details/82253886
According to the above referenced article copied over
curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
Sure enough, seconds up! Next, set to run
chmod +x /usr/local/bin/docker-compose
just to confirm:
$docker-compose --version
docker-compose version 1.24.0, build 0aa59064
2, the first attempt to establish a project: LNMP
-
Establishment of a separate study compose a directory
mkdir cd compose compose &&
mkdir lnmp1 && cd lnmp1
establish a mysql data storage directory, a nginx / php storage directory
mkdir mysql
mkdir nginx && cd nginx
in accordance with nginx websites habits, establish a conf.d directory and a www directory
at www / html pre-written a index.php
*** superfluous preceded by a few words just to confirm it easy!<h3> wellcome WZH! <h4> this is a docker demo <h5> 20190416 <p>==================</p> <?php phpinfo(); ?>
The final directory structure is as follows
$ tree lnmp -d
lnmp
├── mysql
│ └── data
│ ├── mysql
│ ├── performance_schema
│ └── sys
└── nginx
├── conf.d
└── www
└── html
3, start writing templates
-
Reference:
https://www.jianshu.com/p/7fff488604c1
https://www.cnblogs.com/blogscc/p/9473123.html -
In advance to confirm port occupancy
ss -tlnp | grep 3306
LIST 0 80 127.0.0.1:3306 0.0.0.0:*
ss -tlnp |grep 80
LISTEN 0 80 127.0.0.1:3306 0.0.0.0:*
LISTEN 0 128 *:80 *:*
** My 3306 and 80 ports are already occupied, so the following script host port 3307 and replaced by 81
-
They were tested nginx, php, mysql mirror and the container run
-
Write-compose.yml template file Docker
$ Docker CAT-compose.ymlversion: "3" services: web_server: image: nginx:latest container_name: nginx links: - php:php ports: - "81:80" - "443:443" volumes: - ./nginx/www/html:/var/www/html - ./nginx/conf.d:/etc/nginx/conf.d db_server: image: mysql:5.7.21 container_name: mysql environment: MYSQL_ROOT_PASSWORD: 123456 ports: - "3307:3306" volumes: - ./mysql/data:/var/lib/mysql php: image: php:5.6-fpm container_name: php links: - db_server:mysql ports: - "9000:9000" volumes: - ./nginx/www/html:/var/www/html - ./nginx/conf.d:/etc/nginx/conf.d
-
Run the test
in the current directory
$ Docker Compose-up -d
MySQL DATE IS up-to-
PHP up-to-DATE IS
nginx up-to-DATE IS** If you modify the docker-compose.yml, stop before starting again
$ Docker Compose-Down
Stopping nginx ... DONE
Stopping PHP ... DONE
Stopping MySQL ... DONE
Removing nginx ... DONE
Removing PHP ... DONE
Removing MySQL ... DONEConfirm whether the container start
$ docker ps -sCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE 6bb7eb2c7665 nginx:latest "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:443->443/tcp, 0.0.0.0:81->80/tcp nginx 2B (virtual 109MB) 47f3ed37ac21 php:5.6-fpm "docker-php-entrypoi…" About an hour ago Up About an hour 0.0.0.0:9000->9000/tcp php 0B (virtual 344MB) 41d22b0b79f0 mysql:5.7.21 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3307->3306/tcp mysql
curl 127.0.0.1:81
-
Browser opens http://192.168.1.193:81/
4, error handling
-
404 Not Found curl 127.0.0.1:81/index.php always
because of default.conf conf.d copied over the actual environment, php parsing
fastcgi_pass 127.0.0.1:9000;
now need to be replaced php container name
fastcgi_pass php: 9000 ; -
When docker ps -s View, nginx does not start container always
replace php container name is not written correctly
The following is a copy default.conf came after the modification from the reference article
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html index.htm 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 127.0.0.1:9000;
# 必须修改为compose模版文件中php容器名字
fastcgi_pass php:9000
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 修改为$document_root
include fastcgi_params;
}
}