当咱们经过了解 docker 简易入门 本篇文章后,想必此时咱们已经能够基于 nginx
镜像建立一个最简单的容器:启动一个最简单的 http 服务前端
$ docker run -d --name nginx -p 8888:80 nginx:alpine
$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
404e88f0d90c nginx:alpine "nginx -g 'daemon of…" 4 minutes ago Up 4 minutes 0.0.0.0:8888->80/tcp nginx
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
复制代码
其中有诸多参数命令nginx
-d
: 启动一个 daemon
进程--name
: 为容器指定名称-p host-port:container-port
: 宿主机与容器端口映射,方便容器对外提供服务nginx:alpine
: 基于该镜像建立容器这还只是一个简单的 nginx
的容器,若是有更多的容器那应该如何管理呢?git
使用 docker-compose
来编排应用github
使用 docker-compose
建立一个最简单的容器,建立 docker-compose.yaml
文件。它使用配置文件的方式代替之前传参数的方式启动容器web
version: '3'
services:
nginx:
image: nginx:alpine
container_name: nginx-service
restart: always
ports:
- "8888:80"
复制代码
使用 docker-compose up
启动容器,它会自动查找当前目录下的 docker-compose.yaml
文件做为配置文件docker
# 启动
$ docker-compose up
# 查看日志
$ docker-compose logs
# 中止
$ docker-compose stop
# 删除
$ docker-compose rm
复制代码
关于 compose
的全部的配置请参考官方文档 compose file,大部分配置与 dockerfile
配置相相似bash
配置文件管理三种资源,services
,networks
以及 volumes
,咱们能够结合 docker-compose
与 traefik
来管理应用。如如下配置文件将能够以域名 whiami.docker.localhost
来访问应用,详情可参考 traefik 简单介绍服务器
version: '3'
# 配置 service
services:
whoami:
image: containous/whoami
restart: always
labels:
- "traefik.http.routers.whoami.rule=Host(`whoami.docker.localhost`)"
# 配置 network
networks:
default:
external:
name: traefik_default
复制代码
指定镜像微信
image: nginx
复制代码
能够直接根据当前目录构建,而无需镜像app
version: "3"
services:
webapp:
build: .
复制代码
主机与容器的端口映射,可是在 trafik
代理下每每不须要指定
ports:
- "8080:80"
复制代码
用以筛选容器,在结合 traefik
或者 k8s
使用时,用以控制流量
labels:
com.example.description: "Accounting webapp"
com.example.department: "Finance"
com.example.label-with-empty-value: ""
labels:
- "com.example.description=Accounting webapp"
- "com.example.department=Finance"
- "com.example.label-with-empty-value"
复制代码
指定容器名称,可是指定后不可以横向扩展,每每不会用到
当使用 docker-compose
编排应用时,同时也能够选择 ctop
来管理容器。
我是山月,你能够添加我微信 shanyue94 或者关注公众号与我交流。另外能够扫码加入大前端技术交流群