在微服务架构的应用中,一般都会有若干个微服务,每个微服务都会部署多个实例,如果每个实例都要我们手动启动的话,运维的工作量会非常的大,而docker-compose 的作用就是来管理这些实例,可以按照一定的业务规则批量启动这些实例,docker-compose 是一个编排多容器分布式部署的工具,提供命令集管理容器体弱应 用的完整开发周器,其中有构建,启动和停止。
# 最新地址 # https://github.com/docker/compose/releases # 下载docker-compos sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose # 为docker-compos 赋予可执行权限 chmod +x /usr/local/bin/docker-compos
安装成功!
卸载docker-compose
rm /usr/local/bin/docker-compose
Docker-compose 只有在多容器的时候,才能体现出来作用,所以这一次实验的项目为一个简单的 go+nginx 。
# 在当前目录下 mkdir -p ./docker-compose cd docker-compose wget -c https://nginx.org/download/nginx-1.12.2.tar.gz mkdir -p ./nginx/conf touch ./docker-compose.yml touch ./nginx/conf/nginx.conf touch ./hello_dockerfile touch ./nginx_dockerfile touch ./start.sh chmod +x ./start.sh
version: '3' services: nginx: image: compose_nginx:1.0 ports: - 80:80 links: - compose_hello volumes: - ./nginx/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf compose_hello: image: compose_hello:1.0 expose: - "8080"
# hello_dockerfile FROM centos:7 MAINTAINER frost <ycr1997@163.com> ADD hello_world hello RUN chmod +x hello EXPOSE 8080 CMD ./hello
步骤可以查看:Docker | dockerfile 文件编写
# nginx_dockerfile # base image FROM centos:7 # MAINTAINER MAINTAINER frost <ycr1997@163.com> # put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx ADD nginx-1.12.2.tar.gz /usr/local/src # running required command RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel RUN useradd -M -s /sbin/nologin nginx # change dir to /usr/local/src/nginx-1.12.2 WORKDIR /usr/local/src/nginx-1.12.2 # execute command to compile nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install ENV PATH /usr/local/nginx/sbin:$PATH EXPOSE 80 ENTRYPOINT ["nginx"] CMD ["-g","daemon off;"]
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } 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; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://compose_hello:8080; } #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 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$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$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; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
# 制作 nginx 镜像 docker build -f ./nginx_dockerfile -t compose_nginx:1.0 . # 镜作 应用 hello 镜像 docker build -f ./hello_dockerfile -t compose_hello:1.0 . # 批量启动容器 docker-compose up
细节决定成败!
个人愚见,如有不对,恳请斧正!