docer compose学习

docker-compose 编排lnmp容器

https://gitee.com/lichenxin/docker-compose-mnpr

安装

# 二进制包安装
curl -L https://github.com/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# 为安装脚本添加执行权限
chmod +x /usr/local/bin/docker-compose

docker-compose --version


# bash 补全命令
curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose

curl -L https://raw.githubusercontent.com/docker/compose/$(docker-compose version --short)/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose

# 卸载
rm /usr/local/bin/docker-compose

# 自动构建镜像并使用镜像启动容器
docker-compose up  [-d 后台启动]

#Docker Compose将所管理的容器分为三层,分别是工程(project),服务(service)以及容器(container)。
#Docker Compose运行目录下的所有文件(docker-compose.yml, extends文件或环境变量文件等)组成一个工
#程(默认为docker-compose.yml所在目录的目录名称)。一个工程可包含多个服务;每个服务中定义了容器
#运行的镜像、参数和依赖,一个服务可包括多个容器实例。

docker-compose.yml常用命令


# build 配置构建时的选项

build: ./dir

build:
  context: ./dir
  dockerfile: Dockerfile-alternate
  args:
    buildno: 1

# command 覆盖容器启动后默认执行的命令
command: bundle exec thin -p 3000

command: [bundle, exec, thin, -p, 3000]

# dns 配置dns服务器
dns: 8.8.8.8
dns:
  - 8.8.8.8
  - 9.9.9.9


# dns_search 配置DNS的搜索域名
dns_search: example.com
dns_search:
  - dc1.example.com
  - dc2.example.com


# environment 环境变量设置
environment:
  RACK_ENV: development
  SHOW: 'true'
  SESSION_SECRET:

environment:
  - RACK_ENV=development
  - SHOW=true
  - SESSION_SECRET


# env_file 从文件中获取环境变量,可指定一个文件路径或路径列表
#通过 docker-compose -f FILE 指定了Compose文件,那么env_file中的路径是Compose文件所在目录的相对路径
#使用environment指定的环境变量会覆盖env_file指定的环境变量
env_file: .env

env_file:
  - ./common.env   # 共用
  - ./apps/web.env # web用
  - /opt/secrets.env # 密码用

# expose 暴露端口,只将端口暴露给连接的服务,而不暴露给宿主机
expose:
 - "3000"
 - "8000"


# external_links 连接到docker-compose.yml外部的容器,甚至并非Compose管理的容器
external_links:
 - redis_1
 - project_db_1:mysql
 - project_db_1:postgresql


# image 指定镜像名称或镜像id,如果本地不存在该镜像,Compose会尝试下载该镜像
image: java8

# links    连接到其他服务的容器。可以指定服务名称和服务别名( SERVICE:ALIAS ),也可只指定服务名称
web:
  links:
   - db
   - db:database
   - redis

services:
  proxy:
    build: ./proxy
    networks:
      - front

https://blog.csdn.net/Kiloveyousmile/article/details/79830810
https://blog.csdn.net/henni_719/article/details/89376111
https://docs.docker.com/compose/compose-file/
networks:
  default:
    # Use a custom driver
    driver: custom-driver-1
    # 使用已存在的网络
    external:
      name: my-pre-existing-network

  front:
    # Use a custom driver
    driver: custom-driver-1
  back:
    # Use a custom driver which takes special options
    driver: custom-driver-2
    driver_opts:
      foo: "1"
      bar: "2"

# network_mode 设置网络模式
network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"


# ports 暴露端口信息 可使用HOST:CONTAINER 的格式,也可只指定容器端口(此时宿主机将会随机选择端口),类似于docker run -p
# 当使用HOST:CONTAINER 格式映射端口时,容器端口小于60将会得到错误的接口,因为yaml会把xx:yy 的数字解析为60进制。因此,建议使用字符串的形式
ports:
 - "3000"
 - "3000-3005"
 - "8000:8000"
 - "9090-9091:8080-8081"
 - "49100:22"
 - "127.0.0.1:8001:8001"
 - "127.0.0.1:5000-5010:5000-5010"


# volumes 卷挂载路径设置 可以设置宿主机路径 (HOST:CONTAINER) ,也可指定访问模式 (HOST:CONTAINER:ro)
volumes:
  # Just specify a path and let the Engine create a volume
  - /var/lib/mysql

  # Specify an absolute path mapping
  - /opt/data:/var/lib/mysql

  # Path on the host, relative to the Compose file
  - ./cache:/tmp/cache

  # User-relative path
  - ~/configs:/etc/configs/:ro

  # Named volume
  - datavolume:/var/lib/mysql

