Docker测试环境笔记

血红的双手。 提交于 2019-12-04 08:26:32

构建Sinatra应用程序

[root@dockerWEBrickDocker]# vim Dockerfile

 

FROM ubuntu

MAINTAINER wyfTurnbull huisebug@outlook.com

ENV REFRSHED_AT2017-06-28

 

RUN apt-get update

RUN apt-get -y installruby ruby-dev build-essential redis-tools

RUN gem install--no-rdoc --no-ri sinatra json redis

 

RUN mkdir -p/opt/webapp

EXPOSE 4567

CMD ["/opt/webapp/bin/webapp" ]

~                                      

构建后

 

[root@dockerWEBrickDocker]# docker images

REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE

ubuntu/sinatra                1                   4d46ed419d3d        18 minutes ago      386MB

unbuntu/nginx                 1                   d39894d30c41        5 hours ago         214MB

localhost:5000/wyf/huisebug   1                   1a4b54d5d019        25 hours ago        257MB

[root@dockerWEBrickDocker]# wget --cut-dirs=3 -nH -r --no-parenthttp://dockerbook.com/code/5/sinatra/webapp/

--2017-06-2815:48:36-- http://dockerbook.com/code/5/sinatra/webapp/

正在解析主机dockerbook.com (dockerbook.com)... 104.236.224.80, fe80::601:acff:fe91:a301

正在连接dockerbook.com (dockerbook.com)|104.236.224.80|:80... 已连接。

已发出 HTTP 请求,正在等待回应... 302 Found

位置:https://dockerbook.com/code/5/sinatra/webapp/[跟随至新的 URL]

--2017-06-2815:48:39-- https://dockerbook.com/code/5/sinatra/webapp/

正在连接dockerbook.com (dockerbook.com)|104.236.224.80|:443... 已连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:528[text/html]

正在保存至:“webapp/index.html”

 

100%[==========================================================================================>]528         --.-K/s 用时 0s     

 

2017-06-2815:48:41 (19.4 MB/s) - 已保存 “webapp/index.html” [528/528])

 

正在载入robots.txt;请忽略错误消息。

--2017-06-2815:48:41-- https://dockerbook.com/robots.txt

再次使用存在的到dockerbook.com:443 的连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:57[text/plain]

正在保存至:“robots.txt”

 

100%[==========================================================================================>]57          --.-K/s 用时 0s     

 

2017-06-2815:48:41 (10.6 MB/s) - 已保存 “robots.txt” [57/57])

 

--2017-06-2815:48:41-- https://dockerbook.com/code/5/sinatra/webapp/bin/

再次使用存在的到dockerbook.com:443 的连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:314[text/html]

正在保存至: “webapp/bin/index.html”

 

100%[==========================================================================================>]314         --.-K/s 用时 0s     

 

2017-06-2815:48:41 (22.9 MB/s) - 已保存 “webapp/bin/index.html” [314/314])

 

--2017-06-2815:48:41--  https://dockerbook.com/code/5/sinatra/webapp/lib/

再次使用存在的到dockerbook.com:443 的连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:314[text/html]

正在保存至:“webapp/lib/index.html”

 

100%[==========================================================================================>]314         --.-K/s 用时 0s     

 

2017-06-2815:48:42 (22.3 MB/s) - 已保存 “webapp/lib/index.html” [314/314])

 

--2017-06-2815:48:42-- https://dockerbook.com/code/5/sinatra/webapp/Dockerfile

再次使用存在的到dockerbook.com:443 的连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:307[text/plain]

正在保存至:“webapp/Dockerfile”

 

100%[==========================================================================================>]307         --.-K/s 用时 0s     

 

2017-06-2815:48:42 (33.4 MB/s) - 已保存 “webapp/Dockerfile” [307/307])

 

--2017-06-2815:48:42-- https://dockerbook.com/code/5/sinatra/webapp/bin/webapp

再次使用存在的到dockerbook.com:443 的连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:119[text/plain]

正在保存至:“webapp/bin/webapp”

 

100%[==========================================================================================>]119         --.-K/s 用时 0s     

 

2017-06-2815:48:42 (21.3 MB/s) - 已保存 “webapp/bin/webapp” [119/119])

 

