docker-compose初试及命令基础

以一个简单的lnmp.yaml的配置文件进行讲解docker-compose命令的基础讲解,熟练掌握命令

[root@docker lnmp]# cat lnmp.yaml
version: '3'
services:
nginx:
image: nginx
ports:
- "80:80"
links:
- php:php
volumes:
- "/www:/usr/local/nginx/html"
php:
image: php
expose:
- "9000"
volumes:
- "/www:/usr/local/nginx/html"
db:
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: redhat
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress

上面配置文件的关于编写的参数暂时不进行讲解,这里只进行关于docker-compose的命令

[root@docker ~]# docker-compose --help
Define and run multi-container applications with Docker. Usage:
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help

看用法的定义就是利用docker定义和运行多个容器应用

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)

默认如果没有指定具体的compose配置文件,那么就docker-compose.yml,并且如果不指定项目名称,默认就是文件夹的name

up                 Create and start containers

现在利用command中的up进行创建并启动容器(多个容器,安装配置文件中,至于顺序现在不讲解)

[root@docker lnmp]# docker-compose -f lnmp.yaml up
Creating lnmp_php_1 ...
Creating lnmp_db_1 ...
Creating lnmp_db_1
Creating lnmp_php_1 ... done
Creating lnmp_nginx_1 ...
Creating lnmp_db_1 ... done

后面的一大堆输出,这里不进行全部显示了,创建并启动好了容器后,如何查看运行的状态呢?

ps                 List containers
[root@docker lnmp]# docker-compose ps
Name Command State Ports
------------------------------

为什么这里没有显示呢?因为配置文件名称不是docker-compose.yml,所以在执行一系列命令时需要加上-f lnmp.yaml

[root@docker lnmp]# docker-compose -f lnmp.yaml ps
Name Command State Ports
------------------------------------------------------------------------------
lnmp_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
lnmp_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:80->80/tcp
lnmp_php_1 /usr/local/php/sbin/php-fpm Up 9000/tcp

由于配置文件lnmp.yaml在当前目录,这里可以进行绝对路径:

[root@docker lnmp]# docker-compose -f /root/compose_project/lnmp/lnmp.yaml  ps
Name Command State Ports
------------------------------------------------------------------------------
lnmp_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
lnmp_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:80->80/tcp
lnmp_php_1 /usr/local/php/sbin/php-fpm Up 9000/tcp

由上面可以看出将所有的容器都显示出来,如何指定显出某一个容器呢?

[root@docker ~]# docker-compose ps --help
List containers. Usage: ps [options] [SERVICE...]

一般的command命令都可以带上SERVICE,这里的service是什么呢?就是配置文件中定义的services服务,配置文件中定义了三个:nginx、php、db(不能写错service名称)

[root@docker lnmp]# docker-compose -f /root/compose_project/lnmp/lnmp.yaml  ps nginx
Name Command State Ports
--------------------------------------------------------------------------
lnmp_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:80->80/tcp

name这一列代表的是容器名称,而不是service名称

[root@docker lnmp]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5747e115947 nginx "/usr/local/nginx/..." 6 minutes ago Up 6 minutes 0.0.0.0:80->80/tcp lnmp_nginx_1
508ac0a6890c php "/usr/local/php/sb..." 6 minutes ago Up 6 minutes 9000/tcp lnmp_php_1
aaa3cc6f1040 mysql "docker-entrypoint..." 6 minutes ago Up 6 minutes 0.0.0.0:3306->3306/tcp lnmp_db_1

name的组成由project+service组成(后面的_1本人认为是构建的次数,第一次构建up就代表1吧)

If you change a service’s Dockerfile or the contents of its build directory, run docker-compose build to rebuild it.
docker-compose up --build或者docker-compose build

config:

config             Validate and view the Compose file

利用config命令可以打印处配置文件的内容和service名称以及volumes信息

[root@docker lnmp]# docker-compose -f /root/compose_project/lnmp/lnmp.yaml config --services
php
nginx
db
[root@docker lnmp]# docker-compose -f /root/compose_project/lnmp/lnmp.yaml config
services:
db:
environment:
MYSQL_DATABASE: wordpress
MYSQL_PASSWORD: wordpress
MYSQL_ROOT_PASSWORD: redhat
MYSQL_USER: wordpress
image: mysql
ports:
- 3306:3306/tcp
nginx:
image: nginx
links:
- php:php
ports:
- 80:80/tcp
volumes:
- /www:/usr/local/nginx/html:rw
php:
expose:
- '9000'
image: php
volumes:
- /www:/usr/local/nginx/html:rw
version: '3.0'
[root@docker lnmp]# docker-compose -f /root/compose_project/lnmp/lnmp.yaml config --volumes

down:停止并删除容器、网络、镜像、数据卷

