1. 安装 docker-compose
方法一(速度稍微快一些)
sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
方法二
curl -SL https://github.com/docker/compose/releases/download/v2.6.0/docker-compose-linux-x86_64 -O docker-compose
2. 命令与参数:command
使用 command
参数,例如下列的配置,传入内部的命令会是:entrypoint -l 8080 -u 172.17.0.1 -p 30010
version: '3'
services:
extract-frame:
image: triton_warps:warps-v1.1.0.2
container_name: triton_warps_http
working_dir: /
command: ["-l", "8080", "-u", "172.17.0.1", "-p", "30010"]
ports:
- "30018:8080"
privileged: true
user: root
network_mode: bridge
2. 指定容器使用的网关
每次使用 docker-compose.yml
启动容器的时候,都会创建一个临时的 network
,可以通过 network_mode
指定网桥。
version: '3'
services:
hello-world:
image: helloworld:1.0
container_name: hello-world_0
network_mode: bridge
3. 删除临时 volumes
删除docker-compose 创建的命名 volume:
docker-compose down -v
如果在 docker-compose.yml 文件中配置了单独的 volumes
,那么就可以使用这个命令删除。
例如下面的配置中,就有这么一个 volumes
配置,不要都不行,服务会跑不起来:
version: "3"
services:
db:
image: postgres:14
environment:
POSTGRES_DB: wiki
POSTGRES_PASSWORD: wikijsrocks
POSTGRES_USER: wikijs
logging:
driver: "none"
restart: unless-stopped
volumes:
- db-data:/var/lib/postgresql/data
wiki:
image: requarks/wiki:2
depends_on:
- db
environment:
DB_TYPE: postgres
DB_HOST: db
DB_PORT: 5432
DB_USER: wikijs
DB_PASS: wikijsrocks
DB_NAME: wiki
restart: unless-stopped
ports:
- "32180:3000"
volumes:
db-data:
帮助信息:
Usage: down [options]
Options:
--rmi type Remove images. Type must be one of:
'all': Remove all images used by any service.
'local': Remove only images that don't have a
custom tag set by the `image` field.
-v, --volumes Remove named volumes declared in the `volumes`
section of the Compose file and anonymous volumes
attached to containers.
--remove-orphans Remove containers for services not defined in the
Compose file
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
(default: 10)
4. 限制日志文件大小
参考:
https://docs.docker.com/config/containers/logging/configure/#configure-the-logging-driver-for-a-container https://stackoverflow.com/questions/39078715/specify-max-log-json-file-size-in-docker-compose
在docker-compose.yml
中限制日志大小
version: '3'
services:
service_name:
image: image_name
logging:
driver: "json-file"
options:
max-size: "512m"
max-file: "2"
限制 docker 日志缓存的大小为 512M,最多也2个日志文件。默认只有一个文件,当日志文件达到 512M后,会从0开始重新日入数据。
5. 修改 MTU
参考:https://github.com/containerbuildsystem/osbs-box/issues/57
有的虚拟机的 MTU 是 1450,而 docker 默认的网卡是 1500,所以需要修改 docker 的 MTU 为 1450
networks:
default:
driver: bridge
driver_opts:
com.docker.network.driver.mtu: 1450
6. build
执行 docker-compose build
的时候,自动把build 之后的镜像命名为 image
字段定义的名字。
version: "3.9"
services:
dog_zoo:
restart: unless-stopped
env_file:
- .env
image: dog_zoo
build:
context: ./
dockerfile: ./Dockerfile
network: host
container_name: dog_zoo
7. entrypoint
version: "3.9"
services:
dog_zoo:
restart: unless-stopped
entrypoint: /entrypoint.sh
8. 限制 CPU 数量
version: '3'
services:
deploy:
replicas: 1
resources:
reservations:
cpus: '5' # 预留2个
limits:
cpus: '5' # 最多5个
限制CPU和内存 来源:https://javahowtos.com/guides/124-docker/429-limit-container-memory-and-cpu-usage-in-docker-compose.html
services:
service:
image: nginx
deploy:
resources:
limits:
cpus: 0.50
memory: 1024M
reservations:
cpus: 0.25
memory: 256M
9. 指定GPU
version: '3'
services:
deploy:
replicas: 1
resources:
reservations:
devices:
- driver: nvidia
device_ids: ['1']
capabilities: [gpu]