Docker Compose
Compose是什么
Compose是一个定义和管理多容器的工具,也是一种容器编排工具,前身是Pig,使用Python语言编写。使用Compose配置文件描述多个容器应用的构架,比如使用什么镜像,数据卷,网络,映射端口等;然后用一条命令管理所有服务,比如启动,停止,重启等
系统为ubuntu18.04 切换root用户使用命令 sudo -i
安装(也可以github下载二进制包安装)
1 2 | sudo apt - get install python - pip sudo pip install docker - compose |
查看版本
1 | docker - compose - - version |
安装docker
1 | apt install docker.io |
查看版本
1 | docker info |
docker-compose读取文件夹下面的yml文件
编辑文件docker-compose.yml
1 2 3 4 5 6 | version: '3' services: web: build: . ports: - "8888:80" |
version:定义版本有1,2,3版本
services:定义服务
web:服务名是web
build:在本目录构建
ports:端口映射8888映射容器的80端口
编辑Dockerfile
1 2 3 4 5 6 | FROM centos: 6 MAINTAINER liuyueming RUN yum install - y httpd php php - gd php - mysql RUN echo "<?php phpinfo()?>" > / var / www / html / index.php CMD [ "/usr/sbin/httpd" , "-D" , "FOREGROUND" ] EXPOSE 80 |
PS:参数D要加- 否则启动失败
文件目录如下
构建
1 | docker - compose up |
构建后会生成名为php_web的镜像(镜像默认命名规则为文件夹_服务名)
完毕查看
1 2 | docker - compose ps docker ps |
web页面访问 http://ip:8888
一键配置LNMP环境
配置文件如下
docker-compose.yml
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 | version: '3' services: nginx: hostname: nginx build: context: . / nginx dockerfile: Dockerfile ports: - 80 : 80 links: - php:php - cgi volumes: - . / wwwroot: / usr / local / nginx / html php: hostname: php build: . / php links: - mysql:mysql - db volumes: - . / wwwroot: / usr / local / nginx / html mysql: hostname: mysql image: mysql: 5.6 ports: - 3306 : 3306 volumes: - . / mysql / conf: / etc / mysql / conf.d - . / mysql / data: / var / lib / mysql # command: --character-set-server=utf8 environment: MYSQL_ROOT_PASSWORD: 123456 MYSQL_DATABASE: wordpress MYSQL_USER: user MYSQL_PASSWORD: user123 |
mysql/conf/my.cnf
1 2 3 4 5 6 7 8 9 | [mysqld] user = mysql port = 3306 datadir = / var / lib / mysql socket = / var / lib / mysql / mysql.sock pid - file = / var / run / mysqld / mysql.pid log_error = / var / log / mysql / error.log character_set_server = utf8 max_connections = 3600 |
nginx/Dockerfile
1 2 3 4 5 6 7 8 9 10 | FROM centos: 6 MAINTAINER liuyueming RUN yum install - y gcc gcc - c + + make openssl - devel pcre - devel ADD nginx - 1.12 . 1.tar .gz / tmp RUN cd / tmp / nginx - 1.12 . 1 && . / configure - - prefix = / usr / local / nginx && make - j 2 && make install RUN rm - f / usr / local / nginx / conf / nginx.conf COPY nginx.conf / usr / local / nginx / conf EXPOSE 80 CMD [ "/usr/local/nginx/sbin/nginx" , "-g" , "daemon off;" ] |
nginx/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 | user root; worker_processes auto; error_log logs / error.log info; pid logs / nginx.pid; events { use epoll; } http { include 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 logs / access.log main; sendfile on; keepalive_timeout 65 ; server { listen 80 ; server_name localhost; root html; index index.html index.php; location ~ \.php$ { root html; fastcgi_pass php - cgi: 9000 ; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } |
php/Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | FROM centos: 6 MAINTAINER lizhenliang RUN yum install - y gcc gcc - c + + gd - devel libxml2 - devel libcurl - devel libjpeg - devel libpng - devel openssl - devel ADD php - 5.6 . 31.tar .gz / tmp / RUN cd / tmp / php - 5.6 . 31 && \ . / configure - - prefix = / usr / local / php \ - - with - config - file - path = / usr / local / php / etc \ - - with - mysql - - with - mysqli \ - - with - openssl - - with - zlib - - with - curl - - with - gd \ - - with - jpeg - dir - - with - png - dir - - with - iconv \ - - enable - fpm - - enable - zip - - enable - mbstring && \ make - j 4 && make install && \ cp / usr / local / php / etc / php - fpm.conf.default / usr / local / php / etc / php - fpm.conf && \ sed - i "s/127.0.0.1/0.0.0.0/" / usr / local / php / etc / php - fpm.conf && \ cp . / sapi / fpm / init.d.php - fpm / etc / init.d / php - fpm && \ chmod + x / etc / init.d / php - fpm COPY php.ini / usr / local / php / etc EXPOSE 9000 CMD / etc / init.d / php - fpm start && tail - F / var / log / messages |
php/php.ini
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | [PHP] engine = On short_open_tag = Off asp_tags = Off precision = 14 output_buffering = 4096 zlib.output_compression = Off implicit_flush = Off unserialize_callback_func = serialize_precision = 17 disable_functions = disable_classes = zend.enable_gc = On expose_php = On max_execution_time = 30 max_input_time = 60 memory_limit = 128M error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT display_errors = Off display_startup_errors = Off log_errors = On log_errors_max_len = 1024 ignore_repeated_errors = Off ignore_repeated_source = Off report_memleaks = On track_errors = Off html_errors = On variables_order = "GPCS" request_order = "GP" register_argc_argv = Off auto_globals_jit = On post_max_size = 8M auto_prepend_file = auto_append_file = default_mimetype = "text/html" default_charset = "UTF-8" doc_root = user_dir = enable_dl = Off file_uploads = On upload_max_filesize = 2M max_file_uploads = 20 allow_url_fopen = On allow_url_include = Off default_socket_timeout = 60 [CLI Server] cli_server.color = On [Date] date.timezone = Asia / Shanghai [ filter ] [iconv] [intl] [sqlite3] [Pcre] [Pdo] [Pdo_mysql] pdo_mysql.cache_size = 2000 pdo_mysql.default_socket = [Phar] [mail function] SMTP = localhost smtp_port = 25 mail.add_x_header = On [SQL] sql.safe_mode = Off [ODBC] odbc.allow_persistent = On odbc.check_persistent = On odbc.max_persistent = - 1 odbc.max_links = - 1 odbc.defaultlrl = 4096 odbc.defaultbinmode = 1 [Interbase] ibase.allow_persistent = 1 ibase.max_persistent = - 1 ibase.max_links = - 1 ibase.timestampformat = "%Y-%m-%d %H:%M:%S" ibase.dateformat = "%Y-%m-%d" ibase.timeformat = "%H:%M:%S" [MySQL] mysql.allow_local_infile = On mysql.allow_persistent = On mysql.cache_size = 2000 mysql.max_persistent = - 1 mysql.max_links = - 1 mysql.default_port = mysql.default_socket = mysql.default_host = mysql.default_user = mysql.default_password = mysql.connect_timeout = 60 mysql.trace_mode = Off [MySQLi] mysqli.max_persistent = - 1 mysqli.allow_persistent = On mysqli.max_links = - 1 mysqli.cache_size = 2000 mysqli.default_port = 3306 mysqli.default_socket = mysqli.default_host = mysqli.default_user = mysqli.default_pw = mysqli.reconnect = Off [mysqlnd] mysqlnd.collect_statistics = On mysqlnd.collect_memory_statistics = Off [OCI8] [PostgreSQL] pgsql.allow_persistent = On pgsql.auto_reset_persistent = Off pgsql.max_persistent = - 1 pgsql.max_links = - 1 pgsql.ignore_notice = 0 pgsql.log_notice = 0 [Sybase - CT] sybct.allow_persistent = On sybct.max_persistent = - 1 sybct.max_links = - 1 sybct.min_server_severity = 10 sybct.min_client_severity = 10 [bcmath] bcmath.scale = 0 [browscap] [Session] session.save_handler = files session.use_strict_mode = 0 session.use_cookies = 1 session.use_only_cookies = 1 session.name = PHPSESSID session.auto_start = 0 session.cookie_lifetime = 0 session.cookie_path = / session.cookie_domain = session.cookie_httponly = session.serialize_handler = php session.gc_probability = 1 session.gc_divisor = 1000 session.gc_maxlifetime = 1440 session.referer_check = session.cache_limiter = nocache session.cache_expire = 180 session.use_trans_sid = 0 session.hash_function = 0 session.hash_bits_per_character = 5 url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry" [MSSQL] mssql.allow_persistent = On mssql.max_persistent = - 1 mssql.max_links = - 1 mssql.min_error_severity = 10 mssql.min_message_severity = 10 mssql.compatibility_mode = Off mssql.secure_connection = Off [Assertion] [COM] [mbstring] [gd] [exif] [Tidy] tidy.clean_output = Off [soap] soap.wsdl_cache_enabled = 1 soap.wsdl_cache_dir = "/tmp" soap.wsdl_cache_ttl = 86400 soap.wsdl_cache_limit = 5 [sysvshm] [ldap] ldap.max_links = - 1 [mcrypt] [dba] [opcache] [curl] [openssl] |
wwwroot/test.php
1 | <?php phpinfo()?> |
构建
1 | docker - compose up |
成功后查看
页面访问
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2017-12-19 CentOS设置PPTP拨号连接远程服务器