Skip to content

Commit f1fed5d

Browse files
authored
Create dockerenvars.py
0 parents  commit f1fed5d

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

dockerenvars.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import requests
2+
import json
3+
4+
# Docker API URL (assuming Docker is running locally)
5+
DOCKER_API_URL = "http://localhost:2375"
6+
7+
def get_containers():
8+
# Get list of all containers
9+
response = requests.get(f"{DOCKER_API_URL}/containers/json?all=true")
10+
if response.status_code != 200:
11+
print("Failed to get containers")
12+
return []
13+
return response.json()
14+
15+
def get_container_env(container_id):
16+
# Get environment variables for a specific container
17+
response = requests.get(f"{DOCKER_API_URL}/containers/{container_id}/json")
18+
if response.status_code != 200:
19+
print(f"Failed to get info for container {container_id}")
20+
return []
21+
22+
container_data = response.json()
23+
env_vars = container_data.get('Config', {}).get('Env', [])
24+
return env_vars
25+
26+
def main():
27+
containers = get_containers()
28+
if not containers:
29+
print("No containers found.")
30+
return
31+
32+
for container in containers:
33+
container_id = container['Id']
34+
container_name = container['Names'][0]
35+
print(f"\nContainer Name: {container_name}")
36+
print(f"Container ID: {container_id}")
37+
38+
env_vars = get_container_env(container_id)
39+
if env_vars:
40+
print("Environment Variables:")
41+
for env in env_vars:
42+
print(f" - {env}")
43+
else:
44+
print("No environment variables found.")
45+
46+
if __name__ == "__main__":
47+
main()

0 commit comments

Comments
 (0)