0

Trying to understand what's happening here:

root@mordor:~# 
root@mordor:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
root@mordor:~# 
root@mordor:~# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
10d38d6f1fa1        bridge              bridge              local
5b5fde2dd7cc        foo                 bridge              local
1a7946d0b10c        host                host                local
0cbbb6d4f486        none                null                local
root@mordor:~# 
root@mordor:~# docker run --name rd --network foo -d redis redis-server --appendonly yes
ea782770bcff816a642bc700abd2fadd524e3eeca10820e4c158ae4e72eda139
root@mordor:~# 
root@mordor:~# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
ea782770bcff        redis               "docker-entrypoint.s…"   42 seconds ago      Up 40 seconds       6379/tcp            rd
root@mordor:~# 
root@mordor:~# docker run -it --network foo --rm redis redis-cli -h rd
rd:6379> 
rd:6379> ping
PONG
rd:6379> 
rd:6379> set foo bar
OK
rd:6379> 
rd:6379> get foo
"bar"
rd:6379> 
rd:6379> keys *
1) "foo"
rd:6379> 

I can see how to spin up the community version of redis, I only added the foo network. But why is the connection with run rather than exec?

2

In short, docker exec is used to operate an already-running container, while docker run -it starts a new container interactively.

When you ran docker run -it --network foo --rm redis redis-cli -h rd you created a 2nd container, this time using the redis-cli image, which was used to query your running redis container.

Were you to repeat the steps you took, and then open another terminal and run docker container ls, you would see two running containers.

Reference: https://spin.atomicobject.com/2018/10/04/docker-command-line/

1
  • 1
    docker ps (or docker ps -a if it's terminated) would also show this information. Oct 6 '20 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.