linux服务器docker compose的使用步骤
之前说了docker的安装,docker compose的安装,还比较了dockerfile和docker compose的区别,那么docker compose的实际应用是怎么样呢?记录下我的实操步骤
1、服务器上新建目录,目录情况如下,我的data目录是挂载到数据盘的
/data/docker_config/nginx //存放nginx的配置文件 /data/docker_config/nginx/ceti //存放ssl证书 /data/docker_config/nginx/vhost //存放项目配置文件 /data/docker_config/nginx/enable-php.conf //php服务文件 /data/docker_config/nginx/nginx.conf //nginx基础配置文件 /data/docker_config/php //存放php的配置文件 /data/docker_config/php/php.ini //php.ini文件 /data/docker_config/php/php-fpm.conf //php-fpm基础配置文件 /data/docker_config/php/www.conf //php-fpm扩展配置文件 /data/docker_www //存放项目文件 /data/docker_www/test //test项目 /data/docker_www/test/index.html /data/docker_www/test/phpinfo.php /data/docker_logs/nginx //存放nginx日志文件 access.log,error.log 等 /data/docker_logs/php //存放php日志文件 php-fpm.error.log,php-fpm.slow.log等 /data/docker_compose //存放Compose相关文件 /data/docker_compose/docker-compose.yml //docker compose的配置文件 /data/docker_compose/Dockerfile_php73 //php7.3的dockerfile文件
2、把本地的docker_config里面的文件分别更新到服务器指定的目录,这里我贴下我的配置文件,方便参考
nginx.conf
events { use epoll; worker_connections 51200; multi_accept off; accept_mutex off; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 50m; sendfile on; sendfile_max_chunk 512k; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\."; server_tokens off; access_log off; server { listen 80 default_server reuseport; server_name _; index index.html index.htm index.php; root /www; location /nginx_status { stub_status on; access_log off; } location /fpm-status { fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; include fastcgi_params; fastcgi_pass php7.3:9000; } location /index.html { root /www/test; } location /phpinfo.php { root /www/test; include enable-php.conf; } } #引入项目配置文件 include vhost/*.conf; }
enable-php.conf
location ~ [^/]\.php(/|$) { try_files $uri = 404; fastcgi_pass php7.3:9000; fastcgi_index index.php; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REQUEST_SCHEME $scheme; fastcgi_param HTTPS $https if_not_empty; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200; include fastcgi_params; }
vhost/xxx.com.conf
server { listen 443 ssl; server_name xxx.com; ssl_certificate ceti/xxx.com.pem; ssl_certificate_key ceti/xxx.com.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL; ssl_prefer_server_ciphers on; index index.html index.php; root /www/xxx.com/web; include enable-php.conf; location / { #add_header Web-Server '1'; #if (!-f $request_filename){ # rewrite ^/ /index.php; #} index index.html index.htm index.php; try_files $uri $uri/ /index.php$is_args$args; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ { #expires 30d; } location ~ .*\.(js|css)?$ { #expires 12h; } access_log /wwwlogs/xxx.com.access.log; error_log /wwwlogs/xxx.com.error.log; } server { listen 80; server_name xxx.com; return 301 https://xxx.com; }
Dockerfile_php73
FROM php:7.3-fpm MAINTAINER wangqijun wangqijun@pipaw.net RUN docker-php-ext-install mysqli RUN docker-php-ext-enable mysqli RUN docker-php-ext-install pdo_mysql RUN docker-php-ext-enable pdo_mysql RUN docker-php-ext-install bcmath RUN docker-php-ext-enable bcmath WORKDIR /www CMD ["php-fpm"]
docker-compose.yml
version: 'v2.20.1' services: nginx: image: nginx ports: - 80:80 volumes: - "/data/docker_config/nginx/nginx.conf:/etc/nginx/nginx.conf" - "/data/docker_config/nginx/enable-php.conf:/etc/nginx/enable-php.conf" - "/data/docker_config/nginx/vhost:/etc/nginx/vhost" - "/data/docker_config/nginx/ceti:/etc/nginx/ceti" - "/data/docker_www:/www" - "/data/docker_logs/nginx:/wwwlogs" depends_on: - php7.3 php7.3: build: context: . dockerfile: Dockerfile_php73 ports: - 9000:9000 volumes: - "/data/docker_config/php/php-fpm.conf:/usr/local/etc/php-fpm.conf" - "/data/docker_config/php/www.conf:/usr/local/etc/php-fpm.d/www.conf" - "/data/docker_config/php/php.ini:/usr/local/etc/php/php.ini" - "/data/docker_www:/www" - "/data/docker_logs/php:/wwwlogs"
3、切换到/data/docker_compose目录下执行,也可以在其他目录执行
docker-compose up -d //方法1:在docker-compose.yml目录下执行
docker-compose -f /data/docker_compose/docker-compose.yml up -d //方法二:在任意目录执行,指定docker-compose.yml文件路径
4、成功启动服务之后,验证
http://ip/nginx_status 查看nginx状态
http://ip/fpm-status 查看php-fpm状态
http://ip/phpinfo.php 查看phpinfo信息
http://ip/index.html 查看访问状态
5、特别说明
vhost目录下的项目配置文件,不论是root的目录,还是access_log、error_log的目录等其他目录,一定要写容器里面的对应目录,不要写本地服务器的目录哦,举例说明:
docker-compose.yml里面,已经建立了对应的目录映射关系,因此需要注意以下几点:
1、在项目配置文件里面,这里就必须写容器里面的目录,我这里是/www,而不能写服务器上/data/docker_www目录了
2、因为在容器服务运行的时候已经建立好了目录对应的关系,所以我们的实际项目文件还是存放到/data/docker_www目录下即可,会自动同步到容器里面的/www目录下的
3、就算容器销毁,项目数据也不会受影响,容器销毁,容器里面的/www/目录下的数据没有了,但是本地服务器上的目录下的数据依然 存在的
4、这里的同步变化的前提是,容器服务正常运行,无论在哪边做增删改,对应的一边都会跟着变化,但是如果容器服务被销毁,docker stop xxx,docker rm xxx 这种只会把容器服务以及里面的数据销毁,不会销毁服务器本身的/data/docker_www目录下的数据
/data/docker_www <=> /data/ 2个目录数据是双向同步的
1、左边数据变化了,右边也会同步跟着变化
2、右边数据变化了,左边也会同步跟着变化
服务器的项目存放目录
nginx容器里面的对应目录