I have a repo in guthub and there are shared containers(folders) in them. I need to pass the container name and check if it is running. I have a script in place as mentioned below:
from typing import Optional
import docker
def is_container_running(container_name: str) -> Optional[bool]:
RUNNING = "running"
docker_client = docker.from_env()
try:
container = docker_client.containers.get(container_name)
except docker.errors.NotFound as exc:
print(f"Check container name\n{exc.explanation}")
else:
container_state = container.attrs["State"]
return container_state["Status"] == RUNNING
if __name__ == "__main__":
container_name = "folder1"
result = is_container_running(container_name)
print(result)
This is the structure of the repo and I have created newfolder and new_script.py has the above python code.
my_repo
folder1
file1.py
Dockerfile
folder2
file1.py
Dockerfile
folder3
file1.py
file2.py
Dockerfile
newfolder
new_script.py
If I pass "folder1" in the container_name variable, Im getting this message:
Check container name No such container: folder1 None
How do I need to check the status of the container in my repo? Pls suggest.
CodePudding user response:
A container can be run with any name.
folder1
does not represent necessarily a container name, since we do not know what parameter was used for the docker run -t <aName>
command.
But if it does, you could say that such an error ("No such container") means the status is "Not running".