--2017-06-2815:48:42-- https://dockerbook.com/code/5/sinatra/webapp/lib/app.rb

再次使用存在的到dockerbook.com:443 的连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:224[text/plain]

正在保存至:“webapp/lib/app.rb”

 

100%[==========================================================================================>]224         --.-K/s 用时 0s     

 

2017-06-2815:48:42 (21.9 MB/s) - 已保存 “webapp/lib/app.rb” [224/224])

 

FINISHED--2017-06-28 15:48:42--

Total wall clocktime: 6.4s

Downloaded: 7files, 1.8K in 0s (21.8 MB/s)

[root@dockerWEBrickDocker]# ls

Dockerfile  robots.txt webapp

[root@dockerWEBrickDocker]#

[root@dockerWEBrickDocker]# chmod +x ./webapp/bin/webapp

[root@dockerWEBrickDocker]# docker run -d -p 4567 --name webapp -v $PWD/webapp:/opt/webappubuntu/sinatra:1

94e7bb109ea2a8630ebf4e5ce3c26a16b4256caacd370522d72eb7098ac7f87a

[root@dockerWEBrickDocker]# docker logs webapp

[2017-06-28 07:50:31]INFO  WEBrick 1.3.1

[2017-06-2807:50:31] INFO  ruby 2.3.1 (2016-04-26)[x86_64-linux-gnu]

== Sinatra(v2.0.0) has taken the stage on 4567 for development with backup from WEBrick

[2017-06-2807:50:31] INFO WEBrick::HTTPServer#start: pid=1 port=4567

[root@dockerWEBrickDocker]# docker logs -f webapp

[2017-06-2807:50:31] INFO  WEBrick 1.3.1

[2017-06-2807:50:31] INFO  ruby 2.3.1 (2016-04-26)[x86_64-linux-gnu]

== Sinatra(v2.0.0) has taken the stage on 4567 for development with backup from WEBrick

[2017-06-2807:50:31] INFO WEBrick::HTTPServer#start: pid=1 port=4567

 

^C

[root@dockerWEBrickDocker]# docker top webapp

UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD

root                58751               58735               0                   15:50               ?                   00:00:00            /usr/bin/ruby/opt/webapp/bin/webapp

[root@dockerWEBrickDocker]# docker ps

CONTAINER ID        IMAGE              COMMAND                  CREATED              STATUS              PORTS                     NAMES

94e7bb109ea2        ubuntu/sinatra:1    "/opt/webapp/bin/w..."   About a minute ago   Up About a minute   0.0.0.0:32768->4567/tcp   webapp

9e1a39356c04        unbuntu/nginx:1     "nginx"                  4 hours ago          Up 4 hours          0.0.0.0:8777->80/tcp      nginx1

f9d66082594d        registry            "/entrypoint.sh/e..."   24 hours ago         Up 24 hours         0.0.0.0:5000->5000/tcp    reg

67346d341ef0        test/test:1         "/usr/sbin/apache2..."   25 hours ago         Up 25 hours         0.0.0.0:9977->80/tcp      test1

[root@dockerWEBrickDocker]# docker port webapp 4567

0.0.0.0:32768

[root@dockerWEBrickDocker]#

 

 

 

[root@dockerWEBrickDocker]# curl -i -H 'Accept: application/json' -d'name=Foo&status=Bar' http://localhost\

> :32768/json

HTTP/1.1 200 OK

Content-Type:text/html;charset=utf-8

Content-Length: 29

X-Xss-Protection:1; mode=block

X-Content-Type-Options:nosniff

X-Frame-Options:SAMEORIGIN

Server:WEBrick/1.3.1 (Ruby/2.3.1/2016-04-26)

Date: Wed, 28 Jun2017 08:02:07 GMT

Connection:Keep-Alive

 

[root@dockerWEBrickDocker]# ls

Dockerfile  robots.txt webapp

[root@dockerWEBrickDocker]# cp Dockerfile Dockerfilewebapp

 

构建Redis服务器

[root@dockerWEBrickDocker]# vim Dockerfile

FROM ubuntu

MAINTAINER wyfTurnbull huisebug@outlook.com

ENV REFRSHED_AT2017-06-28

RUN apt-get update

RUN apt-get -yinstall redis-server redis-tools

 