down               Stop and remove containers, networks, images, and volumes
[root@docker lnmp]# docker-compose -f lnmp.yaml down
Stopping lnmp_nginx_1 ... done
Stopping lnmp_db_1 ... done
Stopping lnmp_php_1 ... done
Removing lnmp_nginx_1 ... done
Removing lnmp_db_1 ... done
Removing lnmp_php_1 ... done
Removing network lnmp_default
[root@docker lnmp]# docker-compose -f lnmp.yaml ps
Name Command State Ports
------------------------------
[root@docker lnmp]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

down也带有其他的参数:

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

exec:在运行的容器中执行命令:

exec               Execute a command in a running container
[root@docker lnmp]# docker-compose -f lnmp.yaml exec db env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=aaa3cc6f1040
TERM=xterm
MYSQL_ROOT_PASSWORD=redhat
MYSQL_PASSWORD=wordpress
MYSQL_USER=wordpress
MYSQL_DATABASE=wordpress
GOSU_VERSION=1.7
MYSQL_MAJOR=5.7
MYSQL_VERSION=5.7.20-1debian8
HOME=/root

db是指定了在哪一个service中执行env命令

images:列出镜像

[root@docker lnmp]# docker-compose -f lnmp.yaml images
Container Repository Tag Image Id Size
----------------------------------------------------------
lnmp_db_1 mysql latest 5709795eeffa 389 MB
lnmp_nginx_1 nginx latest c3babfeba09b 551 MB
lnmp_php_1 php latest 8902ce599658 1 GB

参数-q:列出镜像的id

[root@docker lnmp]# docker-compose -f lnmp.yaml images -q
5709795eeffac51eca46cf40ab36669aad27e8cb4b0e91876d5e9f7bafad6acb
c3babfeba09bab70a8e3b7e8d734ee274af4a02ed8b9b4d286cd58caf64dbdc5
8902ce599658633ee95e270843b37daabe2e0dc298861ac8c25f5f7e11a7de1e

kill:杀死容器

kill               Kill containers
[root@docker ~]# docker-compose kill --help
Force stop service containers. Usage: kill [options] [SERVICE...] Options:
-s SIGNAL SIGNAL to send to the container.
Default signal is SIGKILL.

可以看见后面也可以指定哪一个service

logs:将容器中的日志进行输出

logs               View output from containers
[root@docker ~]# docker-compose logs --help
View output from containers. Usage: logs [options] [SERVICE...] Options:
--no-color Produce monochrome output.
-f, --follow Follow log output.
-t, --timestamps Show timestamps.
--tail="all" Number of lines to show from the end of the logs
for each container.

将db的日志输出最后的10行:

[root@docker lnmp]# docker-compose -f lnmp.yaml logs -t --tail db
ERROR: tail flag must be all or a number
[root@docker lnmp]# docker-compose -f lnmp.yaml logs -t --tail="10" db
Attaching to lnmp_db_1
db_1 | 2017-11-09T14:48:24.618652188Z 2017-11-09T14:48:24.618090Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1 | 2017-11-09T14:48:24.618654153Z 2017-11-09T14:48:24.618104Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
db_1 | 2017-11-09T14:48:24.621872173Z 2017-11-09T14:48:24.620524Z 0 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1 | 2017-11-09T14:48:24.621897721Z 2017-11-09T14:48:24.620616Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1 | 2017-11-09T14:48:24.658532597Z 2017-11-09T14:48:24.658110Z 0 [Note] Event Scheduler: Loaded 0 events
db_1 | 2017-11-09T14:48:24.660827605Z 2017-11-09T14:48:24.658335Z 0 [Note] mysqld: ready for connections.
db_1 | 2017-11-09T14:48:24.660852816Z Version: '5.7.20' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
db_1 | 2017-11-09T14:48:24.660857339Z 2017-11-09T14:48:24.658347Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check.
db_1 | 2017-11-09T14:48:24.660861018Z 2017-11-09T14:48:24.658349Z 0 [Note] Beginning of list of non-natively partitioned tables
db_1 | 2017-11-09T14:48:24.679766350Z 2017-11-09T14:48:24.679398Z 0 [Note] End of list of non-natively partitioned tables

pause:暂停service:

pause              Pause services
[root@docker ~]# docker-compose pause --help
Pause services. Usage: pause [SERVICE...]

将db这个service进行暂停:

[root@docker lnmp]# docker-compose -f lnmp.yaml pause db
Pausing lnmp_db_1 ... done
[root@docker lnmp]# docker-compose -f lnmp.yaml ps
Name Command State Ports
-------------------------------------------------------------------------------
lnmp_db_1 docker-entrypoint.sh mysqld Paused 0.0.0.0:3306->3306/tcp
lnmp_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:80->80/tcp
lnmp_php_1 /usr/local/php/sbin/php-fpm Up 9000/tcp

unpause:将暂停的service进行恢复:

