doutang2017
doutang2017
2018-04-27 16:28
浏览 488

在另一个容器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.

图片转代码服务由CSDN问答提供 功能建议

我有2个容器:Redis容器和Golang应用程序的容器。 我的Golang应用程序正在尝试执行Redis批量插入( https://redis.io/topics/mass-insert ),方法是使用命令 cat $ {FILE} |执行bash脚本 redis-cli --pipe -h $ {HOST}

但是,在PATH系统变量中找不到 redis-cli ,它不是Golang应用程序服务中的内置shell命令。

因此,我得到一个退出状态127 ,这意味着未找到给定的命令。 我想知道在Golang应用程序服务中没有 redis-cli 命令的情况下如何能够执行bash脚本。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douaonong7807
    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}`
    
    点赞 评论
  • doushang9172
    doushang9172 2018-04-27 17:09

    When you build your application docker image make sure you have install redis client and bash, Depending on base image you may use apk add bash, or apt-get install -y bash. For instance if you are using alpine base image your Docker file might look like:

    Dockerfile

    FROM golang:1.10.1-alpine3.7
    
    RUN apk add --no-cache ca-certificates bash redis
    RUN apk add --update tzdata curl && rm -rf /var/cache/apk/*
    ADD myapp /
    CMD ["myapp"]
    
    点赞 评论

相关推荐