EXPOSE 6379

ENTRYPOINT ["/usr/bin/redis-server" ]

CMD []

 

[root@dockerWEBrickDocker]# docker build -t "ubuntu/redis:1" .

Sending buildcontext to Docker daemon  12.29kB

Step 1/8 : FROMubuntu

 ---> d355ed3537e9

Step 2/8 :MAINTAINER wyf Turnbull huisebug@outlook.com

 ---> Using cache

 ---> e9c604014f9e

Step 3/8 : ENVREFRSHED_AT 2017-06-28

 ---> Using cache

 ---> a1ffe5d0754e

Step 4/8 : RUNapt-get update

 ---> Using cache

 ---> e58a758ae2b4

Step 5/8 : RUNapt-get -y install redis-server redis-tools

 ---> Running in 8ac0a93f0a78

Reading packagelists...

Buildingdependency tree...

Reading stateinformation...

The followingadditional packages will be installed:

  libjemalloc1

Suggestedpackages:

  ruby-redis

The following NEWpackages will be installed:

  libjemalloc1 redis-server redis-tools

0 upgraded, 3newly installed, 0 to remove and 8 not upgraded.

Need to get 517 kBof archives.

After thisoperation, 1505 kB of additional disk space will be used.

Get:1http://archive.ubuntu.com/ubuntu xenial/universe amd64 libjemalloc1 amd64 3.6.0-9ubuntu1[78.9 kB]

Get:2http://archive.ubuntu.com/ubuntu xenial/universe amd64 redis-tools amd642:3.0.6-1 [95.3 kB]

Get:3http://archive.ubuntu.com/ubuntu xenial/universe amd64 redis-server amd642:3.0.6-1 [343 kB]

debconf: delayingpackage configuration, since apt-utils is not installed

Fetched 517 kB in1min 47s (4804 B/s)

Selectingpreviously unselected package libjemalloc1.

(Reading database... 4764 files and directories currently installed.)

Preparing tounpack .../libjemalloc1_3.6.0-9ubuntu1_amd64.deb ...

Unpackinglibjemalloc1 (3.6.0-9ubuntu1) ...

Selectingpreviously unselected package redis-tools.

Preparing tounpack .../redis-tools_2%3a3.0.6-1_amd64.deb ...

Unpackingredis-tools (2:3.0.6-1) ...

Selectingpreviously unselected package redis-server.

Preparing tounpack .../redis-server_2%3a3.0.6-1_amd64.deb ...

Unpackingredis-server (2:3.0.6-1) ...

Processingtriggers for libc-bin (2.23-0ubuntu9) ...

Processingtriggers for systemd (229-4ubuntu17) ...

Setting uplibjemalloc1 (3.6.0-9ubuntu1) ...

Setting upredis-tools (2:3.0.6-1) ...

Setting upredis-server (2:3.0.6-1) ...

invoke-rc.d: couldnot determine current runlevel

invoke-rc.d:policy-rc.d denied execution of start.

Processingtriggers for libc-bin (2.23-0ubuntu9) ...

Processingtriggers for systemd (229-4ubuntu17) ...

 ---> 03255407f165

Removingintermediate container 8ac0a93f0a78

Step 6/8 : EXPOSE6379

 ---> Running in 22affc02406e

 ---> 7df7ea0b1eea

Removingintermediate container 22affc02406e

Step 7/8 :ENTRYPOINT /usr/bin/redis-server

 ---> Running in 3b0adf0c5f07

 ---> 97b2948892ed

Removingintermediate container 3b0adf0c5f07

Step 8/8 : CMD

 ---> Running in ac3178a5bbf2

 ---> 1b4ec4a60516

Removingintermediate container ac3178a5bbf2

Successfully built1b4ec4a60516

Successfullytagged ubuntu/redis:1

[root@dockerWEBrickDocker]#

[root@dockerWEBrickDocker]# docker images

REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE

ubuntu/redis                  1                   1b4ec4a60516        40 minutes ago      160MB

ubuntu/sinatra                1                   4d46ed419d3d        2 hours ago         386MB

unbuntu/nginx                 1                   d39894d30c41        6 hours ago         214MB

 

运行一个容器

