Accessing docker host from (jenkins) docker contai

2020-03-29 01:16发布

问题:

I need to run docker commands from jenkins which is installed as a container on docker. My local setup is on an OSX and I use boot2docker to virtualize the docker machine.

I have installed jenkins on docker by simply docker run -d -p 8080:8080 --name jenkins jenkins and it is running fine.

On jenkins I have installed "Docker plugin" https://wiki.jenkins-ci.org/display/JENKINS/Docker+Plugin which needs DOCKER URL in order to access docker api.

When boot2docker starts i get the following: DOCKER_HOST=tcp://192.168.59.103:2376, so I assume docker api is running on that host/por?

On jenkins I set up the DOCKER URL field as http://192.168.59.103:2376 but I get the following error "shaded.org.apache.http.client.ClientProtocolException".

It seems that the container cannot access boot2docker docker server. Maybe I'm missing something but I am not able to figure out what is the correct ip/port I have to use.

Update: More Details

This is what I get when I start boot2docker:

bash-3.2$ unset DYLD_LIBRARY_PATH ; unset LD_LIBRARY_PATH
bash-3.2$ mkdir -p ~/.boot2docker
bash-3.2$ if [ ! -f ~/.boot2docker/boot2docker.iso ]; then cp /usr/local/share/boot2docker/boot2docker.iso ~/.boot2docker/ ; fi
bash-3.2$ /usr/local/bin/boot2docker init 

  WARNING: The 'boot2docker' command line interface is officially deprecated.

  Please switch to Docker Machine (https://docs.docker.com/machine/) ASAP.

  Docker Toolbox (https://docker.com/toolbox) is the recommended install method.

Virtual machine boot2docker-vm already exists
bash-3.2$ /usr/local/bin/boot2docker up 

  WARNING: The 'boot2docker' command line interface is officially deprecated.

  Please switch to Docker Machine (https://docs.docker.com/machine/) ASAP.

  Docker Toolbox (https://docker.com/toolbox) is the recommended install method.

Waiting for VM and Docker daemon to start...
...............oooooooo
Started.
Writing /Users/local/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/local/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/local/.boot2docker/certs/boot2docker-vm/key.pem

To connect the Docker client to the Docker daemon, please set:
    export DOCKER_TLS_VERIFY=1
    export DOCKER_HOST=tcp://192.168.59.103:2376
    export DOCKER_CERT_PATH=/Users/local/.boot2docker/certs/boot2docker-vm

Or run: `eval "$(boot2docker shellinit)"`

bash-3.2$ $(/usr/local/bin/boot2docker shellinit)
Writing /Users/local/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/local/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/local/.boot2docker/certs/boot2docker-vm/key.pem
bash-3.2$ docker version
Client:
 Version:      1.8.0
 API version:  1.20
 Go version:   go1.4.2
 Git commit:   0d03096
 Built:        Tue Aug 11 17:17:40 UTC 2015
 OS/Arch:      darwin/amd64

Server:
 Version:      1.8.0
 API version:  1.20
 Go version:   go1.4.2
 Git commit:   0d03096
 Built:        Tue Aug 11 17:17:40 UTC 2015
 OS/Arch:      linux/amd64

Here goes the jenkins configuration for the Cloud:

回答1:

As @ISanych suggests you can simply do -v /var/run/docker.sock:/var/run/docker.sock and it will also magically work on boot2docker. No need to define DOCKER_URL.

You might also find --net=host useful if need to access the ports of the started containers.



回答2:

I ran into exact same problem with my Jenkins docker plugin. Docker uses tls by default but docker plugin only supports http. What I did is disabling TLS verification on the docker machine. My docker machine is an Ubuntu so the docker conf file is under /etc/default/docker. Inside the conf file, you can disable TLS by adding

--tls=false 

in DOCKER_OPTS. Something like:

DOCKER_OPTS='-H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock --tls=false'


回答3:

This should probably be a comment on the previous answer, but that does not seem possible. This is just a note to remind folks that listening on 0.0.0.0 means listening on whatever publicly routable interfaces might be connected to any physical or virtual network interface configured on your system. Prudence suggests limiting your exposure to only an internal network, less likely to encounter hostile forces with ill intent.