Andrzej Gis
Andrzej Gis

Reputation: 14306

Access Redis CLI inside a Docker container

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?

Upvotes: 59

Views: 80405

Answers (6)

Andrew Allbright
Andrew Allbright

Reputation: 11

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.

Upvotes: 1

rupweb
rupweb

Reputation: 3328

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

Upvotes: 2

infiniteshi
infiniteshi

Reputation: 151

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

Upvotes: 2

Faruk AK
Faruk AK

Reputation: 339

  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

Upvotes: 30

Rafiq
Rafiq

Reputation: 11445

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.

Upvotes: 12

Ortomala Lokni
Ortomala Lokni

Reputation: 62456

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

Upvotes: 101

Related Questions