Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
compose/compose/container.py at master · rghv/compose · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
rghv
compose
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
compose
/
compose
/
container.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
201 lines (156 loc) · 5.74 KB
master
Breadcrumbs
compose
/
compose
/
container.py
Copy path
Top
File metadata and controls
Code
Blame
201 lines (156 loc) · 5.74 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from
__future__
import
unicode_literals
from
__future__
import
absolute_import
import
six
from
functools
import
reduce
from
.
const
import
LABEL_CONTAINER_NUMBER
,
LABEL_SERVICE
class
Container
(
object
):
"""
Represents a Docker container, constructed from the output of
GET /containers/:id:/json.
"""
def
__init__
(
self
,
client
,
dictionary
,
has_been_inspected
=
False
):
self
.
client
=
client
self
.
dictionary
=
dictionary
self
.
has_been_inspected
=
has_been_inspected
@
classmethod
def
from_ps
(
cls
,
client
,
dictionary
,
**
kwargs
):
"""
Construct a container object from the output of GET /containers/json.
"""
new_dictionary
=
{
'Id'
:
dictionary
[
'Id'
],
'Image'
:
dictionary
[
'Image'
],
'Name'
:
'/'
+
get_container_name
(
dictionary
),
}
return
cls
(
client
,
new_dictionary
,
**
kwargs
)
@
classmethod
def
from_id
(
cls
,
client
,
id
):
return
cls
(
client
,
client
.
inspect_container
(
id
))
@
classmethod
def
create
(
cls
,
client
,
**
options
):
response
=
client
.
create_container
(
**
options
)
return
cls
.
from_id
(
client
,
response
[
'Id'
])
@
property
def
id
(
self
):
return
self
.
dictionary
[
'Id'
]
@
property
def
image
(
self
):
return
self
.
dictionary
[
'Image'
]
@
property
def
image_config
(
self
):
return
self
.
client
.
inspect_image
(
self
.
image
)
@
property
def
short_id
(
self
):
return
self
.
id
[:
10
]
@
property
def
name
(
self
):
return
self
.
dictionary
[
'Name'
][
1
:]
@
property
def
name_without_project
(
self
):
return
'{0}_{1}'
.
format
(
self
.
labels
.
get
(
LABEL_SERVICE
),
self
.
number
)
@
property
def
number
(
self
):
number
=
self
.
labels
.
get
(
LABEL_CONTAINER_NUMBER
)
if
not
number
:
raise
ValueError
(
"Container {0} does not have a {1} label"
.
format
(
self
.
short_id
,
LABEL_CONTAINER_NUMBER
))
return
int
(
number
)
@
property
def
ports
(
self
):
self
.
inspect_if_not_inspected
()
return
self
.
get
(
'NetworkSettings.Ports'
)
or
{}
@
property
def
human_readable_ports
(
self
):
def
format_port
(
private
,
public
):
if
not
public
:
return
private
return
'{HostIp}:{HostPort}->{private}'
.
format
(
private
=
private
,
**
public
[
0
])
return
', '
.
join
(
format_port
(
*
item
)
for
item
in
sorted
(
six
.
iteritems
(
self
.
ports
)))
@
property
def
labels
(
self
):
return
self
.
get
(
'Config.Labels'
)
or
{}
@
property
def
log_config
(
self
):
return
self
.
get
(
'HostConfig.LogConfig'
)
or
None
@
property
def
human_readable_state
(
self
):
if
self
.
is_running
:
return
'Ghost'
if
self
.
get
(
'State.Ghost'
)
else
'Up'
else
:
return
'Exit %s'
%
self
.
get
(
'State.ExitCode'
)
@
property
def
human_readable_command
(
self
):
entrypoint
=
self
.
get
(
'Config.Entrypoint'
)
or
[]
cmd
=
self
.
get
(
'Config.Cmd'
)
or
[]
return
' '
.
join
(
entrypoint
+
cmd
)
@
property
def
environment
(
self
):
return
dict
(
var
.
split
(
"="
,
1
)
for
var
in
self
.
get
(
'Config.Env'
)
or
[])
@
property
def
is_running
(
self
):
return
self
.
get
(
'State.Running'
)
def
get
(
self
,
key
):
"""Return a value from the container or None if the value is not set.
:param key: a string using dotted notation for nested dictionary
lookups
"""
self
.
inspect_if_not_inspected
()
def
get_value
(
dictionary
,
key
):
return
(
dictionary
or
{}).
get
(
key
)
return
reduce
(
get_value
,
key
.
split
(
'.'
),
self
.
dictionary
)
def
get_local_port
(
self
,
port
,
protocol
=
'tcp'
):
port
=
self
.
ports
.
get
(
"%s/%s"
%
(
port
,
protocol
))
return
"{HostIp}:{HostPort}"
.
format
(
**
port
[
0
])
if
port
else
None
def
start
(
self
,
**
options
):
return
self
.
client
.
start
(
self
.
id
,
**
options
)
def
stop
(
self
,
**
options
):
return
self
.
client
.
stop
(
self
.
id
,
**
options
)
def
kill
(
self
,
**
options
):
return
self
.
client
.
kill
(
self
.
id
,
**
options
)
def
restart
(
self
,
**
options
):
return
self
.
client
.
restart
(
self
.
id
,
**
options
)
def
remove
(
self
,
**
options
):
return
self
.
client
.
remove_container
(
self
.
id
,
**
options
)
def
inspect_if_not_inspected
(
self
):
if
not
self
.
has_been_inspected
:
self
.
inspect
()
def
wait
(
self
):
return
self
.
client
.
wait
(
self
.
id
)
def
logs
(
self
,
*
args
,
**
kwargs
):
return
self
.
client
.
logs
(
self
.
id
,
*
args
,
**
kwargs
)
def
inspect
(
self
):
self
.
dictionary
=
self
.
client
.
inspect_container
(
self
.
id
)
self
.
has_been_inspected
=
True
return
self
.
dictionary
# TODO: only used by tests, move to test module
def
links
(
self
):
links
=
[]
for
container
in
self
.
client
.
containers
():
for
name
in
container
[
'Names'
]:
bits
=
name
.
split
(
'/'
)
if
len
(
bits
)
>
2
and
bits
[
1
]
==
self
.
name
:
links
.
append
(
bits
[
2
])
return
links
def
attach
(
self
,
*
args
,
**
kwargs
):
return
self
.
client
.
attach
(
self
.
id
,
*
args
,
**
kwargs
)
def
attach_socket
(
self
,
**
kwargs
):
return
self
.
client
.
attach_socket
(
self
.
id
,
**
kwargs
)
def
__repr__
(
self
):
return
'<Container: %s (%s)>'
%
(
self
.
name
,
self
.
id
[:
6
])
def
__eq__
(
self
,
other
):
if
type
(
self
)
!=
type
(
other
):
return
False
return
self
.
id
==
other
.
id
def
__hash__
(
self
):
return
self
.
id
.
__hash__
()
def
get_container_name
(
container
):
if
not
container
.
get
(
'Name'
)
and
not
container
.
get
(
'Names'
):
return
None
# inspect
if
'Name'
in
container
:
return
container
[
'Name'
]
# ps
shortest_name
=
min
(
container
[
'Names'
],
key
=
lambda
n
:
len
(
n
.
split
(
'/'
)))
return
shortest_name
.
split
(
'/'
)[
-
1
]
You can’t perform that action at this time.