Docker-compose安装
1 2 3 4 5
| #安装docker依赖 sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo sudo yum makecache fast sudo yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
|
1 2 3 4 5 6 7 8
| sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo sudo yum makecache fast #安装docker依赖 sudo yum install -y yum-utils # 安装最新版本 sudo yum install -y docker-ce docker-compose-plugin # 安装指定版本 sudo yum install -y docker-ce-20.10.17 docker-compose-plugin
|
1 2 3 4 5 6 7
| # 启动服务 systemctl enable docker --now find / -name docker-compose #首先,查找 Docker Compose 的位置 usr/libexec/docker/cli-plugins/docker-compose chmod +x /usr/libexec/docker/cli-plugins/docker-compose mv /usr/libexec/docker/cli-plugins/docker-compose /usr/local/bin docker-compose version
|
Docker-compose管理命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #官网: https://docs.docker.com/engine/reference/commandline/compose/ docker compose -p grafana up -d # 运行服务(-p指定服务名) docker compose up -d # 后台运行服务 docker compose ls # 查看服务(服务名已当前所在目录命名) docker compose ps # 查看容器状态 docker compose -p grafana ps # 指定服务名查看 docekr compose restart # 重启所有服务 docker compose stop # 停止服务 docekr compose start # 启动服务 docker compose down # 删除服务 docker-compose pause # 暂停已创建的Docker Compose项目中的服务容器 docker-compose unpause # 取消暂停已创建的Docker Compose项目中的服务容器 docker-compose logs # 查看Docker Compose项目中的服务容器的日志输出 docker-compose build # 根据Docker Compose文件中的配置构建镜像 docker compose -p grafana exec grafana bash # 进入容器(名称看SERVICE字段) docker compose -p grafana logs -f grafana # 查看日志 docker compose -p grafana logs -f --tail 10 grafana # 查看指定的行数 docker-compose --help # 查看所有可用的命令和选项
|
Docker Compose配置文件
通常是docker-compose.yml
支持多个选项,用于定义和配置服务、网络、卷、环境变量等。以下是一些常用的选项:
1 2 3 4 5 6 7 8 9 10 11 12 13
| #Docker Compose 配置文件参考:https://docs.docker.com/compose/compose-file/ 1. version:指定Docker Compose文件格式的版本号。 2. services:定义要运行的服务。每个服务都可以包含多个配置选项,如image、container_name、ports、volumes、environment等。 3. networks:定义要使用的网络。可以创建自定义网络,并将服务连接到指定的网络。 4. volumes:定义要使用的卷(数据卷)。可以指定主机路径或命名卷。 5. environment:设置环境变量,用于传递配置参数给服务容器。 6. depends_on:定义服务之间的依赖关系,指定一个或多个依赖的服务。 7. ports:指定将容器的端口映射到主机的端口。 8. expose:指定容器要公开的端口,但不映射到主机的端口。 9. restart:指定服务在退出后的重启策略。 10. command:覆盖容器启动时的默认命令。 11. volumes_from:从其他容器或服务挂载卷。 12. external_links:连接到其他容器或服务。
|
Docker-compose案例
ELK(Elasticsearch, Logstash, Kibana)集群
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
| version: '3' services: elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch:7.15.1 container_name: elasticsearch environment: - node.name=elasticsearch - cluster.name=elasticsearch-cluster - discovery.seed_hosts=elasticsearch2,elasticsearch3 - cluster.initial_master_nodes=elasticsearch,elasticsearch2,elasticsearch3 ports: - 9200:9200 networks: - elk-network
logstash: image: docker.elastic.co/logstash/logstash:7.15.1 container_name: logstash volumes: - ./logstash.conf:/usr/share/logstash/pipeline/logstash.conf command: logstash -f /usr/share/logstash/pipeline/logstash.conf depends_on: - elasticsearch networks: - elk-network
kibana: image: docker.elastic.co/kibana/kibana:7.15.1 container_name: kibana ports: - 5601:5601 depends_on: - elasticsearch networks: - elk-network
networks: elk-network: driver: bridge
|
1 2 3 4 5 6 7
| 请注意以下几点: 1. 此示例使用ELK版本为7.15.1,你可以根据需要更改版本号。 2. 在`elasticsearch`服务中,设置了`node.name`、`cluster.name`、`discovery.seed_hosts`和`cluster.initial_master_nodes`等环境变量。你可以根据实际需求进行调整。 3. `logstash`服务使用了一个`logstash.conf`配置文件,你需要将其与Docker Compose文件放在同一目录下,并根据你的日志处理需求进行相应的配置。 4. `kibana`服务将Kibana的5601端口映射到主机的5601端口,以便通过浏览器访问Kibana界面。 5. 使用了一个名为`elk-network`的自定义网络,用于将所有ELK容器连接到同一个网络中,以便它们可以相互通信。 将上述内容保存为`docker-compose.yml`文件,并在该文件所在目录中运行`docker-compose up`命令即可启动ELK集群。
|
示例的logstash.conf
配置文件的内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| input { file { path => "/path/to/logfile.log" start_position => "beginning" sincedb_path => "/dev/null" } }
filter { # 添加你需要的过滤规则 # 例如,可以使用grok插件进行日志解析、使用date插件解析时间戳等 # 过滤规则根据具体日志格式和需求进行配置 }
output { elasticsearch { hosts => ["elasticsearch:9200"] index => "logs" } }
|
Elasicsearch
- elasicsearch是开源的,实时分布式的搜索与分析引擎;
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
| # 创建数据目录 mkdir -p elasticsearch/data && cd elasticsearch/ chown 1001.1001 -R data
# 添加compose文件 cat > docker-compose.yaml << eof version: '3' services: elasticsearch: image: bitnami/elasticsearch:8.6.2 container_name: es01 hostname: es01 restart: always environment: - BITNAMI_DEBUG=false - ELASTICSEARCH_HEAP_SIZE=2048m - ELASTICSEARCH_NODE_NAME=es01 - ELASTICSEARCH_BIND_ADDRESS=0.0.0.0 - ELASTICSEARCH_PORT_NUMBER=9200 ports: - 9200:9200 volumes: - ./data:/bitnami/elasticsearch/data - /etc/localtime:/etc/localtime:ro ulimits: memlock: soft: -1 hard: -1 nofile: soft: 655360 hard: 655360 eof
# 启动服务 docker compose up -d
|
Mongodb
- mongodb 是开源的跨平台 NoSQL 数据库;
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
| # 创建目录文件 mkdir -p mongodb/data && cd mongodb/ chown 1001.1001 -R data
# 添加compose文件 cat > docker-compose.yml <<eof version: '3.9' services: mongodb: image: bitnami/mongodb:6.0.4 container_name: mongo hostname: mongo ports: - 27017:27017 volumes: - /etc/localtime:/etc/localtime:ro - ./data:/bitnami/mongodb environment: - TZ=Asia/Shanghai # 设置管理员root密码 - MONGODB_ROOT_PASSWORD=password123 eof
# 启动服务 docker compose up -d
|
MySQL
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
| # 创建目录文件 mkdir -p mysql/{data,conf} && cd mysql/
# 添加配置文件 cat > conf/my_custom.cnf <<eof [mysqld] lower_case_table_names = 1 character-set-server = utf8 collation-server = utf8_general_ci default_storage_engine = InnoDB max_connections=1000 expire_logs_days=20 sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO ,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' eof
# 授权数据目录(这里的权限是和镜像里面的uid和gid一致即可) chown 1001.1001 -R data
# 添加compose文件 cat > docker-compose.yml <<eof version: '3.9' services: mysql: image: docker.io/bitnami/mysql:5.7.37 container_name: mysql hostname: mysql ports: - 3306:3306 volumes: - /etc/localtime:/etc/localtime:ro - ./conf/my_custom.cnf:/opt/bitnami/mysql/conf/my_custom.cnf - ./data:/bitnami/mysql/data environment: - TZ=Asia/Shanghai - MYSQL_ROOT_PASSWORD=admin1234 # 这里指定root账号密码 eof
# 启动服务 docker compose up -d
|
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
| # 创建目录文件 mkdir -p mysql/conf && cd mysql/
# 准备配置文件 cat > conf/my.cnf <<eof [client] default-character-set = utf8
[mysqld] character-set-server = utf8 collation-server = utf8_bin default_storage_engine = InnoDB port = 3306 datadir = /var/lib/mysql
max_connections=1000
expire_logs_days = 15 relay_log_purge = 1 slow_query_log_file = /var/log/mysql/slow.log log-error = /var/log/mysql/error.log log-bin = /var/log/mysql/mysqld-bin # mysql8之后因为mysql8采用了新的加密方式mysql_native_password # 这里修改为mysql_native_password,使用mysql5.7的加密方式 default_authentication_plugin = mysql_native_password
socket = /tmp/mysql.sock lower_case_table_names = 1 sql_mode = NO_ENGINE_SUBSTITUTION
default-time_zone='+8:00' eof
# 添加compose文件 cat > docker-compose.yml <<eof version: '3.9' services: mysql: image: docker.io/library/mysql:8.0.30 container_name: mysql hostname: mysql restart: always ports: - 3306:3306 volumes: - /etc/localtime:/etc/localtime:ro - ./data:/var/lib/mysql - ./conf/my.cnf:/etc/my.cnf environment: - MYSQL_ROOT_PASSWORD=admin1234 - TZ=Asia/Shanghai eof
# 启动服务 docker compose up -d
|
Redis
- 这里redis的密码选择使用redis.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 40 41 42 43 44 45 46
| # 创建数据目录 mkdir -p redis/{data,logs,conf} && cd redis/ chown 999.999 -R ./
# 添加配置文件 cat > conf/redis.conf <<eof tcp-keepalive 300 timeout 0 loglevel warning requirepass redis1234 maxclients 5000 maxmemory-policy allkeys-lru logfile "/logs/redis.log" port 6379 bind 0.0.0.0 pidfile /logs/reids.pid daemonize no appendonly yes auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb dir /bitnami/redis/data eof
# 添加compose文件 cat > docker-compose.yml <<eof version: '3.9' services: redis: image: docker.io/library/redis:6.0.16 container_name: redis hostname: redis environment: - TZ=Asia/Shanghai - ALLOW_EMPTY_PASSWORD=no # 不允许空密码 - REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL # 禁用执行的命令 ports: - 6379:6379 command: redis-server /opt/bitnami/redis/mounted-etc/redis.conf volumes: - ./conf/redis.conf:/opt/bitnami/redis/mounted-etc/redis.conf - ./data:/bitnami/redis/data - ./logs:/logs eof
# 启动服务 docker compose up -d
|
Rabbitmq
- Rabbitmq是目前非常热门的一款消息中间件,许多企业都在大量使用。
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
| # 创建数据目录 mkdir -p rabbitmq && cd rabbitmq/
# 添加compose文件 cat > docker-compose.yml <<eof version: '3.9' services: rabbitmq: image: docker.io/library/rabbitmq:3.11.2-management-alpine container_name: rabbitmq restart: always hostname: rabbitmq ports: - 15672:15672 - 5672:5672 - 25672:25672 - 4369:4369 volumes: - ./data:/var/lib/rabbitmq environment: - RABBITMQ_DEFAULT_USER=admin - RABBITMQ_DEFAULT_PASS=admin eof
# 启动服务 docker compose up -d
|
Minio
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
| # 创建数据目录 mkdir -p minio/data && cd minio/ chown 1001.1001 -R data
# 添加compose文件 cat > docker-compose.yml <<eof version: '3.9' services: minio: image: docker.io/bitnami/minio:2023.2.27 container_name: minio hostname: minio restart: always ports: - 9000:9000 - 9001:9001 volumes: - /etc/localtime:/etc/localtime:ro - ./data:/data environment: - MINIO_ROOT_USER=admin # 账号名长度必须大于等于5位 - MINIO_ROOT_PASSWORD=admin^12345 # 注意密码长度必须大于等于8位 - TZ=Asia/Shangha eof
# 启动服务 docker compose up -d
|
Jenkins
- Jenkins 基于Java开发的一种持续集成工具;
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
| # 创建数据目录 mkdir -p jenkins/data && cd jenkins/ chown 1000.1000 -R data
# 添加compose文件 cat > docker-compose.yml <<eof version: '3.9' services: jenkins-master: image: docker.io/jenkins/jenkins:2.377-jdk17 container_name: jenkins hostname: jenkins restart: always privileged: true network_mode: 'host' volumes: - /etc/localtime:/etc/localtime:ro - ./data:/var/jenkins_home environment: - TZ=Asia/Shanghai - JAVA_OPTS='-Djava.util.logging.config.file=/var/jenkins_home/log.properties' eof
# 启动服务 docker compose up -d
|
Nexus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| # 创建数据目录 mkdir -p nexus/data/nexus-data && cd nexus/ chown 200.200 -R ./data/nexus-data
# 添加compose文件 cat > docker-compose.yml <<eof version: '3.9' services: nexus: container_name: nexys image: sonatype/nexus3:3.37.0 hostname: nexus restart: always ports: - 8081:8081 volumes: - /etc/localtime:/etc/localtime:ro - ./data/nexus-data:/nexus-data environment: - TZ=Asia/Shanghai eof
# 启动服务 docker compose up -d
|
Sonarqube
- Sonarqube 是一个开源的代码分析平台, 用来持续分析和评测项目源代码的质量 ;
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
| # 创建数据目录 mkdir -p sonarqube && cd sonarqube/ mkdir -p sonar/{extensions,logs,data,conf} mkdir postgresql chown 1001.1001 -R postgresql
# 调整内核参数 echo"vm.max_map_count=655300" >> /etc/sysctl.conf sysctl -p
# 添加compose文件 cat > docker-compose.yml <<eof version: '3.9' networks: sonar: driver: bridge services: postgresql: image: bitnami/postgresql:15.1.0 container_name: postgresql hostname: postgresql volumes: - /etc/localtime:/etc/localtime:ro - ./postgresql:/bitnami/postgresql ports: - 15432:5432 environment: - TZ=Asia/Shanghai - POSTGRESQL_USERNAME=sonar - POSTGRESQL_PASSWORD=sonar - POSTGRESQL_DATABASE=sonar networks: - sonar sonar: image: docker.io/library/sonarqube:8.9.7-community container_name: sonar hostname: sonar volumes: - /etc/localtime:/etc/localtime:ro - ./sonar/extensions:/opt/sonarqube/extensions - ./sonar/logs:/opt/sonarqube/logs - ./sonar/data:/opt/sonarqube/data - ./sonar/conf:/opt/sonarqube/conf ports: - 9000:9000 environment: - TZ=Asia/Shanghai - SONARQUBE_JDBC_URL=jdbc:postgresql://postgresql:5432/sonar - SONARQUBE_JDBC_USERNAME=sonar - SONARQUBE_JDBC_PASSWORD=sonar networks: - sonar ulimits: memlock: soft: -1 hard: -1 nofile: soft: 165530 hard: 165530 eof
# 启动服务 docker compose up -d
|
Allure
- allure是开源测试报告框架,支持pytest单元测试框架 ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| # 创建数据目录 mkdir -p allure/data && cd allure/
# 添加compose文件 cat > docker-compose.yml <<eof version: '3' services: allure: image: docker.io/frankescobar/allure-docker-service:2.21.0-amd64 environment: TZ: Asia/Shanghai CHECK_RESULTS_EVERY_SECONDS: 1 KEEP_HISTORY: 1 ports: - 5050:5050 volumes: - ./data/results:/app/allure-results - /etc/localtime:/etc/localtime:ro eof
# 启动服务 docker compose up -d
|
Kuboard
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
| # 创建数据目录 mkdir -p kuboard && cd kuboard/
# 添加compose文件 cat > docker-compose.yml <<eof version: '3.9' services: kuboard: image: eipwork/kuboard:v3.5.0.3-amd64 hostname: kuboard restart: unless-stopped privileged: true ports: - 80:80/TCP - 10081:10081/TCP - 10081:10081/UDP volumes: - /var/run/docker.sock:/var/run/docker.sock - /etc/localtime:/etc/localtime - ./data:/data environment: - TZ=Asia/Shanghai - KUBOARD_AGENT_SERVER_UDP_PORT="10081" - KUBOARD_AGENT_SERVER_TCP_PORT="10081" eof
# 启动服务(admin/Kuboard123) docker compose up -d
|
Grafana
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
| # 创建数据目录 mkdir -p grafana/data && cd grafana/ chown 472.472 -R data
# 添加compose文件 cat > docker-compose.yml <<eof version: "3.9" services: grafana: image: docker.io/grafana/grafana:9.1.0 container_name: grafana hostname: grafana restart: always volumes: - /etc/localtime:/etc/localtime:ro - ./data:/var/lib/grafana user: "472" environment: - TZ=Asia/Shanghai ports: - 3000:3000 eof
# 启动服务(admin/admin) docker compose up -d
|
Jumpserver
- jumpserver 是开源的,符合 4A 规范的堡垒机系统。
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
| # 创建数据目录 mkdir -p jumpserver && cd jumpserver/
# 添加compose文件 cat > docker-compose.yaml << eof version: '3.9' services: jumpserver: image: jumpserver/jms_all:v2.28.6 container_name: jumpserver restart: always hostname: jumpserver privileged: true network_mode: 'host' volumes: - /etc/localtime:/etc/localtime - ./data:/opt/jumpserver/data/ environment: - TZ=Asia/Shanghai - SECRET_KEY=ww6q5uW0yRrP9moRy8A0C7wgvanGIx3nNO4lPlZZbkolusbmOb # 密钥: cat /dev/urandom | tr -dc A-Za-z0-9 | head -c 50 - BOOTSTRAP_TOKEN=lIQmk67fhNMC4ede # token: cat /dev/urandom | tr -dc A-Za-z0-9 | head -c 16 # 数据库地址 - DB_HOST=10.0.0.118 - DB_PORT=3306 - DB_USER=jumpserver - DB_PASSWORD=jumpserver - DB_NAME=jumpserver # redis缓存 - REDIS_HOST=10.0.0.118 - REDIS_PORT=6379 - REDIS_PASSWORD=admin1234 cap_add: - SYS_PTRACE ulimits: memlock: soft: -1 hard: -1 nofile: soft: 655360 hard: 655360 eof
# 启动服务(admin/admin) docker compose up -d
|
GitLab
- Gitlab 是被广泛使用的基于git的开源代码管理平台 ;
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
| # 创建数据目录 mkdir -p gitlab/{conf,data,logs} && cd gitlab/
# 添加ssl证书(这里自建一个) mkdir conf/ssl && cd conf/ssl/ openssl genrsa -out server.key 2048 openssl genrsa -out private.key 2048 openssl req -new -key private.key -out cert_req.csr -subj "/C=CN/ST=TEST/L=TEST/O=test/OU=main/CN=*.demo.com/emailAddress=root@localhost.com" openssl x509 -req -days 365 -in cert_req.csr -signkey private.key -out server_cert.crt
# 添加compose文件 cd ../../ cat > docker-compose.yaml << eof version: '3.9' services: gitlab: image: 'gitlab/gitlab-ce:13.5.2-ce.0' container_name: 'gitlab' restart: always hostname: 'gitlab' environment: TZ: 'Asia/Shanghai' GITLAB_OMNIBUS_CONFIG: | external_url 'https://gitlab.demo.com'# 使用https访问 nginx['enable'] = true nginx['client_max_body_size'] = '512m' nginx['redirect_http_to_https'] = true nginx['ssl_certificate'] = "/etc/ssl/server_cert.crt" nginx['ssl_certificate_key'] = "/etc/ssl/private.key" nginx['ssl_protocols'] = "TLSv1.1 TLSv1.2" nginx['ssl_session_cache'] = "builtin:1000 shared:SSL:10m" nginx['listen_addresses'] = ["0.0.0.0"] nginx['http2_enabled'] = true prometheus_monitoring['enable'] = false ports: - 443:443 - 80:80 volumes: - /etc/localtime:/etc/localtime:ro - ./conf:/etc/gitlab - ./data:/var/opt/gitlab - ./logs:/var/log/gitlab - ./conf/ssl:/etc/ssl eof
# 启动服务 docker compose up -d
|
SQL Server
- 如果不想在windows下安装sql server数据库,可以通过如下方式搭建;
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
| # 创建数据目录 mkdir -p sqlserver/data/mssql && cd sqlserver/ chmod 777 -R data/mssql
# 添加compose文件 cat > docker-compose.yaml << eof version: '3.9' services: sqlserver: image: mcr.microsoft.com/mssql/server:2019-latest container_name: sqlserver hostname: sqlserver restart: always ports: - 1433:1433 volumes: - /etc/localtime:/etc/localtime:ro - ./data/mssql:/var/opt/mssql environment: - TZ=Asia/Shanghai - ACCEPT_EULA=Y - MSSQL_SA_PASSWORD=Admin_123456 # 管理员sa的密码 eof
# 启动服务 docker compose up -d
|