文章目录
1. 构建和删除
1.1 up
-
作用: 构建并启动容器
-
语法
docker-compose -f xxx.yaml up [ -d ]
说明:
默认文件名为docker-compose.yml
时 ,-f
可以省略。-d
是后台运行。
- 示例
[root@test mysql]# docker-compose up -d
Creating network "mysql_default" with the default driver
Creating liubei_mysql ... done
说明:
创建了一个网络:mysql_default
创建了一个容器:liubei_mysql
1.2 down
-
作用: 停止并删除容器、网络、卷、镜像。
-
语法:
docker-compose down [options]
-
示例
[root@test mysql]# docker-compose down
Stopping liubei_mysql ... done
Removing liubei_mysql ... done
Removing network mysql_default
说明:
停止了容器,移除了容器,移除了网络。
如果这个网络有容器在用,移除网络这里会报一个警告
1.3 rm
-
作用: 删除指定服务的容器(停止状态的)
-
语法: docker-compose rm [options] [SERVICE…]
【选项】
–f, –force,强制直接删除,包括非停止状态的容器
-v,删除容器所挂载的数据卷
- 示例
[root@test mysql]# docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------
liubei_mysql docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp
[root@test mysql]# docker-compose rm
No stopped containers
[root@test mysql]# docker-compose stop
Stopping liubei_mysql ... done
[root@test mysql]# docker-compose rm
Going to remove liubei_mysql
Are you sure? [yN] y
Removing liubei_mysql ... done
[root@test mysql]# docker-compose ps
Name Command State Ports
------------------------------
1.4 run
-
作用: 启动一个服务,并在一个服务上执行一个命令
-
语法:
docker-compose run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]
- 示例1
说明:
如果没有这个服务,会直接创建容器和网络。
如果启动了,则会直接执性后边点命令
执行完之后,容器会被删除,但默认网络不会删除。
服务名是
[root@n9e-client-01 mysql]# docker-compose ps
Name Command State Ports
------------------------------
[root@n9e-client-01 mysql]# docker-compose run mysql "pwd"
Creating network "mysql_default" with the default driver
/
[root@n9e-client-01 mysql]# docker-compose run mysql "pwd"
/
[root@n9e-client-01 mysql]# docker-compose ps
Name Command State Ports
------------------------------
2. 启动/停止/暂停
2.1 start
-
**作用:**启动指定服务已存在的容器
-
语法:
docker-compose stop [SERVICE...]
-
示例
[root@test mysql]# docker-compose ps
Name Command State Ports
-----------------------------------------------------------
liubei_mysql docker-entrypoint.sh mysqld Exit 0
[root@test mysql]# docker-compose start
Starting mysql ... done
[root@test mysql]# docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------
liubei_mysql docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp
如果服务不存在,则报错如下:
[root@test mysql]# docker-compose down
Stopping liubei_mysql ... done
Removing liubei_mysql ... done
Removing network mysql_default
[root@test mysql]# docker-compose ps
Name Command State Ports
------------------------------
[root@test mysql]# docker-compose start
Starting mysql ... failed
ERROR: No containers to start
2.2 stop
-
作用: 停止已运行的服务的容器
-
语法:
docker-compose stop [SERVICE...]
-
示例
[root@test mysql]# docker-compose stop
Stopping liubei_mysql ... done
[root@test mysql]# docker-compose ps
Name Command State Ports
-----------------------------------------------------------
liubei_mysql docker-entrypoint.sh mysqld Exit 0
.2.3 restart
-
作用: 重起容器
-
语法:
docker-compose restart [options] [SERVICE...]
-
示例:
[root@test mysql]# docker-compose restart mysql
Restarting liubei_mysql ... done
2.4 pause
1)作用:暂停已运行的服务的容器
2)语法: docker-compose pause [SERVICE…]
[root@test mysql]# docker-compose pause
Pausing liubei_mysql ... done
[root@test mysql]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------
liubei_mysql docker-entrypoint.sh mysqld Paused 0.0.0.0:3306->3306/tcp, 33060/tcp
从状态可以看到,pause是暂停,和stop点结束时不同的。
2.5 kill
- 作用: 通过发送 SIGKILL 信号来停止指定服务的容器
-
语法:
docker-compose kill [options] [SERVICE...]
- 示例:
[root@test]# docker-compose kill
Killing liubei_mysql ... done
[root@test]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------
liubei_mysql docker-entrypoint.sh mysqld Exit 137
和stop点区别:从退出状态可知,stop是正常结束服务,而kill是杀死,非特殊情况不建议kill。
3 查看和日志
3.1 ps
- 作用: 列出所有容器
-
语法:
docker-compose ps [options] [SERVICE...]
- 示例
[root@test crust-framework]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------
auth sh -c java -server $JAVA_O ... Up 0.0.0.0:10001->10001/tcp
gateway sh -c java -server $JAVA_O ... Up 0.0.0.0:10000->10000/tcp
user sh -c java -server $JAVA_O ... Up 0.0.0.0:10002->10002/tcp
[root@test crust-framework]# docker-compose ps auth
Name Command State Ports
------------------------------------------------------------------------
auth sh -c java -server $JAVA_O ... Up 0.0.0.0:10001->10001/tcp
3.2 logs
-
作用: 查看服务日志输出(容器前台输出的日志)
-
语法:
docker-compose logs [options] [SERVICE...]
[root@test mysql]# docker-compose logs
3.3 port
-
作用: 打印绑定的公共端口
-
示例
打印mysql 3306端口绑定的端口
[root@test mysql]# docker-compose port mysql 3306
0.0.0.0:3306
4 其它
4.1 pull
-
作用: 下载服务镜像
-
示例:
下载docker-compose.yml中所需的镜像
[root@test mysql]# docker-compose pull
Pulling mysql ... done
4.2 scale
- 作用: 设置指定服务运行容器的个数,以 service=num 形式指定
- 示例:
[root@n9e-client-01 mysql]# docker-compose up -d
Creating network "mysql_default" with the default driver
Creating mysql_mysql_1 ... done
[root@n9e-client-01 mysql]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------
mysql_mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp
[root@n9e-client-01 mysql]# docker-compose scale mysql=2
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting mysql_mysql_1 ... done
Creating mysql_mysql_2 ... done
[root@n9e-client-01 mysql]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------
mysql_mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp
mysql_mysql_2 docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp
说明:
不能给容器起名,否则冲突。
不能映射端口,否则冲突……
4.3 build
- 作用: 构建或者重新构建服务