The error message “Error: can only create exec sessions on running
containers: container state improper” typically occurs when you try to
execute a command inside a Docker container using docker exec but the
container is not in the correct state to accept commands. This usually
means that the container is not running.
# docker run -td --name test_image custom-docker-releases.repo3.cci.dummy.net/build_centos8_rocky:latest
312b0fc5642a7670a2c3ee9561880f62320dc4587ee7683fefbe3237af978a10
# docker exec -ti test_image bash
Error: can only create exec sessions on running containers: container state improper
Understanding Docker Containers State
Docker
containers can be in several different states, each indicating a
specific stage in their lifecycle. You can inspect the state of a
container using commands like
docker ps
for running containers and docker ps -a for all containers,
including those not running.
Here’s a detailed explanation of each state:
- Created: The container has been created but not started. It’s initialized and ready to run but is not executing any processes yet.
- Running: The container is actively running. Its main process is operational, and it’s capable of executing commands and tasks.
- Paused: The container’s processes are temporarily halted. It’s in a suspended state, conserving resources without stopping completely.
- Exited: The container has stopped running. This happens after its main process completes or if the container is manually stopped.
- Restarting: The container is in the process of stopping and then starting again, usually triggered by a container restart policy.
- Dead: The container is in a non-functional state, often due to system errors or issues in the Docker daemon that prevent it from being managed properly.
- Removing: The container is in the process of being removed from the system. This is a transitional state during the deletion process.
Common Reasons for the Error
When dealing with the error “Error: can only create exec sessions on running containers: container state improper,” there are several common reasons why this issue might occur.
- The container has stopped or crashed. This could be due to the main process inside the container exiting or due to an error during container startup.
- The container is paused. When a container is paused, all its processes are also paused, and it cannot accept new commands.
- The container is in the process of starting up but not fully running yet.
How to Resolve “Error: can only create exec sessions on running containers”
- Check Container Status: First, check the status of the container
using
docker ps -a. Look for the container in question and check its status. - Start the Container: If the container is not running, start it
with
docker start [container_name_or_id]. - Unpause the Container: If the container is paused, unpause it
using
docker unpause [container_name_or_id]. - Keep the Container Running:
Set an
entrypoint and provide a custom command to keep the command
running
[
docker run --entrypoint /bin/bash container_name_or_id -c "sleep 999999"]
In my case I went ahead with setting my custom entrypoint and adding sleep command to keep the container running:
docker run --entrypoint /bin/bash custom-docker-releases.repo3.cci.dummy.net/build_centos8_rocky:latest -c "sleep 999999"
Next verify if docker process is running
~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
....
d7bb60af1cfc custom-docker-releases.repo3.cci.dummy.net/build_centos8_rocky:latest -c sleep 999999 5 hours ago Up 5 hours ago nice_stonebraker
Next connect to the container image
~]# docker exec -ti d7bb60af1cfc bash
[root@d7bb60af1cfc /]#
Once you are done, kill the process
~]# docker kill d7bb60af1cfc
Summary
The article provides a comprehensive guide on resolving the error
“Error: can only create exec sessions on running containers: container
state improper,” commonly encountered in Docker. This error typically
occurs when an attempt is made to execute a command in a Docker
container that isn’t in a ‘running’ state. The article emphasizes that
Docker containers need to be in a running state to accept exec
commands, with other states like ‘created’, ‘paused’, or ’exited’
leading to this issue. It outlines the common causes for this error,
including scenarios where a container has stopped, crashed, is paused,
or is still initializing. Incorrect container identifiers or Docker
daemon issues can also trigger this problem.
To troubleshoot, the article suggests checking the container’s state
using docker ps -a. If the container is not running, it can be started
with docker start. For paused containers, docker unpause is the
solution. In cases where containers stop or crash immediately upon
starting, checking the
logs with
docker logs is recommended for insights. The article also
underscores the importance of proper container configuration, adequate
system resources, and using a stable Docker version. Additionally, it
proposes a practical solution for keeping a container in a running
state: using docker run with an overridden entrypoint to execute a
prolonged sleep command, like
docker run --entrypoint /bin/bash image_name -c "sleep 999999". This
approach is especially useful for scenarios requiring the container to
remain active for extended periods, such as during debugging or
development phases. Overall, the article serves as a valuable resource
for effectively managing
and resolving state-related issues in Docker containers.

