Access Redis CLI inside a Docker container
Asked Answered
Q

6

55

I have Redis running inside of a docker container.

docker run --rm -d --name "my_redis" redis

I'd like to access it via CLI:

If I run docker exec -it my_redis redis-cli the console becomes unresponsive until I leave the container (Ctrl + P, Ctrl + Q)

C:\Users\Andrzej>docker exec -it my_redis redis-cli
// nothing here until I go Ctrl + P, Ctrl + Q
exec attach failed: error on attach stdin: read escape sequence
C:\Users\Andrzej>

If I run docker exec -it my_redis sh and then run redis-cli from inside of the container it works.

C:\Users\Andrzej>docker exec -it my_redis sh
# redis-cli
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379>

My OS is Windows 10.

Is there any way to fix docker exec -it my_redis redis-cli behavior?

UPDATE

When the console gets unresponsive and I click "arrow up" key exactly 11 times I get the Redis cli. This is 100% reproducible. What kind of voodoo magic is that?

Quantify answered 15/1, 2019 at 19:33 Comment(2)
Try another terminal such as conemu.github.ioBlowtorch
@OrtomalaLokni Still the same (cmd, powershell, mingw)Quantify
B
96

Run a redis container in detached mode:

docker run -d redis

Run redis-cli on it:

docker exec -it e0c061a5700bfa400f8f24b redis-cli

where e0c061a5700bfa400f8f24b is the id of the container.

According to the documentation:

Detached (-d)

To start a container in detached mode, you use -d=true or just -d option. By design, containers started in detached mode exit when the root process used to run the container exits, unless you also specify the --rm option. If you use -d with --rm, the container is removed when it exits or when the daemon exits, whichever happens first.

.

--interactive , -i Keep STDIN open even if not attached

--tty , -t Allocate a pseudo-TTY

Blowtorch answered 15/1, 2019 at 19:55 Comment(2)
I should have mentioned how I instantiate my container. It is detached indeed. Please see the update.Quantify
This command works in (Laravel) Sail as wellRaspings
H
27
  1. Run redis container at 6379 port with the name redis in detached mode.

docker run --name redis -p 6379:6379 -d redis

  1. Run the redis-cli command in the container.

docker exec -it redis redis-cli

Hautegaronne answered 8/8, 2021 at 8:17 Comment(1)
Code-only answers are generally not recommended; you should also provide an explanation of what the code does to solve the problem, especially where there are other existing answers.Stogner
P
11

First of all a container can be a multi command container, which creates option of running CLI inside a container after creating it. enter image description here

If you want to start up the CLI you need to some how get in to the container and execute a second command. You need to start up a second program inside the container.

To run redis-cli into the container, you need to use another docker command-

enter image description here

exec is short for execute and we use it to execute an additional command inside of a container, so write down docker exec then put down dash IT.

-it argument allows us to type input directly into the container we then provide the container ID and the the command that we want to execute inside the container.

sudo docker exec -it container_id redis-cli

enter image description here

if you do not use -it, you will get kicked directly back, because redis CLI was started up but you did not get the ability to enter in any text.

Putter answered 21/3, 2021 at 20:20 Comment(0)
R
2

Yet another alternative approach in addition to the other answers:

docker exec -it <your_redis_container_id> sh

#redis-cli //run redis-cli following the flashing #

127.0.0.1:6379>your_redis_cli_commands ...

#exit //run exit when you need to exit redis-cli

Rothberg answered 6/5, 2022 at 23:58 Comment(0)
S
2

If you're running redis via Docker on Windows you can open a CLI like this

enter image description here

Then type redis-cli to get the redis client

enter image description here

Sapajou answered 14/10, 2022 at 10:46 Comment(0)
C
1

What if you want to keep the "redis-server" container separate from the "redis-cli" container? You'll have to use docker networking if you want the redis-cli container to be able to speak to the redis-server container.

docker network create redis-net
docker run -it --rm --network redis-net --name redis-server -d redis
docker run -it --rm --network redis-net redis redis-cli -h redis-server ping

Note: the --name (container name) docker flag value is used as the -h (redis host) redis-cli flag input.

Cluck answered 5/8, 2023 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.