docker run -d -p6379 --name redis1 ubuntu/redis:1

[root@docker ~]#docker port redis1

6379/tcp ->0.0.0.0:32769

 

切换到Ubuntu系统,centos安装redis-tools比较麻烦,测试效果

huisebug@client:~$sudo apt-get -y install redis-tools

huisebug@client:~$

huisebug@client:~$redis-cli -h 192.168.100.60 -p 32769

192.168.100.60:32769>

 

Docker中网卡信息

Docker0:docker所有容器的网关地址

 

[root@docker ~]#ip a show docker0

5: docker0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP

    link/ether 02:42:ad:31:9b:d8 brdff:ff:ff:ff:ff:ff

    inet 172.17.0.1/16 scope global docker0

       valid_lft forever preferred_lft forever

    inet6 fe80::42:adff:fe31:9bd8/64 scope link

       valid_lft forever preferred_lft forever

 

docker中容器新建的网卡veth开头的网卡

[root@docker ~]#ip a

1: lo:<LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

    link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: ens33:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

    link/ether 00:0c:29:d8:b9:40 brdff:ff:ff:ff:ff:ff

    inet 192.168.100.60/24 brd 192.168.100.255scope global ens33

       valid_lft forever preferred_lft forever

    inet6 fe80::5bdd:8f38:a7fc:ff74/64 scopelink

       valid_lft forever preferred_lft forever

3: virbr0:<NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWNqlen 1000

    link/ether 52:54:00:ce:7e:7d brdff:ff:ff:ff:ff:ff

    inet 192.168.122.1/24 brd 192.168.122.255scope global virbr0

       valid_lft forever preferred_lft forever

4: virbr0-nic:<BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWNqlen 1000

    link/ether 52:54:00:ce:7e:7d brdff:ff:ff:ff:ff:ff

5: docker0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP

    link/ether 02:42:ad:31:9b:d8 brdff:ff:ff:ff:ff:ff

    inet 172.17.0.1/16 scope global docker0

       valid_lft forever preferred_lft forever

    inet6 fe80::42:adff:fe31:9bd8/64 scope link

       valid_lft forever preferred_lft forever

61:veth94ac648@if60: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscnoqueue master docker0 state UP

    link/ether e6:bc:dd:c8:d2:f6 brdff:ff:ff:ff:ff:ff link-netnsid 0

    inet6 fe80::e4bc:ddff:fec8:d2f6/64 scopelink

       valid_lft forever preferred_lft forever

65:vethfaff039@if64: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscnoqueue master docker0 state UP

    link/ether 0e:14:2e:d0:9d:bb brdff:ff:ff:ff:ff:ff link-netnsid 1

    inet6 fe80::c14:2eff:fed0:9dbb/64 scopelink

       valid_lft forever preferred_lft forever

77:vethc99ae6d@if76: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscnoqueue master docker0 state UP

   link/ether e6:e0:5c:84:6a:13 brd ff:ff:ff:ff:ff:ff link-netnsid 2

    inet6 fe80::e4e0:5cff:fe84:6a13/64 scopelink

       valid_lft forever preferred_lft forever

87:veth23033e9@if86: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscnoqueue master docker0 state UP

    link/ether 0a:46:9a:4c:af:51 brdff:ff:ff:ff:ff:ff link-netnsid 3

    inet6 fe80::846:9aff:fe4c:af51/64 scopelink

       valid_lft forever preferred_lft forever

95:vethdb86bd5@if94: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueuemaster docker0 state UP

    link/ether c2:ff:12:93:f0:9b brdff:ff:ff:ff:ff:ff link-netnsid 4

    inet6 fe80::c0ff:12ff:fe93:f09b/64 scopelink

       valid_lft forever preferred_lft forever

[root@docker ~]#

 

使用link功能实现容器互联

新建一个没有开放端口的redis服务器

[root@docker ~]#docker run -d --name redis2 ubuntu/redis:1

a817a181b937f2231d9b0b86514644611c98ae16a1cb072eb7ab3ad9e3c0f56d

[root@docker ~]#docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                     NAMES

a817a181b937        ubuntu/redis:1      "/usr/bin/redis-se..."   2 minutes ago       Up 2 minutes                6379/tcp                  redis2

