Mac环境初学docker-compose常用的命令练手项目

在mac环境安装docker-compose,前提你已经有docker的环境和python3的环境

安装docker-cpmpose

python3 -m pip install docker-compose

查看版本

docker-compose -v

输出如:docker-compose version 1.29.2, build unknown

Docker-Compose练手项目

千年码农/SpringBoot2+Vue后台管理系统+mongodb4.0


docker-compose命令需要本地文件夹目录有docker-compose.xml采用yaml编写,如果需要本地的dockfile打包运行那么必须创建dockerfile文件

测试目录中创建一个名为 docker-compose.yml 的文件,比如依赖mongodb数据,一个java的接口镜像和一个前后端分离的vue项目 nginx的镜像


version: '2' # 表示该 Docker-Compose 文件使用的是 Version 2 file

services:

boot-mongo-admin: # 指定服务名称

build: ./java/ # 指定 Dockerfile 所在路径

# image: shuogesha/boot-mongo-admin

ports: # 指定端口映射

- "8080:8080"

links:

- mymongo

boot-mongo-admin-nginx: # 指定服务名称

build: ./vue-admin/ # 指定 Dockerfile 所在路径

# image: shuogesha/boot-mongo-admin-nginx

ports: # 指定端口映射

- "18080:80"

links:

- boot-mongo-admin

mymongo:

image: mongo:4.0

ports: # 指定端口映射

- "27017:27017"

volumes:

- ./data:/data/db


打包编译命令

docker-compose build

马上运行

docker-compose up

后台运行,后台执行该服务可以加上 -d 参数

docker-compose up -d

yml 配置指令参考

version

指定本 yml 依从的 compose 哪个版本制定的。

build

指定为构建镜像上下文路径

帮助命令查看更多

docker-compose --help


Usage:

docker-compose [-f <arg>...] [--profile <name>...] [options] [--] [COMMAND] [ARGS...]

docker-compose -h|--help

Options:

-f, --file FILE Specify an alternate compose file

(default: docker-compose.yml)

-p, --project-name NAME Specify an alternate project name

(default: directory name)

--profile NAME Specify a profile to enable

-c, --context NAME Specify a context name

--verbose Show more output

--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)

--ansi (never|always|auto) Control when to print ANSI control characters

--no-ansi Do not print ANSI control characters (DEPRECATED)

-v, --version Print version and exit

-H, --host HOST Daemon socket to connect to

--tls Use TLS; implied by --tlsverify

--tlscacert CA_PATH Trust certs signed only by this CA

--tlscert CLIENT_CERT_PATH Path to TLS certificate file

--tlskey TLS_KEY_PATH Path to TLS key file

--tlsverify Use TLS and verify the remote

--skip-hostname-check Don't check the daemon's hostname against the

name specified in the client certificate

--project-directory PATH Specify an alternate working directory

(default: the path of the Compose file)

--compatibility If set, Compose will attempt to convert keys

in v3 files to their non-Swarm equivalent (DEPRECATED)

--env-file PATH Specify an alternate environment file

Commands:

build Build or rebuild services

config Validate and view the Compose file

create Create services

down Stop and remove resources

events Receive real time events from containers

exec Execute a command in a running container

help Get help on a command

images List images

kill Kill containers

logs View output from containers

pause Pause services

port Print the public port for a port binding

ps List containers

pull Pull service images

push Push service images

restart Restart services

rm Remove stopped containers

run Run a one-off command

scale Set number of containers for a service

start Start services

stop Stop services

top Display the running processes

unpause Unpause services

up Create and start containers

version Show version information and quit


docker-compose up -d boot-mongo-admin 构建建启动boot-mongo-admin容器

docker-compose exec boot-mongo-admin bash 登录到boot-mongo-admin容器中

docker-compose down 删除所有boot-mongo-admin容器,镜像

docker-compose ps 显示所有容器

docker-compose restart boot-mongo-admin 重新启动boot-mongo-admin容器

docker-compose run --no-deps --rm php-fpm php -v 在php-fpm中不启动关联容器,并容器执行php -v 执行完成后删除容器

docker-compose build boot-mongo-admin 构建镜像

docker-compose build --no-cache boot-mongo-admin 不带缓存的构建

docker-compose logs boot-mongo-admin 查看boot-mongo-admin的日志

docker-compose logs -f boot-mongo-admin 查看boot-mongo-admin的实时日志

docker-compose config -q 验证(docker-compose.yml)文件配置,当配置正确时,不输出任何内容,当文件配置错误,输出错误信息。

docker-compose events --json boot-mongo-admin 以json的形式输出boot-mongo-admin的docker日志

docker-compose pause boot-mongo-admin 暂停boot-mongo-admin容器

docker-compose unpause boot-mongo-admin 恢复ningx容器

docker-compose rm boot-mongo-admin 删除容器(删除前必须关闭容器)

docker-compose stop boot-mongo-admin 停止boot-mongo-admin容器

docker-compose start boot-mongo-admin 启动boot-mongo-admin容器