1 安装Compose
从 Github 上下载它的二进制包来使用
最新发行的版本地址:https://github.com/docker/compose/releases
# 下载
[root@localhost compose]# curl -L https://github.com/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 896 0 --:--:-- --:--:-- --:--:-- 896
100 16.2M 100 16.2M 0 0 1735k 0 0:00:09 0:00:09 --:--:-- 2366k
# 可执行权限
[root@localhost compose]# chmod +x /usr/local/bin/docker-compose
# 查看安装是否成功
[root@localhost compose]# docker-compose -v
docker-compose version 1.25.1, build a82fef07
2 使用
将jar包上传到该目录下
[root@localhost compose]# vi Dockerfile
FROM openjdk:8
MAINTAINER gp6 <gp6@test.com>
# VOLUME 指定了临时文件目录为/tmp。
# 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为docker-boot.jar
ADD SpringBoot-1.0-SNAPSHOT.jar docker-boot.jar
# 运行jar包
RUN bash -c 'touch /docker-boot.jar'
EXPOSE 8081
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/docker-boot.jar"]
# 在 jar 包所在路径创建文件 docker-compose.yml
[root@localhost compose]# vi docker-compose.yml
# 表示该 Docker-Compose 文件使用的是 Version 3 file
version: '3'
services:
# 指定服务名称
docker-compose-test:
# 指定 Dockerfile 所在路径
build: .
# 指定端口映射
ports:
- "9000:8080"
# 启动并运行容器
[root@localhost compose]# docker-compose up -d
Creating network "compose_default" with the default driver
Building docker-demo
Step 1/7 : FROM openjdk:8
---> f8146facf376
......
docker-demo_1 | 2020-01-09 02:29:28.430 INFO 1 --- [ main] com.gp6.HelloApplication : Started HelloApplication in 6.277 seconds (JVM running for 7.637)
# 查看容器
[root@localhost compose]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f091b46f3f85 compose_docker-compose-test "java -Djava.securit…" 23 seconds ago Up 21 seconds 8081/tcp, 0.0.0.0:9000->8080/tcp compose_docker-compose-test_1
# 测试
[root@localhost compose]# curl 127.0.0.1:9000/hello
hello world!
# 列出所有运行容器
[root@localhost compose]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------------------
compose_docker-compose-test_1 java -Djava.security.egd=f ... Up 0.0.0.0:9000->8080/tcp, 8081/tcp
# 查看服务日志输出
[root@localhost compose]# docker-compose logs
Attaching to compose_docker-compose-test_1
docker-demo_1 |
docker-demo_1 | . ____ _ __ _ _
docker-demo_1 | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
docker-demo_1 | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
docker-demo_1 | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
docker-demo_1 | ' |____| .__|_| |_|_| |_\__, | / / / /
docker-demo_1 | =========|_|==============|___/=/_/_/_/
......
docker-demo_1 | 2020-01-09 02:34:09.979 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 50 ms
网友评论