如何从一个Docker容器执行命令到另一个 [英] How to execute command from one docker container to another

查看:520
本文介绍了如何从一个Docker容器执行命令到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,该应用程序将允许用户上传视频文件,然后再进行一些处理。

I'm creating an application that will allow users to upload video files that will then be put through some processing.

我有两个容器。


  1. Nginx 容器,该容器可为网站提供用户上传视频文件的服务。

  2. 已安装 FFmpeg 和其他一些处理内容的视频处理容器。

  1. Nginx container that serves the website where users can upload their video files.
  2. Video processing container that has FFmpeg and some other processing stuff installed.

我想要实现的目标。我需要容器1才能在容器2上运行bash脚本。

What I want to achieve. I need container 1 to be able to run a bash script on container 2.

据我所知,一种可能性是使它们通过API通过HTTP进行通信。但是然后我需要在容器2中安装Web服务器并编写一个API,这似乎有些过分了。
我只想执行一个bash脚本。

One possibility as far as I can see is to make them communicate over HTTP via an API. But then I would need to install a web server in container 2 and write an API which seems a bit overkill. I just want to execute a bash script.

有什么建议吗?

推荐答案

您有一些选择,但是前两个是:

You have a few options, but the first 2 that come time mind are:


  1. 在容器1中,安装Docker CLI并绑定挂载
    /var/run/docker.sock (您需要在启动容器时从
    主机指定绑定安装)。然后,在容器内,您
    应该能够对绑定安装的
    套接字使用 docker 命令,就像您从主机上执行它们一样(您可能还需要
    容器内的套接字 chmod ,以允许非root用户
    用户执行此操作。

  2. 您可以在容器2上安装 SSHD ,然后在容器1中安装 ssh 并运行脚本。这样做的好处是您无需对容器内部进行任何更改即可说明它们在Docker中运行而不是在裸机上运行的缺点是,您需要将SSHD设置添加到Dockerfile或

  1. In container 1, install the Docker CLI and bind mount /var/run/docker.sock (you need to specify the bind mount from the host when you start the container). Then, inside the container, you should be able to use docker commands against the bind mounted socket as if you were executing them from the host (you might also need to chmod the socket inside the container to allow a non-root user to do this.
  2. You could install SSHD on container 2, and then ssh in from container 1 and run your script. The advantage here is that you don't need to make any changes inside the containers to account for the fact that they are running in Docker and not bare metal. The down side is that you will need to add the SSHD setup to your Dockerfile or the startup scripts.

我能想到的大多数其他想法只是选项(2)的变体,其中SSHD被某些替换其他工具。

Most of the other ideas I can think of are just variants of option (2), with SSHD replaced by some other tool.

还请注意,Docker网络有点奇怪(至少在Mac主机上如此),因此您需要确保容器使用的是同一个docker -净工作并能够通过它进行交流。

Also be aware that Docker networking is a little strange (at least on Mac hosts), so you need to make sure that the containers are using the same docker-network and are able to communicate over it.

这篇关于如何从一个Docker容器执行命令到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