The docker exec command allows you to interact with a running container or enter its shell. It is commonly used with the -itflags to enable interaction with the container’s input and output streams.
docker exec [options] container_id command
Example
In the following example, we will start by running a container with the redisimage. This will create a container that will initially run redis-server.
docker run redis# * monotonic clock: POSIX clock_gettime# * Running mode=standalone, port=6379.# * Server initialized# * Ready to accept connections tcp
To use the redis functionality, we need to run redis-cli in the container shell and its environment. To do this, we create another terminal and check the continer_id using docker ps. Then we use docker exec -it with the retrieved container_id to call redis-cli in the container.
docker ps# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES# 408ff5ce70cb redis "docker-entrypoint.s…" About a minute ago Up About a minute 6379/tcp nice_dhawandocker exec -it 408ff5ce70cb redis-cli127.0.0.1:6379> set number 1# OK127.0.0.1:6379> get number# "1"