欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

docker-compose

程序员文章站 2022-03-04 21:07:40
...

1、介绍


        Compose是一个定义和管理多容器的工具,使用Python语言编写。使用Compose配置文件描述多个容器应用的架构,比如使用什么镜像、数据卷、网络、映射端口等;然后一条命令管理所有服务,比如启动、停止、重启等。

2、安装

curl -L https://github.com/docker/compose/releases/download/1.15.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
或者
pip install docker-compose

 

3、nginx-tomcat-mysql架构

以下以安装nginx-tomcat-mysq架构简单展现Compose的使用

nginx Dockerfile文件

用于构建nginx镜像

FROM centos:7
MAINTAINER www.luoshuyu.com
RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel
#把宿主机当前目录下的nginx包拷贝到镜像/tmp目录并解压
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
#删除nginx二进制文件
RUN rm -rf /tmp/nginx-1.12.1* && yum clean all
#拷贝宿主机当前目录下的已经配置好的nginx配置文件进行替换
COPY nginx.conf /usr/local/nginx/conf

tomcat Dockerfile文件

用于构建tomcat镜像

FROM centos:7
MAINTAINER www.luoshuyu.com
#宿主机jdk路径
ENV JAVA_HOME /usr/local/jdk1.8.0_45

ADD apache-tomcat-8.0.46.tar.gz /usr/local
#拷贝宿主机当前目录下的已经配置好的tomcat配置文件进行替换
COPY server.xml /usr/local/apache-tomcat-8.0.46/conf
RUN sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/apache-tomcat-8.0.46/bin/catalina.sh 

RUN rm -f /usr/local/*.tar.gz

WORKDIR /usr/local/apache-tomcat-8.0.46
EXPOSE 8080
ENTRYPOINT ["./bin/catalina.sh", "run"]

mysql
mysql直接使用基础镜像,创建mysql目录用于存放配置文件和数据文件即可:
./mysql/conf
./mysql/data:/var/lib/mysql

YAML语法


YAML是一种标记语言很直观的数据序列化格式,可读性高。类似于XML数据描述语言,语法比XML简单的很多。
YAML数据结构通过缩进来表示,连续的项目通过减号来表示,键值对用冒号分隔,数组用中括号括起来,hash用花括号括起来。
YAML文件格式注意事项:
1.不支持制表符tab键缩进,需要使用空格缩进
2.通常开头缩进2个空格
3.字符后缩进1个空格,如冒号、逗号、横杆
4.用井号注释
5.如果包含特殊字符用单引号引起来
6.布尔值(true、false、yes、no、on、off)必须用引号括起来,这样分析器会将他们解释为字符串。

compose字段说明

docker-compose

docker-compose.yml

构建nginx-tomcat-mysql

version: '3'
services:
#nginx
  nginx:
    hostname: nginx
    build:
      context: ./nginx #上下文,即相对目录
      dockerfile: Dockerfile #Dockerfile构建镜像
    ports:
      - 82:80 #主机82端口映射到容器80
    networks: #使用网络
      - lnmt
    volumes:  #主机./webapps映射到容器目录
      - ./webapps:/opt/webapps
#tomcat
  tomcat01:
    hostname: tomcat01
    build: ./tomcat
    networks:
      - lnmt
    volumes:
      - /usr/local/jdk1.8.0_45:/usr/local/jdk1.8.0_45
      - ./webapps:/usr/local/apache-tomcat-8.0.46/webapps

  tomcat02:
    hostname: tomcat02
    build: ./tomcat
    networks:
      - lnmt
    volumes:
      - /usr/local/jdk1.8.0_45:/usr/local/jdk1.8.0_45
      - ./webapps:/usr/local/apache-tomcat-8.0.46/webapps
#mysql
  mysql:
    hostname: mysql
    image: mysql:5.6
    ports:
      - 3307:3306
    networks:
      - lnmt
    volumes:
      - ./mysql/conf:/etc/mysql/conf.d #主机mysql配置文件和数据目录映射到容器目录
      - ./mysql/data:/var/lib/mysql
    environment: #环境变量
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: db
      MYSQL_USER: user
      MYSQL_PASSWORD: user123

networks: #定义网络
  lnmt:

一键构建nginx-tomcat-mysql架构

docker-compose -f docker-compose.yml up -d
#-f 指定yml文件 -d 后台运行

4、附录(相关配置文件)

nginx

#nginx.conf
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;
    
    upstream www.example.com {
        #ip_hash;
        server tomcat01:8080;
	server tomcat02:8080;
    }

    server {
        listen 80;
        server_name localhost;
	
	location / {
	    proxy_pass http://www.example.com;
	}
        location ~ \.(html|css|js|jpg|png|gif)$ {
            root /opt/webapps/ROOT;
        }
    }
}

tomcat

#server.xml
<?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

 
  <GlobalNamingResources>
   
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

 
  <Service name="Catalina">

    
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    

    
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    
    <Engine name="Catalina" defaultHost="localhost">

      
      <Realm className="org.apache.catalina.realm.LockOutRealm">
       
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

       
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

mysql

#my.cnf
[mysqld]
user=mysql
port=3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysql/mysql.pid
log_error=/var/log/mysql/error.log
character_set_server = utf8
max_connections=3600

 

相关标签: docker compose