在園子裏看到了這篇關於 compose 的文章,很是好!!轉過來,先附上原文地址css
http://www.cnblogs.com/52fhy/p/5991344.htmlhtml
-----mysql
一、定義Dockerfile,方便遷移到任何地方;
二、編寫docker-compose.yml文件;
三、運行docker-compose up
啓動服務nginx
準備工做:提早下載好鏡像:web
docker pull mysql docker pull wordpress
須要新建一個空白目錄,例如wptest。新建一個docker-compose.ymlredis
version: '2' services: web: image: wordpress:latest links: - db ports: - "8002:80" environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_PASSWORD: 123456 db: image: mysql environment: - MYSQL_ROOT_PASSWORD=123456
以上命令的意思是新建db和wordpress容器。等同於:sql
$ docker run --name db -e MYSQL_ROOT_PASSWORD=123456 -d mysql $ docker run --name some-wordpress --link db:mysql -p 8002:80 -d wordpress
注意,若是你是直接從fig遷移過來的,且
web
裏links
是- db:mysql
,這裏會提示沒有給wordpress設置環境變量,這裏須要添加環境變量WORDPRESS_DB_HOST
和WORDPRESS_DB_PASSWORD
。docker
好,咱們啓動應用:ubuntu
# docker-compose up Creating wptest_db_1... Creating wptest_wordpress_1... Attaching to wptest_db_1, wptest_wordpress_1 wordpress_1 | Complete! WordPress has been successfully copied to /var/www/html
就成功了。瀏覽器訪問 http://localhost:8002(或 http://host-ip:8002)便可。數組
默認是前臺運行並打印日誌到控制檯。若是想後臺運行,能夠:
docker-compose up -d
服務後臺後,可使用下列命令查看狀態:
# docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------- figtest_db_1 docker-entrypoint.sh mysqld Up 3306/tcp figtest_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8002->80/tcp # docker-compose logs Attaching to wptest_wordpress_1, wptest_db_1 db_1 | 2016-10-4T14:38:46.98030Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). db_1 | 2016-10-4T14:38:46.99974Z 0 [Note] mysqld (mysqld 5.7.15) starting as process 1 ... db_1 | 2016-10-4T14:38:46.27191Z 0 [Note] InnoDB: PUNCH HOLE support available
中止服務:
# docker-compose stop Stopping wptest_wordpress_1... Stopping wptest_db_1...
從新啓動服務:
docker-compose restart
每一個docker-compose.yml必須定義image
或者build
中的一個,其它的是可選的。
指定鏡像tag或者ID。示例:
image: redis image: ubuntu:14.04 image: tutum/influxdb image: example-registry.com:4000/postgresql image: a4bc65fd
注意,在
version 1
裏同時使用image
和build
是不容許的,version 2
則能夠,若是同時指定了二者,會將build
出來的鏡像打上名爲image
標籤。
用來指定一個包含Dockerfile
文件的路徑。通常是當前目錄.
。Fig將build並生成一個隨機命名的鏡像。
注意,在
version 1
裏bulid
僅支持值爲字符串。version 2
裏支持對象格式。
build: ./dir build: context: ./dir dockerfile: Dockerfile-alternate args: buildno: 1
context
爲路徑,dockerfile
爲須要替換默認docker-compose
的文件名,args
爲構建(build)過程當中的環境變量,用於替換Dockerfile裏定義的ARG
參數,容器中不可用。示例:
Dockerfile:
ARG buildno ARG password RUN echo "Build number: $buildno" RUN script-requiring-password.sh "$password"
docker-compose.yml:
build: context: . args: buildno: 1 password: secret build: context: . args: - buildno=1 - password=secret
用來覆蓋缺省命令。示例:
command: bundle exec thin -p 3000
command
也支持數組形式:
command: [bundle, exec, thin, -p, 3000]
用於連接另外一容器服務,如須要使用到另外一容器的mysql服務。能夠給出服務名和別名;也能夠僅給出服務名,這樣別名將和服務名相同。同docker run --link
。示例:
links: - db - db:mysql - redis
使用了別名將自動會在容器的/etc/hosts
文件裏建立相應記錄:
172.17.2.186 db 172.17.2.186 mysql 172.17.2.187 redis
因此咱們在容器裏就能夠直接使用別名做爲服務的主機名。
用於暴露端口。同docker run -p
。示例:
ports: - "3000" - "8000:8000" - "49100:22" - "127.0.0.1:8001:8001"
expose提供container之間的端口訪問,不會暴露給主機使用。同docker run --expose
。
expose: - "3000" - "8000"
掛載數據卷。同docker run -v
。示例:
volumes: - /var/lib/mysql - cache/:/tmp/cache - ~/configs:/etc/configs/:ro
掛載數據卷容器,掛載是容器。同docker run --volumes-from
。示例:
volumes_from: - service_name - service_name:ro - container:container_name - container:container_name:rw
container:container_name
格式僅支持version 2
。
添加環境變量。同docker run -e
。能夠是數組或者字典格式:
environment: RACK_ENV: development SESSION_SECRET: environment: - RACK_ENV=development - SESSION_SECRET
用於指定服務依賴,通常是mysql、redis等。
指定了依賴,將會優先於服務建立並啓動依賴。
links
也能夠指定依賴。
連接搭配docker-compose.yml
文件或者Compose
以外定義的服務,一般是提供共享或公共服務。格式與links
類似:
external_links: - redis_1 - project_db_1:mysql - project_db_1:postgresql
注意,
external_links
連接的服務與當前服務必須是同一個網絡環境。
添加主機名映射。
extra_hosts: - "somehost:162.242.195.82" - "otherhost:50.31.209.229"
將會在/etc/hosts
建立記錄:
162.242.195.82 somehost 50.31.209.229 otherhost
繼承自當前yml文件或者其它文件中定義的服務,能夠選擇性的覆蓋原有配置。
extends: file: common.yml service: webapp
service
必須有,file
可選。service
是須要繼承的服務,例如web
、database
。
設置網絡模式。同docker的--net
參數。
net: "bridge" net: "none" net: "container:[name or id]" net: "host"
自定義dns服務器。
dns: 8.8.8.8 dns: - 8.8.8.8 - 9.9.9.9
這些命令都是單個值,含義請參考docker run。
cpu_shares: 73 cpu_quota: 50000 cpuset: 0,1 user: postgresql working_dir: /code domainname: foo.com hostname: foo ipc: host mac_address: 02:42:ac:11:65:43 mem_limit: 1000000000 mem_limit: 128M memswap_limit: 2000000000 privileged: true restart: always read_only: true shm_size: 64M stdin_open: true tty: true
$ docker-compose Define and run multi-container applications with Docker. Usage: docker-compose [-f <arg>...] [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) --verbose Show more output -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 (for example if your docker host is an IP address) Commands: 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 kill Kill containers logs View output from containers pause Pause services port Print the public port for a port binding ps List containers pull Pulls 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 unpause Unpause services up Create and start containers version Show the Docker-Compose version information
# 關閉全部正在運行容器 docker ps | awk '{print $1}' | xargs docker stop # 刪除全部容器應用 docker ps -a | awk '{print $1}' | xargs docker rm # 或者 docker rm $(docker ps -a -q)