十分钟配置Docker镜像自动构建

十分钟配置Docker镜像自动构建

这是参加Rancher 深圳站线下活动时候的一个演示项目

现在贴一下整个的步骤,供大家参考~

一,Git

可选的git系统如下,示例用gogs

  • Gogs
  • Gitlab
  • Github

Gogs,Docker-Compose安装:

```
version: '2'

services:

  app:
    image: gogs/gogs:0.9.113
    ports:
      - "10022:22"
      - "10080:3000"
    volumes:
      - ./gogs:/data
    restart: always
    links:
        - mysql:mysql
    environment:
      - Domain=${GOGS_IP}
      - SSH_PORT=${GOGS_SSH_PORT}

  mysql:
    image: mysql:5.7.13
    restart: always
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=gogs
```
# env
export MYSQL_ROOT_PASSWORD={mysql_root_password}
export GOGS_IP={ip}
export GOGS_SSH_PORT=10022
export GOGS_HTTP_PORT=10080

# run
docker-compose up -d

浏览器打开:http:{ip}:{http_port} 完成安装

登录后创建测试项目:alpine-drone

二,Drone CI

Drone,Docker-compose安装(Gogs):

version: '2'

services:
  drone-server:
    image: thonatos/alpine-drone # 换成drone/drone:0.5.0
    ports:
      - 80:8000
    volumes:
      - ./drone:/var/lib/drone/
    restart: always
    environment:
      - DRONE_OPEN=true
      - DRONE_DEBUG=false
      - DRONE_GOGS=true
      - DRONE_GOGS_URL=http://${GOGS_IP}:${GOGS_HTTP_PORT}
      - DRONE_GOGS_PRIVATE_MODE=true
      - DRONE_GOGS_SKIP_VERIFY=true
      - DRONE_SECRET=${DRONE_SECRET}
      # - DRONE_ADMIN=suyi

  drone-agent:
    image: thonatos/alpine-drone # 换成drone/drone:0.5.0
    command: agent
    restart: always
    depends_on: [ drone-server ]
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - DRONE_SERVER=ws://drone-server:8000/ws/broker
      - DRONE_SECRET=${DRONE_SECRET}

Drone,Docker-compose安装(Github):

version: '2'

services:
  drone-server:
    image: drone/drone:0.5.0
    environment:
      DRONE_GITHUB: true
      DRONE_GITHUB_CLIENT: 95c0282573633eb25e82
      DRONE_GITHUB_SECRET: 30f5064039e6b359e075

运行Drone:

# env
export DRONE_SECRET={secret}

# run
docker-compose up -d

浏览器打开:http://{ip}:{drone_port},使用安装gogs设置的管理员账号密码登录,点击右侧account看到当前项目,点开项目旁边的构建选项即可


三,配置Docker自动构建

下载并配置 drone cli:

# mac
brew tap drone/drone
brew install drone --devel

# http://readme.drone.io/usage/getting-started-cli/
# 下载并配置到PATH中

# run
export DRONE_SERVER=http://drone.mycompany.com
export DRONE_TOKEN={}   # token刚才drone的管理界面account中点show_token

在项目内创建.drone.yml文件,内容如下:

pipeline:
  docker:
    # registry: {}              # 私有registry地址
    # auth: {}                  # auth_token
    image: plugins/docker
    repo: thonatos/alpine-drone # 修改为你对应的repo名字
    tags: latest                # tags

更多配置项:Docker | Plugins | Drone

配置push信息:

# 获取repo列表
drone repo ls
# suyi/alpine-drone

# 添加docker hub信息
drone secret add --image=plugins/docker suyi/alpine-drone DOCKER_USERNAME thonatos

drone secret add --image=plugins/docker suyi/alpine-drone DOCKER_PASSWORD {password}

# 签名
drone sign suyi/alpine-drone

提交代码到git:

git init
git add .
git ct -m "init"
git remote add origin ssh://git@{ip}:{ssh_port}/suyi/alpine-drone.git
git push -u origin master

可以看到如下信息:

构建过程:

可以看到已经推送成功了~


--

专栏不定期更新容器实践过程中的一些经历,欢迎关注~

编辑于 2017-01-16

文章被以下专栏收录