unpause            Unpause services
[root@docker lnmp]# docker-compose -f lnmp.yaml unpause db
Unpausing lnmp_db_1 ... done
[root@docker lnmp]# docker-compose -f lnmp.yaml ps
Name Command State Ports
------------------------------------------------------------------------------
lnmp_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
lnmp_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:80->80/tcp
lnmp_php_1 /usr/local/php/sbin/php-fpm Up 9000/tcp

port:查看端口映射信息:

port               Print the public port for a port binding
[root@docker ~]# docker-compose port --help
Print the public port for a port binding. Usage: port [options] SERVICE PRIVATE_PORT Options:
--protocol=proto tcp or udp [default: tcp]
--index=index index of the container if there are multiple
instances of a service [default: 1]
[root@docker lnmp]# docker-compose -f lnmp.yaml port db 3306
0.0.0.0:3306

restart:重启service:

restart            Restart services
[root@docker ~]# docker-compose restart --help
Restart running containers. Usage: restart [options] [SERVICE...] Options:
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
(default: 10)

将nginx这个service进行重启:

[root@docker lnmp]# docker-compose -f lnmp.yaml restart nginx
Restarting lnmp_nginx_1 ... done
[root@docker lnmp]# docker-compose -f lnmp.yaml ps
Name Command State Ports
------------------------------------------------------------------------------
lnmp_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
lnmp_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:80->80/tcp
lnmp_php_1 /usr/local/php/sbin/php-fpm Up 9000/tcp

rm:删除已经停止的服务容器,没有停止删除不了

rm                 Remove stopped containers
[root@docker ~]# docker-compose rm --help
Removes stopped service containers. By default, anonymous volumes attached to containers will not be removed. You
can override this with `-v`. To list all volumes, use `docker volume ls`. Any data which is not in a volume will be lost. Usage: rm [options] [SERVICE...] Options:
-f, --force Don't ask to confirm removal
-s, --stop Stop the containers, if required, before removing
-v Remove any anonymous volumes attached to containers
-a, --all Deprecated - no effect.
[root@docker lnmp]# docker-compose -f lnmp.yaml rm
No stopped containers

如果加上-s/--stop那么就会先将服务进行停止,然后询问你是否删除:

[root@docker lnmp]# docker-compose -f lnmp.yaml rm -s db
Stopping lnmp_db_1 ... done
Going to remove lnmp_db_1
Are you sure? [yN]

-f:代表在删除的过程中不会询问你是否删除,而是直接进行删除

run:运行一次性命令

run                Run a one-off command
[root@docker ~]# docker-compose run --help
Run a one-off command on a service. For example: $ docker-compose run web python manage.py shell

scale:扩展容器,为service设置多个容器:

scale              Set number of containers for a service
[root@docker ~]# docker-compose scale --help
Set number of containers to run for a service. Numbers are specified in the form `service=num` as arguments.
For example: $ docker-compose scale web=2 worker=3 This command is deprecated. Use the up command with the `--scale` flag
instead. Usage: scale [options] [SERVICE=NUM...]
[root@docker lnmp]# docker-compose -f lnmp.yaml scale nginx=2
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.

而且在创建多个容器时需要考虑端口及ip的监听状况(占用的情况)

start              Start services

stop               Stop services

[root@docker lnmp]# docker-compose -f lnmp.yaml stop db
Stopping lnmp_db_1 ... done
[root@docker lnmp]# docker-compose -f lnmp.yaml start db
Starting db ... done

top:显示运行的进程

top                Display the running processes
[root@docker lnmp]# docker-compose -f lnmp.yaml top
lnmp_db_1
UID PID PPID C STIME TTY TIME CMD
--------------------------------------------------------------
systemd+ 38246 38223 7 23:27 ? 00:00:00 mysqld lnmp_nginx_1
UID PID PPID C STIME TTY TIME CMD
--------------------------------------------------------------------------------------------------------------------
root 38367 38352 1 23:27 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -g daemon off;
www 38421 38367 0 23:27 ? 00:00:00 nginx: worker process lnmp_php_1
UID PID PPID C STIME TTY TIME CMD
-------------------------------------------------------------------------------------------------------------
root 38208 38186 0 23:27 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
www 38307 38208 0 23:27 ? 00:00:00 php-fpm: pool www
www 38308 38208 0 23:27 ? 00:00:00 php-fpm: pool www

指定某一个service:

[root@docker lnmp]# docker-compose -f lnmp.yaml top nginx
lnmp_nginx_1
UID PID PPID C STIME TTY TIME CMD
--------------------------------------------------------------------------------------------------------------------
root 38367 38352 0 23:27 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -g daemon off;
www 38421 38367 0 23:27 ? 00:00:00 nginx: worker process

version:显示docker-compose的版本信息:

