<p>In short, <code>docker exec</code> is used to operate an already-running container, while <code>docker run -it</code> starts a new container interactively.</p>
<p>When you ran <code>docker run -it --network foo --rm redis redis-cli -h rd</code> you created a 2nd container, this time using the <code>redis-cli</code> image, which was used to query your running <code>redis</code> container.</p>
<p>Were you to repeat the steps you took, and then open another terminal and run <code>docker container ls</code>, you would see two running containers.</p>
<p>Reference: <a href="https://spin.atomicobject.com/2018/10/04/docker-command-line/" rel="nofollow noreferrer">https://spin.atomicobject.com/2018/10/04/docker-command-line/</a></p>

CC BY-SA 4.0
2020-10-06T16:01:15.737Z
`docker ps` (or `docker ps -a` if it's terminated) would also show this information.
Danny Staple
CC BY-SA 4.0
2020-09-29T16:52:06.153Z
2020-09-29T16:52:06.153Z
Woodland
<p>Trying to understand what's happening here:</p>
<pre><code>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               &quot;docker-entrypoint.s…&quot;   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&gt; 
rd:6379&gt; ping
PONG
rd:6379&gt; 
rd:6379&gt; set foo bar
OK
rd:6379&gt; 
rd:6379&gt; get foo
&quot;bar&quot;
rd:6379&gt; 
rd:6379&gt; keys *
1) &quot;foo&quot;
rd:6379&gt; 
</code></pre>
<p>I can see how to spin up the <a href="https://hub.docker.com/_/redis" rel="nofollow noreferrer">community</a> version of <code>redis</code>, I only added the <code>foo</code> network.  But why is the connection with <code>run</code> rather than <code>exec</code>?</p>

<docker><networking><databases><configuration><redis>
why is "docker run" used to connect to redis?
Nicholas Saunders
# why is "docker run" used to connect to redis?

(Asked by: username_0 on 2020-09-29)

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?

**Tags:** docker, networking, databases, configuration, redis


---

## 1 answers

### Answer by username_1 on 2020-09-29. Score: 2 | Accepted answer

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/>

> **Comments:**
> **username_2**: `docker ps` (or `docker ps -a` if it's terminated) would also show this information.  

---