# volumes_from 从另一个服务或容器挂载卷。可指定只读(ro)或读写(rw),默认是读写(rw)。
volumes_from:
 - service_name
 - service_name:ro
 - container:container_name
 - container:container_name:rw


https://docs.docker.com/compose/compose-file/






http://www.itmuch.com/docker/20-docker-compose-install/
https://yeasy.gitbooks.io/docker_practice/content/compose/introduction.html

docker-compose常用命令

docker-compose --help你会看到如下这么多命令

build               构建或重新构建服务。服务被构建后将会以project_service 的形式标记
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
images              List images

kill                停止指定服务的容器
docker-compose kill containerId

port                Print the public port for a port binding
docker-compose port eureka 8761   # 输出eureka服务8761端口所绑定的公共端口


logs                View output from containers



up                  Create and start containers 构建、创建|重新创建、启动,连接服务的相关容器
docker-compose up  # -d 可在后台启动并运行所有容器

ps                  List containers 列出所有容器
docker-compose ps


pull                Pull service images 下载服务镜像
push                Push service images

pause               Pause services

restart             Restart services


rm                  Remove stopped containers 删除指定服务的容器
docker-compose rm eureka

run                 Run a one-off command 在一个服务上执行一个命令
docker-compose run web bash


scale               Set number of containers for a service  设置指定服务运行容器的个数 以service=num的形式指定
docker-compose scale user=3 movie=3



start               Start services 启动指定服务已存在的容器
docker-compose start eureka


stop                Stop services 停止已运行的容器
docker-compose stop eureka



top                 Display the running processes
unpause             Unpause services


version             Show the Docker-Compose version information


docker-compose help COMMAND   
#查看docker-compose build的帮助
docker-compose help build

  

3.常用命令

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容器

version: '2'
services:
  mysql:
    image: mysql:5.7
    expose:
      - "3306"
    environment:
      - MYSQL_ROOT_PASSWORD=123456
  wordpress:
    image: wordpress
    ports:
      - "80:80"
    environment:
      - WORDPRESS_DB_HOST=mysql
      - WORDPRESS_DB_USER=root
      - WORDPRESS_DB_PASSWORD=123456

FROM mysql:5.6

COPY docker-entrypoint.sh /usr/local/bin/
COPY ./sql /sql


RUN chmod -R +x /usr/local/bin/

ENTRYPOINT ["docker-entrypoint.sh"]

EXPOSE 3306
CMD ["mysqld"]


FROM centos:latest
RUN \
  yum update -y && \
  yum install -y epel-release && \
  yum install -y iproute python-setuptools hostname inotify-tools yum-utils which jq && \
  yum clean all && \
  easy_install supervisor
COPY container-files /
RUN chmod +x /config/bootstrap.sh
VOLUME ["/data"]
ENTRYPOINT ["/config/bootstrap.sh"]

https://blog.csdn.net/u010882691/article/details/82259033
https://blog.csdn.net/sinat_27143551/article/details/79873050
https://blog.csdn.net/hujun_123456/article/details/80547473

spring cloud 与 docker-compose构建微服务

https://blog.csdn.net/vchy_zhao/article/details/70238432

https://testerhome.com/topics/12768

https://www.cnblogs.com/senlinyang/p/8856975.html

https://www.cnblogs.com/moxiaoan/p/9299404.html

https://blog.csdn.net/shen_gang/article/details/40619073
https://blog.csdn.net/a745233700/article/details/80452862
https://blog.csdn.net/weixin_39332806/article/details/80420411

分析Mysql 5.6的Dockerfile
https://yq.aliyun.com/articles/68245

Docker Compose部署lnmp
Docker Compose搭建mysql主从复制
https://www.cnblogs.com/bigberg/p/8884788.html
Docker-Compose部署nginx代理Tomcat集群
Docker Compose部署 nginx代理Tomcat集群

https://download.csdn.net/download/u011781521/10440466

Docker Compose 运行 Tomcat
https://blog.csdn.net/weixin_38187317/article/details/81487301

https://github.com/Bigberg/docker

https://blog.csdn.net/zhang__rong/article/details/88697544
https://blog.csdn.net/a745233700/article/details/80452862
https://blog.csdn.net/weixin_39332806/article/details/80420411

Docker mysql 主从

posted @ 2018-09-26 09:21  antball  阅读(498)  评论(0编辑  收藏  举报