688249a9e083        ubuntu/redis:1      "/usr/bin/redis-se..."   5 hours ago         Up 6 minutes                0.0.0.0:32771->6379/tcp   redis1

717d73d8018e        a1ffe5d0754e        "/bin/sh -c 'apt-g..."   22 hours ago        Exited (100) 22 hours ago                             lucid_stallman

91ae0c650ce9        a1ffe5d0754e        "/bin/sh -c 'apt-g..."   22 hours ago        Exited (100) 22 hours ago                             romantic_blackwell

94e7bb109ea2        ubuntu/sinatra:1    "/opt/webapp/bin/w..."   23 hours ago        Up 23 hours                 0.0.0.0:32768->4567/tcp   webapp

9e1a39356c04        unbuntu/nginx:1     "nginx"                  27 hours ago        Up 27 hours                 0.0.0.0:8777->80/tcp      nginx1

f9d66082594d        registry            "/entrypoint.sh/e..."   47 hours ago        Up 46 hours                 0.0.0.0:5000->5000/tcp    reg

67346d341ef0        test/test:1         "/usr/sbin/apache2..."   2 days ago          Up 2 days                   0.0.0.0:9977->80/tcp      test1

新建一个应用程序容器并关联redis服务器

进入存在webapp这个文件的路径

 [root@docker ~]# cd WEBrickDocker/

 [root@docker WEBrickDocker]# ls

Dockerfile  Dockerfilewebapp  robots.txt webapp

[root@dockerWEBrickDocker]# docker run -p 4567 --name webapp2 --link redis2:db -t -i -v$PWD/webapp:/opt/webapp ubuntu/sinatra:1 /bin/bash

root@2db78cce3a9a:/#

 

redis2服务器并没有将端口开放出来

[root@docker ~]#docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES

2db78cce3a9a        ubuntu/sinatra:1    "/bin/bash"              6 minutes ago       Up 6 minutes        0.0.0.0:32772->4567/tcp   webapp2

a817a181b937        ubuntu/redis:1      "/usr/bin/redis-se..."   12 minutes ago      Up 12 minutes       6379/tcp                  redis2

688249a9e083        ubuntu/redis:1      "/usr/bin/redis-se..."   5 hours ago         Up 17 minutes       0.0.0.0:32771->6379/tcp   redis1

 

在容器中可以查看连接信息

可以看见redis服务器的IP地址,连接后的别名 容器ID 容器名

root@2db78cce3a9a:/#cat /etc/hosts

127.0.0.1 localhost

::1 localhost ip6-localhost ip6-loopback

fe00::0    ip6-localnet

ff00::0 ip6-mcastprefix

ff02::1 ip6-allnodes

ff02::2 ip6-allrouters

172.17.0.7   db a817a181b937 redis2

172.17.0.8   2db78cce3a9a

 

使用ping来测试

root@2db78cce3a9a:/#apt-get install inetutils-ping

Reading packagelists... Done

Buildingdependency tree      

Reading stateinformation... Done

The following NEWpackages will be installed:

  inetutils-ping

0 upgraded, 1newly installed, 0 to remove and 8 not upgraded.

Need to get 56.9kB of archives.

After thisoperation, 338 kB of additional disk space will be used.

Get:1http://archive.ubuntu.com/ubuntu xenial/universe amd64 inetutils-ping amd642:1.9.4-1build1 [56.9 kB]

Fetched 56.9 kB in6s (8289 B/s)                                                                                                   

debconf: delayingpackage configuration, since apt-utils is not installed

Selectingpreviously unselected package inetutils-ping.

(Reading database... 13996 files and directories currently installed.)

Preparing tounpack .../inetutils-ping_2%3a1.9.4-1build1_amd64.deb ...

Unpackinginetutils-ping (2:1.9.4-1build1) ...

Setting upinetutils-ping (2:1.9.4-1build1) ...

root@2db78cce3a9a:/#ping db

PING db(172.17.0.7): 56 data bytes

64 bytes from172.17.0.7: icmp_seq=0 ttl=64 time=0.146 ms

64 bytes from172.17.0.7: icmp_seq=1 ttl=64 time=0.131 ms

64 bytes from172.17.0.7: icmp_seq=2 ttl=64 time=0.093 ms

