文章目录
DockerCompese能够做什么
官方介绍
通过YAML file配置文件用来定义运行多个容器
介绍
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.
Compose是一个用于定义和运行多容器Docker应用程序的工具。使用Compose,您可以使用YAML文件配置应用程序的服务。然后,使用一个命令,从配置中创建并启动所有服务。要了解更多关于Compose的所有功能,请参阅功能列表。
Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.
在所有环境中组合工作:生产、登台、开发、测试以及CI工作流。您可以在常用用例中了解关于每个用例的更多信息。
运行的三个步骤
Using Compose is basically a three-step process:
- Define your app’s environment with a so it can be reproduced anywhere.
Dockerfile
- Define the services that make up your app in so they can be run together in an isolated environment.
docker-compose.yml
- Run and the Docker compose command starts and runs your entire app. You can alternatively run using the docker-compose binary.
docker compose up``docker-compose up
使用Compose基本上是一个三步过程:
-
用Dockerfile定义你的应用的环境,这样它就可以被复制到任何地方。
-
通过docker-compose.yml定义组成你的应用的服务,这样它们就可以在一个独立的环境中一起运行。
-
运行Docker compose 命令启动并运行整个应用程序。你也可以使用Docker -compose二进制文件运行。通过’ docker-compose up '启动
yml文件示例
A looks like this:docker-compose.yml
version: "3.9" # optional since v1.27.0
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
DockerFile build run可以手动开启一个容器
如果有100个容器就很麻烦
所以DockerCompese用来管理定义多个容器
Compose 是Docker官方的开源项目,需要单独安装
Compose概念
安装compose
#官方下载
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
#授权
chmod +x /usr/local/bin/docker-compose
# 加速下载
curl -L https://get.daocloud.io/docker/compose/releases/download/1.26.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
#授权
chmod +x /usr/local/bin/docker-compose
安装成功
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7w79X4TH-1641034217001)(C:\Users\精神小伙\AppData\Roaming\Typora\typora-user-images\image-20211231131747632.png)]
测试一个Redis的计数器
- 应用app.py
- Dockerfile 将应用打包为镜像
- Docker-compose yaml文件 定义整个服务,和需要的环境
- 启动compose dicker-compose up
步骤 1:设置
定义应用程序依赖项。
-
为项目创建目录:
$ mkdir composetest $ cd composetest
-
import time import redis from flask import Flask app = Flask(__name__) cache = redis.Redis(host='redis', port=6379) def get_hit_count(): retries = 5 while True: try: return cache.incr('hits') except redis.exceptions.ConnectionError as exc: if retries == 0: raise exc retries -= 1 time.sleep(0.5) @app.route('/') def hello(): count = get_hit_count() return 'Hello World! I have been seen {} times.\n'.format(count)
flask
redis
步骤 2:创建 Dockerfile
在此步骤中,您将编写一个构建 Docker 映像的 Docker 文件。该映像包含 Python 应用程序所需的所有依赖项,包括 Python 本身。
在项目目录中,创建一个名为以下内容并粘贴以下内容的文件:Dockerfile
# Syntax=docker/dockerfile:1
FROM python:3.7-alpine
workdir /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
copY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
copY . .
CMD ["flask", "run"]
这告诉 Docker:
- 从 Python 3.7 映像开始构建映像。
- 将工作目录设置为 。
/code
- 设置命令使用的环境变量。
flask
- 安装 gcc 和其他依赖项
- 复制并安装 Python 依赖项。
requirements.txt
- 向映像添加元数据以描述容器正在侦听端口 5000
- 将项目中的当前目录复制到映像中的 workdir。
.``.
- 将容器的缺省命令设置为 。
flask run
步骤 3:在撰写文件中定义服务
创建在项目目录中调用的文件并粘贴以下内容:docker-compose.yml
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
网络服务
该服务使用从 当前目录中生成的映像。然后,它将容器和主机绑定到公开的端口 。此示例服务使用 Flask Web 服务器的缺省端口。web``Dockerfile``5000``5000
Redis 服务
该服务使用从 Docker Hub 注册表中提取的公共Redis映像。redis
步骤 4:使用撰写构建和运行应用
- 从项目目录中,通过运行 启动应用程序。
docker-compose up
docker-compose up
[root@iZ70eyv5ttqkcsZ composetest]# docker-compose up
Starting composetest_web_1 ... done
Starting composetest_redis_1 ... done
Attaching to composetest_redis_1, composetest_web_1
redis_1 | 1:C 31 Dec 2021 06:24:25.704 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 31 Dec 2021 06:24:25.704 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 31 Dec 2021 06:24:25.704 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 31 Dec 2021 06:24:25.705 * monotonic clock: POSIX clock_gettime
redis_1 | 1:M 31 Dec 2021 06:24:25.719 * Running mode=standalone, port=6379.
redis_1 | 1:M 31 Dec 2021 06:24:25.719 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 31 Dec 2021 06:24:25.719 # Server initialized
redis_1 | 1:M 31 Dec 2021 06:24:25.719 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | 1:M 31 Dec 2021 06:24:25.732 * Loading RDB produced by version 6.2.6
redis_1 | 1:M 31 Dec 2021 06:24:25.732 * RDB age 64 seconds
redis_1 | 1:M 31 Dec 2021 06:24:25.732 * RDB memory usage when created 0.77 Mb
redis_1 | 1:M 31 Dec 2021 06:24:25.732 # Done loading RDB, keys loaded: 0, keys expired: 0.
redis_1 | 1:M 31 Dec 2021 06:24:25.732 * DB loaded from disk: 0.012 seconds
redis_1 | 1:M 31 Dec 2021 06:24:25.732 * Ready to accept connections
web_1 | * Serving Flask app 'app.py' (lazy loading)
web_1 | * Environment: production
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | Use a production Wsgi server instead.
web_1 | * Debug mode: off
web_1 | * Running on all addresses.
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | * Running on http://172.19.0.3:5000/ (Press CTRL+C to quit)
web_1 | 172.19.0.1 - - [31/Dec/2021 06:25:28] "GET / HTTP/1.1" 200 -
web_1 | 172.19.0.1 - - [31/Dec/2021 06:25:30] "GET / HTTP/1.1" 200 -
web_1 | 172.19.0.1 - - [31/Dec/2021 06:25:31] "GET / HTTP/1.1" 200 -
web_1 | 172.19.0.1 - - [31/Dec/2021 06:25:31] "GET / HTTP/1.1" 200 -
web_1 | 172.19.0.1 - - [31/Dec/2021 06:25:32] "GET / HTTP/1.1" 200 -
运行结果验证
[root@iZ70eyv5ttqkcsZ composetest]# curl localhost:5000
Hello World! I have been seen 1 times.
[root@iZ70eyv5ttqkcsZ composetest]# curl localhost:5000
Hello World! I have been seen 2 times.
[root@iZ70eyv5ttqkcsZ composetest]# curl localhost:5000
Hello World! I have been seen 3 times.
[root@iZ70eyv5ttqkcsZ composetest]# curl localhost:5000
Hello World! I have been seen 4 times.
[root@iZ70eyv5ttqkcsZ composetest]# curl localhost:5000
Hello World! I have been seen 5 times.
docker ps
[root@iZ70eyv5ttqkcsZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae49ff137c76 redis:alpine "docker-entrypoint..." 16 minutes ago Up 19 seconds 6379/tcp composetest_redis_1
96f13de97b4d composetest_web "flask run" 16 minutes ago Up 19 seconds 0.0.0.0:5000->5000/tcp composetest_web_1
docker network ls
[root@iZ70eyv5ttqkcsZ ~]# docker network ls
NETWORK ID NAME DRIVER ScopE
b8ea21e99b8a bridge bridge local
44dd29d47235 composetest_default bridge local
92d4ae51b461 host host local
ee8ffcd2dc27 mynet bridge local
fb8882d50635 none null local
c619a62c8c7b redis bridge local
[root@iZ70eyv5ttqkcsZ ~]# docker network inspect composetest_default
[
{
"Name": "composetest_default",
"Id": "44dd29d472355fa3528ec960a9f72e6b957549e8971e4c2403a7d8f235544540",
"Created": "2021-12-31T13:33:59.722342545+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"subnet": "172.19.0.0/16",
"Gateway": "172.19.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Containers": {
"96f13de97b4d107cfed410c4628b8f5106b8677bad0d550b1b21e05e427d155a": {
"Name": "composetest_web_1",
"EndpointID": "efe58cb37c53adfd63361851aebd51f751c59e0f8efe3aae590f317294f44397",
"MacAddress": "02:42:ac:13:00:03",
"IPv4Address": "172.19.0.3/16",
"IPv6Address": ""
},
"ae49ff137c767a414dbe30be88f41f70a21a4c58fd79d0628a59f448b5455326": {
"Name": "composetest_redis_1",
"EndpointID": "91472b74fed591aa7430236af50b8ebab8785d78b6ff3672f8f0a7606447bfb3",
"MacAddress": "02:42:ac:13:00:02",
"IPv4Address": "172.19.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "default",
"com.docker.compose.project": "composetest",
"com.docker.compose.version": "1.26.2"
}
}
]
报错
因为docker-compose.yml中的版本在写下的时候已经为3.9所以报错,可以将其改为3.3就可以正常运行了
version: "3.3" #之前为3.9
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
[root@iZ70eyv5ttqkcsZ composetest]# docker-compose up
ERROR: Version in "./docker-compose.yml" is unsupported. You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service deFinitions under the `services` key, or omit the `version` key and place your service deFinitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/
文章目录
自己定义一个类似于2.1中官方入门文档的redis登录计数器
创建一个springboot微服务项目
HelloController
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedistemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
StringRedistemplate redistemplate;
@GetMapping("/hello")
public String hello(){
Long views = redistemplate.opsForValue().increment("views");
return "hello,voews" + views;
}
}
application.properties 在本地运行不了,在服务器上进行redis的ip替换
server.port=8080
spring.redis.host=redis
Dockerfile 定义应用环境
FROM java:8
copY *.jar /app.jar #拷贝所有jar包到容器内
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yaml 定义组成应用的服,让它可以在一个独立的环境运行
version: '3.8'
services:
myapp:
build: .
image: myapp
depends_on: #依托于redis,redis会先于这个服务启动
- redis
ports:
- "8080:8080"
redis:
image: "library/redis:alpine" #指定镜像
编写完成后,进行打包工作,将文件全部放在同一目录下输入命令docker-compose up完成启动服务并部署.
成功进行启动
[root@iZ70eyv5ttqkcsZ weifuwuApp]# docker-compose up --build
Building myapp
Step 1/5 : FROM java:8
---> d23bdf5b1b1b
Step 2/5 : copY *.jar /app.jar
---> 5dc157d72b42
Removing intermediate container 7f46d50025c3
Step 3/5 : CMD --server.port=8080
---> Running in fb354ee24460
---> 94d8590875bd
Removing intermediate container fb354ee24460
Step 4/5 : EXPOSE 8080
---> Running in c4559d699ca8
---> deb879ec8fce
Removing intermediate container c4559d699ca8
Step 5/5 : ENTRYPOINT java -jar /app.jar
---> Running in ff8a60f57fb2
---> 09637f6055fc
Removing intermediate container ff8a60f57fb2
Successfully built 09637f6055fc
Creating weifuwuapp_redis_1 ... done
Creating weifuwuapp_myapp_1 ... done
Attaching to weifuwuapp_redis_1, weifuwuapp_myapp_1
redis_1 | 1:C 31 Dec 2021 16:23:38.766 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 31 Dec 2021 16:23:38.766 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 31 Dec 2021 16:23:38.766 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 31 Dec 2021 16:23:38.767 * monotonic clock: POSIX clock_gettime
redis_1 | 1:M 31 Dec 2021 16:23:38.776 * Running mode=standalone, port=6379.
redis_1 | 1:M 31 Dec 2021 16:23:38.776 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 31 Dec 2021 16:23:38.776 # Server initialized
redis_1 | 1:M 31 Dec 2021 16:23:38.776 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | 1:M 31 Dec 2021 16:23:38.776 * Ready to accept connections
myapp_1 |
myapp_1 | . ____ _ __ _ _
myapp_1 | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
myapp_1 | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
myapp_1 | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
myapp_1 | ' |____| .__|_| |_|_| |_\__, | / / / /
myapp_1 | =========|_|==============|___/=/_/_/_/
myapp_1 | :: Spring Boot :: (v2.6.2)
myapp_1 |
myapp_1 | 2021-12-31 16:23:44.447 INFO 1 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_111 on 143e4f05fbed with PID 1 (/app.jar started by root in /)
myapp_1 | 2021-12-31 16:23:44.450 INFO 1 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
测试结果
[root@iZ70eyv5ttqkcsZ ~]# curl localhost:8080/hello
hello,voews1[root@iZ70eyv5ttqkcsZ ~]# curl localhost:8080/hello
hello,voews2[root@iZ70eyv5ttqkcsZ ~]#
docker-compose up --build 启动项目的时候可以重新构建
yaml规则
# 3层
version: '' # 版本
services: # 服务
服务1: web
# 服务配置
images
build
network
......
服务2: redis
.....
服务3: MysqL
.....
# 其他配置 网络/卷 全局规则
volumes:
networks:
configs
配置个人博客wordpress
Quickstart: Compose and WordPress | Docker Documentation
-
Change into your project directory.
For example, if you named your directory
my_wordpress
:$ cd my_wordpress/
-
Create a
docker-compose.yml
file that starts yourwordpress
blog and a separateMysqL
instance with volume mounts for data persistence:version: "3.9" services: db: image: MysqL:5.7 volumes: - db_data:/var/lib/MysqL restart: always environment: MysqL_ROOT_PASSWORD: somewordpress MysqL_DATABASE: wordpress MysqL_USER: wordpress MysqL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest volumes: - wordpress_data:/var/www/html ports: - "8000:80" restart: always environment: wordpress_DB_HOST: db wordpress_DB_USER: wordpress wordpress_DB_PASSWORD: wordpress wordpress_DB_NAME: wordpress volumes: db_data: {} wordpress_data: {}
-
使用docker-compose up启动
之后就可以进入著名的5分钟访问了
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。