欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

docker-compose编排容器

程序员文章站 2022-05-14 20:43:46
...

下载安装

https://github.com/docker/compose/tags
找到对应版本下载到本地,比如1.23版本下的docker-compose-Linux-x86_64

# mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compost
# chmod +x /usr/local/bin/docker-compost
# docker-compose  --version
docker-compose version 1.23.2, build 1110ad01

 

目录结构

# pwd
/data/docker/docker-compose/LNMP
[[email protected] LNMP]# tree
.
├── docker-compose.yml
└── Dockerfile
    ├── apps
    │   ├── cmake-2.8.4.tar.gz
    │   ├── libiconv-1.15.tar.gz
    │   ├── mysql-boost-5.7.25.tar.gz
    │   └── php-5.6.40.tar.gz
    ├── conf
    │   ├── index.conf
    │   ├── my.cnf.57
    │   └── nginx.conf
    ├── Dockerfile_Mysql
    ├── Dockerfile_Ngxin
    └── Dockerfile_PHP

 docker-compose文件内容(build方式为构建镜像,image方式为引用现有的镜像)

version: '2'

services:
        nginx01:
                container_name: nginx01
                restart: always
                depends_on:
                - php01
                - mysql01
#                image: centos/nginx:v1
                build:
                  context: ./
                  dockerfile: Dockerfile/Dockerfile_Ngxin
                ports:
                - 8001:80
                links:
                - php01:php-fpm
                volumes:
                - /data/01:/data
                command: 
                - /sbin/init
                networks:
                - lnmp-out
                - lnmp-in
        php01:
                container_name: php01
                restart: always
                depends_on:
                - mysql01
#                image: centos/php:v1
                build:
                  context: ./
                  dockerfile: Dockerfile/Dockerfile_PHP
                links:
                - mysql01:db
                volumes:
                - /data/01:/data
                command: 
                - /sbin/init
                networks:
                - lnmp-in
        mysql01:
                container_name: mysql01
                restart: always
#                image: centos/mysql:v1
                build:
                  context: ./
                  dockerfile: Dockerfile/Dockerfile_Mysql
                volumes:
                - /data/01:/data
                command: 
                - /sbin/init
                networks:
                - lnmp-in

networks:
        lnmp-in:
        lnmp-out:

docker-compose常用命令:

1.docker-compose.yml未被改名并且在当前目录下

#docker-compose up -d

2.docker-compose.yml被改名且不在当前目录下 

# docker-compose -f doc/data/docker/docker-compose/LNMP/ker-compose_lnmp.yml  up -d

 # docker-compose --help

build               Build or rebuild services
bundle              Generate a Docker bundle from the Compose file
config              Validate and view the Compose file
create              Create services
down                Stop and remove containers, networks, images, and volumes
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 the Docker-Compose version information

 

常用命令:

docker-compose up -d nginx                     构建建启动nignx容器

docker-compose exec nginx bash            登录到nginx容器中

docker-compose down                              删除所有nginx容器,镜像

docker-compose ps                                   显示所有容器

docker-compose restart nginx                   重新启动nginx容器

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

docker-compose build nginx                     构建镜像 。        

docker-compose build --no-cache nginx   不带缓存的构建。

docker-compose logs  nginx                     查看nginx的日志 

docker-compose logs -f nginx                   查看nginx的实时日志

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

docker-compose events --json nginx       以json的形式输出nginx的docker日志

docker-compose pause nginx                 暂停nignx容器

docker-compose unpause nginx             恢复ningx容器

docker-compose rm nginx                       删除容器(删除前必须关闭容器)

docker-compose stop nginx                    停止nignx容器

docker-compose start nginx                    启动nignx容器