^C--- db pingstatistics ---

3 packetstransmitted, 3 packets received, 0% packet loss

round-tripmin/avg/max/stddev = 0.093/0.123/0.146/0.000 ms

 

 

env查看变量

 

 

后台运行webapp程序

还要修改,无法生效

root@2db78cce3a9a:~#vim /opt/webapp/lib/app.rb

 

require"rubygems"

require"sinatra"

require"json"

require"uri"

classApp < Sinatra::Application

 

  set :bind, '0.0.0.0'

 

  get '/' do

    "<h1>DockerBook Test Sinatraapp</h1>"

  end

 

  post '/json/?' do

    params.to_json

  end

 

end

uri=URI.parse(ENV['DB_PORT'])

redis =Redis.new(:host =>uri.host, :port => uri.port)

~                                                            

root@2db78cce3a9a:~#

root@2db78cce3a9a:~#nohup /opt/webapp/bin/webapp &

[1] 68

root@2db78cce3a9a:~#nohup: ignoring input and appending output to 'nohup.out'

^C

root@2db78cce3a9a:~#

 

Docker结合Jenkins

 [root@docker ~]# mkdir JenkinsDocker

[root@docker ~]#cd JenkinsDocker/

 

 

(yi)也是可以从官方搜索镜像

 [root@dockerJenkinsDocker]# docker search jenkins

NAME                                 DESCRIPTION                                     STARS     OFFICIAL  AUTOMATED

jenkins                               Official JenkinsDocker image                   2925      [OK]      

stephenreed/jenkins-java8-maven-git   Automated build that provides acontinuous...   64                   [OK]

killercentury/jenkins-dind            Generic Jenkins CI with DockerEngine and ...   26                   [OK]

aespinosa/jenkins                     Sets up a container withjenkins installed...   22                   [OK]

