简体   繁体   English

如何使用spotify docker client连接到主机上的docker守护程序

[英]How to connect to the docker daemon on the host using spotify docker client

I want to run a Java Spring application inside of a docker container and this application should be able to deploy sibling containers. 我想在docker容器中运行Java Spring应用程序,这个应用程序应该能够部署兄弟容器。 When I run the Java application directly on my machine it works fine and can deploy containers, but as soon as I try to run the application inside a container it does not work any more (im using supervisord to run the mongodb and Java Spring app in one container and I know that thats not best practice). 当我直接在我的机器上运行Java应用程序时它工作正常并且可以部署容器,但是当我尝试在容器内运行应用程序时它就不再起作用了(我使用supervisord来运行mongodb和Java Spring应用程序)一个容器,我知道这不是最好的做法)。 The container starts up fine, but crashes as soon as my application tries to connect to the docker deamon without any stacktraces from Java, just the error WARN received SIGTERM indicating exit request . 容器启动正常,但是一旦我的应用程序尝试连接到docker守护程序而没有来自Java的任何堆栈跟踪就崩溃,只是错误WARN received SIGTERM indicating exit request The supervisord logs dont contain additional info. supervisord日志不包含其他信息。

I tried mounting the docker socket from the host (Windows 10 Pro with Docker Desktop, also tried Ubuntu Server 18.04) into the container using -v /var/run/docker.sock:/var/run/docker.sock . 我尝试使用-v /var/run/docker.sock:/var/run/docker.sock将Docker套接字从主机(带有Docker Desktop的Windows 10 Pro,也尝试使用Ubuntu Server 18.04)安装到容器中。 I also tried to use --net="host" . 我也尝试使用--net="host" Both did not work, although with the second one the container does not crash but produces a different error ( {}->unix://localhost:80: Connection refused ) visible in the log of my java application, which indicates that it cant even find the right address for the deamon. 两者都不起作用,虽然第二个容器没有崩溃但产生不同的错误( {}->unix://localhost:80: Connection refused )在我的java应用程序的日志中可见,这表明它不能甚至找到了deamon的正确地址。 I also activated "Expose deamon on tcp://localhost:2375 without TLS". 我还激活了“在没有TLS的tcp:// localhost:2375上暴露deamon”。 I also tried to set the DOCKER_HOST environment variable inside the container to default values such as "tcp://localhost:2375" or "/var/run/docker.sock". 我还尝试将容器内的DOCKER_HOST环境变量设置为默认值,例如“tcp:// localhost:2375”或“/var/run/docker.sock”。

Here is the code that I use to initialize the docker client. 这是我用来初始化docker客户端的代码。

DockerClient docker = DefaultDockerClient.fromEnv().build();

The DefaultDockerClient.fromEnv().build(); DefaultDockerClient.fromEnv().build(); should create a docker client that uses the DOCKER_HOST environment variable to connect to the host or the default adress ("/var/run/docker.sock" on *NIX). 应创建一个docker客户端,它使用DOCKER_HOST环境变量连接到主机或默认地址(* NIX上的“/var/run/docker.sock”)。

Here is my DOCKERFILE: 这是我的DOCKERFILE:

FROM    openjdk:8-jre-alpine

ENV     PACKAGES mongodb supervisor

VOLUME  /opt/server
VOLUME  /data/db
WORKDIR /opt/accservermanager

ADD     supervisord.conf /etc/supervisor.conf
ADD     accservermanager.jar /opt/accservermanager/accservermanager.jar
ADD     application.properties /opt/accservermanager/application.properties

RUN     apk update && \
        apk add --update $PACKAGES --no-cache && \
        rm -rf /var/cache/apk/*

EXPOSE  8000

CMD     ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor.conf"]

And finally, my supervisord.conf 最后,我的supervisord.conf

[supervisord]
user=root
stderr_logfile=/var/log/supervisord.err.log
stdout_logfile=/var/log/supervisord.out.log
loglevel=debug

[program:mongodb]
command=mongod --smallfiles
autostart=true
autorestart=true
stderr_logfile=/var/log/mongo.err.log
stdout_logfile=/var/log/mongo.out.log

[program:accservermanager]
directory=/opt/accservermanager/
command=java -jar accservermanager.jar
autostart=true
autorestart=true
stderr_logfile=/var/log/accservermanager.err.log
stdout_logfile=/var/log/accservermanager.out.log

Expected result: Application connects to the docker client from the host and is able to deploy/manage containers on the host 预期结果:应用程序从主机连接到docker客户端,并且能够在主机上部署/管理容器

Actual result: Container crashes or outputs errors. 实际结果:容器崩溃或输出错误。

Turns out that there is a new version of spotify-docker that fixes my problem. 事实证明,有一个新版本的spotify-docker解决了我的问题。 Updating from v8.15.1 to v8.15.2 solved my issue. v8.15.1更新到v8.15.2解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM