docker-compose简介
Compose是用于定义和运行多容器Docker应用程序的工具。通过Compose,您可以使用YAML文件来配置应用程序的服务。然后,使用一个命令,就可以从配置中创建并启动所有服务。
使用Compose基本上是一个三步过程:
1.使用定义您的应用环境,Dockerfile以便可以在任何地方复制。
2.定义组成应用程序的服务,docker-compose.yml 以便它们可以在隔离的环境中一起运行。
3. docker-compose up 启动并运行您的整个应用程序。
在linux下安装docker-compose
1.下载
#官方文档下载地址 有点慢
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
#国内镜
像地址下载
curl -L "https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m`" -o /usr/local/bin/docker-compose
2.文件授权
sudo chmod +x /usr/local/bin/docker-compose
3.检查是否安装成功 docker-compose version 安装成功会显示版本号
[root@localhost ~]# docker-compose version
docker-compose version 1.25.5, build 8a1c60f6
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019
体验docker-compose
在docker官网上面有一个体验案列可以试一下 https://docs.docker.com/compose/gettingstarted/
在这里我们自己编写一个服务上线
1.创建一个springboot项目 勾选web跟redis !
2.创建controller
package com.zh.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 DemoController {
@Autowired
StringRedisTemplate stringRedisTemplate;
@GetMapping("/test")
public String test(){
Long count = stringRedisTemplate.opsForValue().increment("count");
return "hello world! count : "+count;
}
}
3.properties文件
server.port=8080
#服务在docker同一个网络中可以通过域名访问
spring.redis.host=redis
4.编写Dockerfile文件
FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
5.编写docker-compose文件
version: '3.8'
services:
app:
build: .
image: app
depends_on:
- redis
ports:
- "8080:8080"
redis:
image: redis:4
6.将springboot项目打包,把jar包、DockerFile、docker-compose.yml文件上传到linux服务器同一个目录文件下 执行docker-compose up 命令,docker将会自动构建项目并发布
[root@localhost docker-compose-test]# docker-compose up
Pulling redis (redis:4)...
4: Pulling from library/redis
54fec2fa59d0: Pull complete
9c94e11103d9: Pull complete
04ab1bfc453f: Pull complete
7988789e1fb7: Pull complete
8ce1bab2086c: Pull complete
40e134f79af1: Pull complete
Digest: sha256:2e03fdd159f4a08d2165ca1c92adde438ae4e3e6b0f74322ce013a78ee81c88d
Status: Downloaded newer image for redis:4
Building app
Step 1/5 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
---> d23bdf5b1b1b
Step 2/5 : COPY *.jar /app.jar
---> 2d7ff2a040e8
Step 3/5 : CMD ["--server.port=8080"]
---> Running in 69704d37a0f6
Removing intermediate container 69704d37a0f6
---> 3664164f2437
Step 4/5 : EXPOSE 8080
---> Running in d1368c234806
Removing intermediate container d1368c234806
---> d885f3510e56
Step 5/5 : ENTRYPOINT ["java","-jar","app.jar"]
---> Running in 75ca8fc834f9
Removing intermediate container 75ca8fc834f9
---> 0675ee41b0b3
Successfully built 0675ee41b0b3
Successfully tagged app:latest
WARNING: Image for service app was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating docker-compose-test_redis_1 ... done
Creating docker-compose-test_app_1 ... done
Attaching to docker-compose-test_redis_1, docker-compose-test_app_1
redis_1 | 1:C 28 Jul 07:49:40.532 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 28 Jul 07:49:40.533 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 28 Jul 07:49:40.533 # 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 28 Jul 07:49:40.534 * Running mode=standalone, port=6379.
redis_1 | 1:M 28 Jul 07:49:40.534 # 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 28 Jul 07:49:40.534 # Server initialized
redis_1 | 1:M 28 Jul 07:49:40.535 # 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 28 Jul 07:49:40.535 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1 | 1:M 28 Jul 07:49:40.535 * Ready to accept connections
app_1 |
app_1 | . ____ _ __ _ _
app_1 | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
app_1 | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
app_1 | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
app_1 | ' |____| .__|_| |_|_| |_\__, | / / / /
app_1 | =========|_|==============|___/=/_/_/_/
app_1 | :: Spring Boot :: (v2.3.0.RELEASE)
app_1 |
app_1 | 2020-07-28 07:49:42.317 INFO 1 --- [ main] com.zh.demo.DemoApplication : Starting DemoApplication on 302fa5fb4ac2 with PID 1 (/app.jar started by root in /)
app_1 | 2020-07-28 07:49:42.323 INFO 1 --- [ main] com.zh.demo.DemoApplication : No active profile set, falling back to default profiles: default
app_1 | 2020-07-28 07:49:43.440 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
app_1 | 2020-07-28 07:49:43.446 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
app_1 | 2020-07-28 07:49:43.486 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 16ms. Found 0 Redis repository interfaces.
app_1 | 2020-07-28 07:49:44.480 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
app_1 | 2020-07-28 07:49:44.502 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
app_1 | 2020-07-28 07:49:44.502 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.35]
app_1 | 2020-07-28 07:49:44.637 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
app_1 | 2020-07-28 07:49:44.638 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2198 ms
app_1 | 2020-07-28 07:49:46.066 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
app_1 | 2020-07-28 07:49:46.622 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
app_1 | 2020-07-28 07:49:46.655 INFO 1 --- [ main] com.zh.demo.DemoApplication : Started DemoApplication in 5.083 seconds (JVM running for 5.854)
7.访问项目地址 http://ip:8080/test 访问成功


来源:oschina
链接:https://my.oschina.net/u/4290804/blog/4446004