version            Show the Docker-Compose version information
[root@docker lnmp]# docker-compose -f lnmp.yaml version
docker-compose version 1.17.0, build ac53b73
docker-py version: 2.5.1
CPython version: 2.7.13
OpenSSL version: OpenSSL 1.0.1t 3 May 2016

docker-compose命令运行的级别在service层,而如果需要对容器进行修改等操作还是运行docker命令方便

docker-compose 命令的更多相关文章

  1. 附003.Docker Compose命令详解

    一 Docker Compose命令格式 Usage: docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...] docker- ...

  2. .NET遇上Docker - 使用Docker Compose组织Ngnix和.NETCore运行

    本文工具准备: Docker for Windows Visual Studio 2015 与 Visual Studio Tools for Docker 或 Visual Studio 2017 ...

  3. Docker 三剑客之 Docker Compose

    Docker Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排,开源地址:https://github.com/docker/compose Dock ...

  4. 009.Docker Compose部署及基础使用

    一 Docker Compose概述 Compose是一个用于定义和运行多容器Docker应用程序的工具.使用Compose,您可以使用YAML文件来配置应用程序的服务.然后,使用单个命令,您可以从配 ...

  5. Docker学习笔记之使用 Docker Compose 管理容器

    0x00 概述 通过之前的介绍,我们已经基本掌握了构建.运行容器的方法,但这还远远不够,由于 Docker 采用轻量级容器的设计,每个容器一般只运行一个软件,而目前绝大多数应用系统都绝不是一个软件所能 ...

  6. Docker编排工具Docker Compose的使用

    一.安装docker compose 官网推荐方式之一: sudo curl -L "https://github.com/docker/compose/releases/download/ ...

  7. 手把手教你 Docker Compose的安装和使用

    一.Docker Compose是什么? Docker Compose是一个工具,用于定义和运行多容器应用程序的工具: Docker Compose通过yml文件定义多容器的docker应用: Doc ...

  8. Docker小白到实战之Docker Compose在手,一键足矣

    前言 Docker可以将应用程序及环境很方便的以容器的形式启动,但当应用程序依赖的服务比较多,或是遇到一个大系统拆分的服务很多时,如果还一个一个的根据镜像启动容器,那就有点累人了,到这有很多小伙伴会说 ...

  9. Docker Compose 的介绍、安装与使用

    什么是 Docker Compose? Compose 是 Docker 官方的开源项目,负责实现Docker容器集群的快速编排,开源代码在 https://github.com/docker/com ...

  10. docker和docker compose安装使用、入门进阶案例

    一.前言 现在可谓是容器化的时代,云原生的袭来,导致go的崛起,作为一名java开发,现在慌得一批.作为知识储备,小编也是一直学关于docker的东西,还有一些持续继承jenkins. 提到docke ...

随机推荐

  1. C++异常处理(转)

    引言 异常,让一个函数可以在发现自己无法处理的错误时抛出一个异常,希望它的调用者可以直接或者间接处理这个问题.而传统错误处理技术,检查到一个局部无法处理的问题时: 1.终止程序(例如atol,atoi ...

  2. 【303】C# 复制窗体 & 修改名称

    参考:C#复制粘贴窗体 参考:VS修改项目解决方案名称 一.复制窗体 在“解决方案资源管理器”(以下简称:管理器)中选择要复制的窗体,比如要复制Form2,则在Form2.cs上右单击,选择复制. 在 ...

  3. css实现栅格的方法

    1. 方法一 1.1. 效果 2. 方法二 2.1. 效果 3. 代码 3.1. Html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...

  4. copy&mutableCopy 浅拷贝(shallow copy)深拷贝 (deep copy)

    写在前面 其实看了这么多,总结一个结论: 拷贝的初衷的目的就是为了:修改原来的对象不能影响到拷贝出来得对象 && 修改拷贝出来的对象也不能影响到原来的对象 所以,如果原来对象就是imm ...

  5. [c++] final override keyword

    the two keyword are aimed at virtual function final final function must be a virtual funtion , final ...

  6. [JAVA] 小数转百分数

    import java.text.NumberFormat; //获取格式化对象 NumberFormat format = NumberFormat.getPercentInstance(); // ...

  7. SliceBox

    SliceBox相当于一个轮播图插件,只不过是3D的. 先来查看它能实现的效果: 官网:http://tympanus.net/codrops/2011/09/05/slicebox-3d-image ...

  8. easyui 获取当前页签选中的名称

    parent.parent.$("#tabs").tabs('getSelected').panel('options').title == "收藏夹管理"

  9. Spring Boot☞ 统一异常处理

    效果区:  代码区: package com.wls.integrateplugs.exception.dto; public class ErrorInfo<T> { public st ...

  10. Java基础——常用类型转换

    关于类型转化问题: (1)String--------->char / char[ ] String str = "ab"; char str1 = str.charAt(0 ...