docker随笔9--docker-compose安装lnmp环境
一个项目往往需要多个容器,容器与容器之间存在依赖关系,数量太多,启动是输入的命令太多,为了解决这些问题,采用docker-compose来完成,通过编写docker-compose.yml文件,启动项目,可以类比node里的package.json,执行编译只需npm build,docker-compose编译执行只需
docker-compose up -d
1.首先是安装docker-compose:
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose
2.构建目录结构:
我的目录结构如下
----web: | |---------nginx |----------------conf |----------------vhost |----------------web |---------php |---------mysql
3.下面是关于docker-compose的参数说明:
version : "3" #docker-compose的版本 services: #容器的集合 mysql: #项目名称 image: mysql:5.7 #镜像名称,如果是通过dockerfile创建的可以使用build属性 container_name: mysql #容器名称,如果没有这个属性的话,docker-compose会随机分配一个名字给容器 privileged: true #允许操作的表示,如果不加的话会出现类似,permission deny的错误 ports: - 3307:3306 #开放宿主机和容器的映射端口 environment: MYSQL_ROOT_PASSWORD: root #mysql镜像中的环境变量 php: image: php:7.2-fpm container_name: php privileged: true ports: - 9001:9000 links: - mysql #容器之间进行关联 volumes: - /docker/wwwroot:/www/web #挂载卷,需要注意的是,php在以模块的形式加载到nginx的时候,需要他们两个的目录结构一致,否则nginx无法加载php,但是 html等静态问价可以正常访问。 nginx: image: nginx container_name: nginx privileged: true links: - php ports: - 8088:80 volumes: - ./nginx/vhost:/www/nginx/vhost - ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro - ./nginx/log:/www/nginx/log - /docker/wwwroot:/www/web phpmyadmin: image: phpmyadmin/phpmyadmin container_name: phpmyadmin privileged: true links: - mysql ports: - 7001:80 environment: MYSQL_ROOT_PASSWORD: root PMA_HOST: mysql redis: image: redis:4.0.14 container_name: redis privileged: true ports: - 6379:6379 mongo: image: mongo restart: always ports: - 27017:27017
4.修改nginx.conf配置:
user nginx; worker_processes 1; error_log /www/nginx/log/error.log warn; pid /www/nginx/log/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /www/nginx/log/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /www/nginx/vhost/*.conf; }
5.修改default.conf配置
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; root /www/web; location / { index index.html index.htm index.php; try_files $uri $uri/ $uri.php?$args; } #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 /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_index index.php; fastcgi_pass php:9000; fastcgi_param SCRIPT_FILENAME $document_root$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; #} }
6.访问localhost:8088端口,并在web目录下新增test.php,通过localhost:8088/test 访问