如何在正在运行的docker-compose容器中输入redis-server shell

时间:2017-02-07 07:13:26

标签: redis docker-compose

我想看看是否正在运行redis-server容器状态。所以我想在redis shell中执行info,看看redis slave是否与远程redis master同步。

我的docker-compose文件列出了redis,如下所示:

  placements-store:
    image: redis:3.0
    command: redis-server ${REDIS_OPTIONS}
    ports:
      - "6379:6379"

运行docker-compose ps我可以看到容器已启动并正在运行:

app_placements-store_1   docker-entrypoint.sh redis ...   Up      0.0.0.0:6379->6379/tcp 

我试图执行:

docker-compose run --rm redis-cli

得到了:

ERROR: No such service: redis-cli

我认为这是可以理解的,因为没有redis-cli容器。我试图标记正在运行的redis-server,但不知道该怎么做。

更新

我可以通过执行以下命令查看日志:

docker-compose logs -f --tail 500 placements-store

我确实得到了类似下面的信息,但我正在寻找更多可以从外部轻松控制的信息和其他信息:

placements-store_1  | 1:S 06 Feb 19:16:35.427 * Connecting to MASTER mo-api.mydomain.com:6379
placements-store_1  | 1:S 06 Feb 19:16:35.589 * MASTER <-> SLAVE sync started
placements-store_1  | 1:S 06 Feb 19:16:35.889 * Non blocking connect for SYNC fired the event.
placements-store_1  | 1:S 06 Feb 19:16:36.189 * Master replied to PING, replication can continue...
placements-store_1  | 1:S 06 Feb 19:16:36.790 * Partial resynchronization not possible (no cached master)
placements-store_1  | 1:S 06 Feb 19:16:37.091 * Full resync from master: 5ada1d8c65fd49d67d931bea66530a169ce83a40:29442
placements-store_1  | 1:S 06 Feb 19:16:37.145 * MASTER <-> SLAVE sync: receiving 60 bytes from master
placements-store_1  | 1:S 06 Feb 19:16:37.145 * MASTER <-> SLAVE sync: Flushing old data
placements-store_1  | 1:S 06 Feb 19:16:37.145 * MASTER <-> SLAVE sync: Loading DB in memory
placements-store_1  | 1:S 06 Feb 19:16:37.145 * MASTER <-> SLAVE sync: Finished with success 

3 个答案:

答案 0 :(得分:6)

您需要使用docker exec登录容器(如另一个答案所述 - 不确定命令是否100%正确,因为它可能只运行redis-cli然后退出)。

我会运行以下命令:

docker exec -it app_placements-store_1 sh

这将使您登录到容器。然后,您可以从命令提示符运行redis-cli

答案 1 :(得分:3)

使用docker exec在正在运行的容器中执行命令:

docker exec -it app_placements-store_1 redis-cli

答案 2 :(得分:0)

虽然已经给出了答案,但是我将更详细地解释码头工人有这些问题的新手的详细信息。

命令docker exec用于从机器的终端运行一个容器的命令。

您还可以通过执行命令docker exec --help

获取命令的详细帮助
Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container

Options:
  -d, --detach               Detached mode: run command in the background
  --detach-keys string   Override the key sequence for detaching a
                         container
  -e, --env list             Set environment variables
  --help                 Print usage
  **-i, --interactive          Keep STDIN open even if not attached**
  --privileged           Give extended privileges to the command
  **-t, --tty                  Allocate a pseudo-TTY**
  -u, --user string          Username or UID (format:
                         <name|uid>[:<group|gid>])

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]清楚地表明您需要CONTAINERCOMMAND以及参数。例如:docker exec -it app_placements-store_1 redis-cli

注意:我已经显示了&#39; -i and t as bold in the output of docker exec --help`,以便您了解我们使用这些选项的原因