Docker构建&只在第一个流浪者中运行

时间:2017-08-21 19:57:20

标签: docker jenkins vagrant

问题:有没有办法只在第一次创建流浪汉机器时才能构建和运行泊坞窗图像?

问题:运行vagrant provision时出现docker: Error response from daemon: Conflict. The container name "/jenkins" is already in use by container <snip>错误。

其他信息:我运行了vagrant provision,因为我在plugins.txt文件中添加了另一个插件,以便在设置jenkins机器时自动安装。

我以这种方式创建了构建脚本,以使vagrant机器构建尽可能快,并且docker镜像尽可能小(仍需要在这里工作)。另外,因为我想删除Jenkins设置GUI,并创建了默认的管理员帐户。

Vagrantfile:

Vagrant.require_version ">= 1.5"

Vagrant.configure("2") do |config|

    config.vbguest.auto_update = false

    config.vm.provider :virtualbox do |v|
        v.customize [
            "modifyvm", :id,
            "--memory", 1024,
            "--natdnshostresolver1", "on",
            "--cpus", 2,
        ]
    end

    config.vm.box = "ubuntu/trusty64"
    config.vm.hostname = "dev-jenkins-site"

    # Forward jenkins port.
    config.vm.network "forwarded_port", guest: 8080, host: 8080

    config.vm.network :private_network, ip: "192.168.34.56"

    jenkins_dir = "/var/jenkins_home"

    # Run Dockerfile.
    config.vm.provision "docker"

    # Restart jenkins container, in case it was stopped (which is possible from a 'vagrant halt').
    #   This won't run if this after the docker build & run provision (below) when using 'vagrant provision' because it
    #   returns an error and exits before this can run.
    config.vm.provision "shell",
      inline: "docker restart jenkins",
      run: "always"

    # Only need to build the image and container one time.
    #   Using '--restart always' doesn't work when the container is stopped from 'vagrant halt'.
    config.vm.provision "shell",
      inline: "cd /vagrant && make jenkins && docker run -d --name jenkins -p 80:8080 -p 50000:50000 -v #{jenkins_dir}:#{jenkins_dir} draven/jenkins",
      run: "once"

    # Files on your local machine
    config.vm.synced_folder "./jenkins_home", jenkins_dir
    config.vm.synced_folder ".", "/vagrant"
end

Dockerfile:

FROM jenkins/jenkins:latest

COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN /usr/local/bin/install-plugins.sh < /usr/share/jenkins/ref/plugins.txt

ENV JENKINS_USER admin
ENV JENKINS_PASS admin

# Skip initial setup
ENV JAVA_OPTS -Djenkins.install.runSetupWizard=false

COPY default-user.groovy /usr/share/jenkins/ref/init.groovy.d/

VOLUME /var/jenkins_home

生成文件:

SHELL:=/bin/bash

jenkins:
    cd docker-image && docker build -t draven/jenkins .

1 个答案:

答案 0 :(得分:1)

如果没有另外指示,流浪性条款总是运行所有供应商。

解决方案是通过首先删除同名的容器(如果存在)来使您的docker provisioner具有幂等性:

config.vm.provision "shell",
  run: "once",
  inline: <<-SHELL
    cd /vagrant \
    && docker ps -a -f "name=jenkins" \
    && docker rm jenkins;
    make jenkins \
    && docker run -d \
        --name jenkins \
        -p 80:8080 -p 50000:50000 \
        -v #{jenkins_dir}:#{jenkins_dir} \                   
        draven/jenkins
  SHELL