docker入门(二)dockerfile 与docker-compose

  • 2016-12-17 17:00:55
  • 1820
  • 0

上篇中我们以一个非常的例子简述了下docker给我们带来的便利。然而尽管如此,但docker那些镜像能提供的服务毕竟是纯粹的,我们在做项目的过程中不免用到各种不同的服务、扩展包或组件等。

因此,基于docker 我们会面临以下两种情况:一种是须在某个镜像的基础上安装其他服务或者扩展包;一种是须集合多个镜像的服务。对于第一种情况,建议使用dockerfile文件;第二种情况,建议使用docker-compose。接下来将简单介绍下另外两个docker相关神器:dockerfile和docker-compose。

一、Dockerfile

1、定义:是一种被Docker程序解释的脚本,Dockerfile由一条一条的指令组成,每条指令对应Linux下面的一条命令。

2、解释:当我们需要定制自己额外的需求时,只需在Dockerfile上添加或者修改指令,Docker程序在读取Dockerfile时,将这些指令翻译成真正的linux指令, 然后生成定制的image。dockerfile指令见:http://www.docker.org.cn/article/114.html

3、举例:

FROM nginx:1.9
MAINTAINER docker 

ADD nginx.conf /etc/nginx/nginx.conf
ADD conf.d/* /etc/nginx/conf.d/
RUN mkdir -p /var/www/static

RUN apt-get -y update
RUN apt-get -y install python-pip
RUN apt-get -y install vim 
RUN apt-get -y install cron
RUN mkdir -p /search/nginx ADD . /search/nginx WORKDIR /search/nginx RUN crontab rotlog_crontab

该dockerfile是在镜像nginx:1.9的基础上,修改原始配置文件和服务配置文件,同时安装pip、vim、cron组件、并配置好crontab运行的任务。

二、Docker-compose:

1、定义:Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.

2、解释:compose 是被用来运行多个容器的docker应用,通过compose,你可以用compose 文件配置你的各种应用服务,然后,通过运行一条指令,你可以以你的配置创建和启动所有服务。

3、安装:pip install  docker-compose 

4、举例:

version: '2'
services:
  ngx:
    build: nginx(nginx为生成ngx镜像的dockerfile所在相对路径)
    image: ngx:latest
    ports:
      - "8016:8006"
    command: /bin/bash -c "nginx -g 'daemon off;'"
    volumes_from:
      - web
    links:
      - web:search_web
    depends_on:
      - web
  web:
    build: .
    image: base:latest
    working_dir: /search/search
    volumes:
      - .:/search
    command:
      gunicorn -c guni.conf search.wsgi:application
    links:
      - redis:search_redis
      - mysql:search_mysql
    depends_on:
      - redis
      - mysql
  redis:
    ports:
      - "6378:6379"
    image: redis
    command: redis-server --appendonly yes
  mysql:
    ports:
      - "33061:3306"
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=passwdissearch
      - MYSQL_DATABASE=search
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

该docker-compose文件创建了一个使用mysql、redis、nginx服务的python web服务。

5、运行:最简洁的指令:docker-compose up(需在docker-compose文件所在目录执行)。如需使用参数,可以通过使用帮助指令:docker-compose --help 进行查看。

 

 


发表评论

* *