cloudbees/jenkins-enterprise          CloudBees Jenkins Enterprise (Rollingrele...   22                   [OK]

appcontainers/jenkins                 Centos/Debian/Ubuntu BasedCustomizable Je...   9                    [OK]

blacklabelops/jenkins                 Docker Jenkins Swarm-Readywith HTTPS and ...   9                    [OK]

killercentury/jenkins-slave-dind      Generic Jenkins Slave with Docker Enginea...   8                    [OK]

fabric8/jenkins-docker                Fabric8 Jenkins DockerImage                    7                    [OK]

tianon/jenkins-slave                  SSHd, Java, andDocker-in-Docker specifica...   7                    [OK]

bitnami/jenkins                       Bitnami Docker Image forJenkins                6                    [OK]

publicisworldwide/jenkins-slave       Jenkins Slave based on Oracle Linux             4                    [OK]

blacklabelops/jenkins-swarm           Jenkins Swarm Slave Dockerized andParamet...   4                    [OK]

eeacms/jenkins-slave                  Jenkins slave ready to runDocker image (J...   3                    [OK]

mrbobbytables/jenkins                 Jenkins CI container withMesos integration     1                    [OK]

baselibrary/jenkins                   ThoughtWorks Docker Image:jenkins              1                    [OK]

marcelhuberfoo/jenkins                jenkins build server dockerimage               1                    [OK]

foxylion/jenkins                      Jenkins 2 with automatedsetup to support ...   1                    [OK]

enieuw/jenkins                        Jenkins                                         1                    [OK]

openfrontier/jenkins                  The official Jenkins dockerplus some plug...   0                    [OK]

unox/jenkins                          Jenkins, with thejenkins user added to th...   0                    [OK]

mmerrick/jenkins                      Added dependencies to Jenkins                   0                    [OK]

garethjevans/jenkins                  Jenkins docker image withadditional plugins    0                    [OK]

dockersolutions/jenkins               Jenkins image used for SE demos                 0                    [OK]

virtuasphere/jenkins                  jenkins                                         0                    [OK]

 

 

(er)编写构建

解决问题:

#下面的wget是我后来更改的,起初用的是curl,总是无法下载gpg这个文件下来,还要研究解决,curl -o gpg这个有时候可以下载下来

#起初使用的apt-get openjdk-6-jdk,因为从网上拉取的jenkins是最新版,所以要更新支持java最新,用java6无法运行,apt-get openjdk-8-jdk也没有这个包,所以修改为使用压缩包形式安装java,注意ADD命令是不会重命名的

 

所需包和Dockerfile内容

[root@dockerJenkinsDocker]# ls

Dockerfile  dockerjenkins.sh  jdk-8u111-linux-x64.tar.gz

[root@dockerJenkinsDocker]# vim Dockerfile

 

FROM ubuntu:14.04

MAINTAINERhuisebug@outlook.com

ENV REFRESHED_AT2017-06-30

 

ADD./jdk-8u111-linux-x64.tar.gz /usr/local/

RUN mv/usr/local/jdk1.8.0_111  /usr/local/java

ENVJAVA_HOME=/usr/local/java

ENV PATH=$PATH:$JAVA_HOME/bin

RUN bash

 

RUN sed -is@/archive.ubuntu.com/@/mirrors.163.com/@g /etc/apt/sources.list

RUN apt-get clean

 

RUN apt-get update-qq

RUN apt-get -yinstall wget

RUN wgethttps://get.docker.io/gpg

RUN apt-key addgpg

RUN apt-getinstall -y apt-transport-https

RUN echo debhttps://get.docker.io/ubuntu docker main >/etc/apt/sources.list.d/docker.list

RUN apt-get update

RUN apt-getinstall -y iptables ca-certificates lxc git-core lxc-docker curl

 

ENV JENKINS_HOME/opt/jenkins/date

ENV JENKINS_MIRRORhttp://mirrors.jenkins-ci.org

 

RUN mkdir -p$JENKINS_HOME/plugins

RUN curl -sf -o/opt/jenkins/jenkins.war -L $JENKINS_MIRROR/war-stable/latest/jenkins.war

 

RUN for plugin inchucknorris greenballs scm-api git-client git ws-cleanup ;\

    do curl -sf -o$JENKINS_HOME/plugins/${plugin}.hpi \

       -L$JENKINS_MIRROR/plugins/${plugin}/latest/${plugin}.hpi ;\

     done

ADD./dockerjenkins.sh /usr/local/bin/dockerjenkins.sh

RUN chmod +x/usr/local/bin/dockerjenkins.sh

 

VOLUME/var/lib/docker

 

EXPOSE 8080

ENTRYPOINT["/usr/local/bin/dockerjenkins.sh"]

 

首先,他设置了Ubuntu环境,加入了需要的Docker APT仓库,并加入了对应的GPG key。之后更新了包列表,并安装了Docker和Jenkins所需的包,加入了一些Jenkins需要的包

 

创建了/opt/jenkins目录,并把最新稳定版本的Jenkins下载都这个目录。还需要一些Jenkins插件,给Jenkins提供额外的功能

 

使用ENV指令吧JENKINS_HOME和JENKINS_MIRROR环境变量设置为Jenkins的数据目录和镜像站点

 

指定了VOLUME 指令,VOLUME指令从容器运行的宿主机上挂载一个卷,在这里,为了“骗过”Docker,指定/var/lib/docker作为卷,这是因为/var/lib/docker 目录是Docker用来存贮其容器的目录。这个位置必须是真实的文件系统,而不能是像Docker镜像层那种挂载点。

 

使用VOLUME 指令该诉Docker进程,在容器运行内部使用宿主机的文件系统,而不能是像Docker镜像层那种挂载点。

 

那么,我们使用VOLUME指令告诉Docker进程,在容器运行内部使用宿主机的文件系统作为容器的存储,这样,容器内嵌Docker的/var/lib/docker目录将保存在宿主机系统的/var/lib/docker/volumes目录下的某个位置

 

公开jenkins的8080端口

 

#先把启动脚本下载下来

[root@dockerJenkinsDocker]# wget https://dockerbook.com/code/5/jenkins/dockerjenkins.sh

构建

[root@dockerJenkinsDocker]# docker build -t "ubuntu/jenkins:1" .

 

创建容器

 

[root@dockerJenkinsDocker]# docker run -p 8080:8080 -d --name "jenkins"--privileged ubuntu/jenkins:1

79e645e40f228708b03b9d3f45d5af15243902edc3025d1098e61a135a4c7b75

[root@dockerJenkinsDocker]#

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!