LoginSignup
6
9

More than 5 years have passed since last update.

dockerでjenkinsを立てる+その中でDockerを走らせる(docker in docker)

Last updated at Posted at 2018-08-14

docker-compose.ymlファイル

jenkins-docker-compose.yml
version: '2'

services:
  jenkins:
    image: 'jenkins:2.60.3'
    container_name: jenkins
    user: root
    restart: always
    ports:
      - '8080:8080'
      - '50000:50000'
    volumes:
      - ./data/jenkins:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock

ポイント:
1. volumeでdocker.sockをhost(どっかーを動かすマシン)と共有することで、立ち上げたDockerの中でもDockerを使えるようになる。(が、Dockerの中からでも、立ち上げたContainerを削除できてしまうので、注意が必要)
2. /var/jenkins_homeはdockerイメージが読む場所なので、変えてはいけない。Host側の/data/jenkinsは好きな場所にしてよい。こうすることで、Docker自体が死んでも、Dataは残っているので、もう一回立ち直せば、同じ状態に復元できる。

JenkinsをDockerで立ち上げる

docker-compose -f jenkins-docker-compose.yml up

これで、localの場合は、http://0.0.0.0:8080でJenkinsにアクセスできる。

Jenkins内でDockerを使う

jenkins のbuild shellの中で以下を書くと

docker --version
docker run --rm hello-world

ちゃんとHelloworldが出来てることがわかる。

+ docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9db2ca6ccae0: Pulling fs layer
9db2ca6ccae0: Verifying Checksum
9db2ca6ccae0: Download complete
9db2ca6ccae0: Pull complete
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

DockerでJeninsを立てた時にJenkins内でVolumeを共有できない場合の対応 後から更新(2018/08/28)

Dockerで立てたJenkins内にMountしたVolumeをそのまま,jenkinsから立てるDocker container内でmountできない。

hostの./data/jenkins -> jenkins containerの /var/jenkins_homeにマウントしているが、

Jenkins内のプロジェクトをdocker-composeで管理している時に、そこから別のプロジェクトのDirをマウントしたいときが出てくる(かも)

その時に、

  1. jenkins上でrepoを持ってくる
  2. 持ってきたrepoを新しいcontainerにMountする

をやろうとすると、そもそも1のJenkinsのworkspaceはホストにmountされた部分なので(なのでなのかわからないが)、あるコンテナ内のdirを別のコンテナにマウントすることになり、これはできない模様。(同じマウントオプションにするには、volumes_fromというオプションがある)

そこで仕方ないので、jenkinsがマウントしているHost上でのPathに実態があるので、それを新しく作るcontainerのdocker-compose.ymlに書いて解決した。

hostの./data/jenkinsの絶対パス。今回は、/home/ubuntu/docker-compose/data/jenkinsは、自分で変更する必要がある!/home/ubuntu/docker-compose/の下で、jenkinsのdocker-compose.ymlを動かしたとして。

内側で動かすdocker-compose.yml

services:
  my_container:
    ...
    volumes:
      - "/home/ubuntu/docker-compose/data/jenkins/workspace/<別のjenkins jobのrepo>:/another_repo"
      - "/home/ubuntu/docker-compose/data/jenkins/workspace/my_repo/<repo内で引っ張ってきたrepo>:/cloned_repo"

...

参考

  1. https://stackoverflow.com/questions/49312100/docker-in-docker-volumes-not-working-full-of-files-in-1st-level-container-em

  2. https://damnhandy.com/2016/03/06/creating-containerized-build-environments-with-the-jenkins-pipeline-plugin-and-docker-well-almost/

  3. https://github.com/jenkinsci/mesos-plugin/issues/159

6
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
9