doutang2017 2018-04-27 16:28
浏览 1778
已采纳

在另一个容器docker中执行另一个容器命令

I have 2 containers: a Redis container and a container for a Golang application. My Golang application is trying to perform Redis Mass Insertion (https://redis.io/topics/mass-insert) by executing a bash script with the command cat ${FILE}| redis-cli --pipe -h ${HOST}.

However, redis-cli is not found within the PATH system variable and it is not a built-in shell command in the Golang application service.

Hence, I get an exit status 127, which means the given command in not found. I would like to know how I would be able to execute the bash script without redis-cli command in the Golang application service.

  • 写回答

2条回答 默认 最新

  • douaonong7807 2018-04-27 17:39
    关注

    It is possible to run the command from within the application container only if you rebuild your go application image and install the redis cli tools. Example using an ubuntu based image (add this to your Dockerfile): RUN apt update; apt -y install redis-tools If you are using docker compose, you can talk to your redis server using the name specified in your docker compose file.

    cat ${FILE}| redis-cli --pipe -h redis-server

    Assuming you name your redis container as shown in this sample

    version: '3'
    services:
      redis-server:
        image: xxx
    [...]
    

    Alternatively, if you want to run the command from the host server, you'll have to make sure you are forwarding the redis port from the redis container, then you can use:

    docker exec {containerId} 'cat {FILE}' | redis-cli --pipe -h localhost:{REDIS_CONTAINER_PORT}`
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格