@@ -44,6 +44,8 @@ public class ContainerConfig {
4444 private final boolean autoRemove ;
4545 private final long shmSize ;
4646 private final Map <String , Object > hostConfig ;
47+ private final Map <String , String > labels ;
48+ private final String name ;
4749
4850 public ContainerConfig (
4951 Image image ,
@@ -61,6 +63,7 @@ public ContainerConfig(
6163 devices ,
6264 networkName ,
6365 shmSize ,
66+ ImmutableMap .of (),
6467 ImmutableMap .of ());
6568 }
6669
@@ -73,6 +76,52 @@ public ContainerConfig(
7376 String networkName ,
7477 long shmSize ,
7578 Map <String , Object > hostConfig ) {
79+ this (
80+ image ,
81+ portBindings ,
82+ envVars ,
83+ volumeBinds ,
84+ devices ,
85+ networkName ,
86+ shmSize ,
87+ hostConfig ,
88+ ImmutableMap .of ());
89+ }
90+
91+ public ContainerConfig (
92+ Image image ,
93+ Multimap <String , Map <String , Object >> portBindings ,
94+ Map <String , String > envVars ,
95+ Map <String , String > volumeBinds ,
96+ List <Device > devices ,
97+ String networkName ,
98+ long shmSize ,
99+ Map <String , Object > hostConfig ,
100+ Map <String , String > labels ) {
101+ this (
102+ image ,
103+ portBindings ,
104+ envVars ,
105+ volumeBinds ,
106+ devices ,
107+ networkName ,
108+ shmSize ,
109+ hostConfig ,
110+ labels ,
111+ null );
112+ }
113+
114+ public ContainerConfig (
115+ Image image ,
116+ Multimap <String , Map <String , Object >> portBindings ,
117+ Map <String , String > envVars ,
118+ Map <String , String > volumeBinds ,
119+ List <Device > devices ,
120+ String networkName ,
121+ long shmSize ,
122+ Map <String , Object > hostConfig ,
123+ Map <String , String > labels ,
124+ String name ) {
76125 this .image = image ;
77126 this .portBindings = portBindings ;
78127 this .envVars = envVars ;
@@ -82,6 +131,12 @@ public ContainerConfig(
82131 this .autoRemove = true ;
83132 this .shmSize = shmSize ;
84133 this .hostConfig = hostConfig ;
134+ this .labels = labels ;
135+ this .name = name ;
136+ }
137+
138+ public String getName () {
139+ return this .name ;
85140 }
86141
87142 public Image getImage () {
@@ -114,40 +169,94 @@ public ContainerConfig map(Port containerPort, Port hostPort) {
114169 ImmutableMap .of ("HostPort" , String .valueOf (hostPort .getPort ()), "HostIp" , "" ));
115170
116171 return new ContainerConfig (
117- image , updatedBindings , envVars , volumeBinds , devices , networkName , shmSize );
172+ image ,
173+ updatedBindings ,
174+ envVars ,
175+ volumeBinds ,
176+ devices ,
177+ networkName ,
178+ shmSize ,
179+ hostConfig ,
180+ labels ,
181+ name );
118182 }
119183
120184 public ContainerConfig env (Map <String , String > envVars ) {
121185 Require .nonNull ("Container env vars" , envVars );
122186
123187 return new ContainerConfig (
124- image , portBindings , envVars , volumeBinds , devices , networkName , shmSize );
188+ image ,
189+ portBindings ,
190+ envVars ,
191+ volumeBinds ,
192+ devices ,
193+ networkName ,
194+ shmSize ,
195+ hostConfig ,
196+ labels ,
197+ name );
125198 }
126199
127200 public ContainerConfig bind (Map <String , String > volumeBinds ) {
128201 Require .nonNull ("Container volume binds" , volumeBinds );
129202
130203 return new ContainerConfig (
131- image , portBindings , envVars , volumeBinds , devices , networkName , shmSize );
204+ image ,
205+ portBindings ,
206+ envVars ,
207+ volumeBinds ,
208+ devices ,
209+ networkName ,
210+ shmSize ,
211+ hostConfig ,
212+ labels ,
213+ name );
132214 }
133215
134216 public ContainerConfig network (String networkName ) {
135217 Require .nonNull ("Container network name" , networkName );
136218
137219 return new ContainerConfig (
138- image , portBindings , envVars , volumeBinds , devices , networkName , shmSize );
220+ image ,
221+ portBindings ,
222+ envVars ,
223+ volumeBinds ,
224+ devices ,
225+ networkName ,
226+ shmSize ,
227+ hostConfig ,
228+ labels ,
229+ name );
139230 }
140231
141232 public ContainerConfig shmMemorySize (long shmSize ) {
142233 return new ContainerConfig (
143- image , portBindings , envVars , volumeBinds , devices , networkName , shmSize );
234+ image ,
235+ portBindings ,
236+ envVars ,
237+ volumeBinds ,
238+ devices ,
239+ networkName ,
240+ shmSize ,
241+ hostConfig ,
242+ labels ,
243+ name );
144244 }
145245
146246 public ContainerConfig devices (List <Device > devices ) {
147247 Require .nonNull ("Container device files" , devices );
148248
149249 return new ContainerConfig (
150- image , portBindings , envVars , volumeBinds , devices , networkName , shmSize );
250+ image ,
251+ portBindings ,
252+ envVars ,
253+ volumeBinds ,
254+ devices ,
255+ networkName ,
256+ shmSize ,
257+ hostConfig ,
258+ labels ,
259+ name );
151260 }
152261
153262 public ContainerConfig applyHostConfig (Map <String , Object > hostConfig , List <String > configKeys ) {
@@ -158,7 +267,46 @@ public ContainerConfig applyHostConfig(Map<String, Object> hostConfig, List<Stri
158267 .collect (Collectors .toMap (key -> key , hostConfig ::get ));
159268
160269 return new ContainerConfig (
161- image , portBindings , envVars , volumeBinds , devices , networkName , shmSize , setHostConfig );
270+ image ,
271+ portBindings ,
272+ envVars ,
273+ volumeBinds ,
274+ devices ,
275+ networkName ,
276+ shmSize ,
277+ setHostConfig ,
278+ labels ,
279+ name );
280+ }
281+
282+ public ContainerConfig labels (Map <String , String > labels ) {
283+ Require .nonNull ("Container labels" , labels );
284+
285+ return new ContainerConfig (
286+ image ,
287+ portBindings ,
288+ envVars ,
289+ volumeBinds ,
290+ devices ,
291+ networkName ,
292+ shmSize ,
293+ hostConfig ,
294+ labels ,
295+ name );
296+ }
297+
298+ public ContainerConfig name (String name ) {
299+ return new ContainerConfig (
300+ image ,
301+ portBindings ,
302+ envVars ,
303+ volumeBinds ,
304+ devices ,
305+ networkName ,
306+ shmSize ,
307+ hostConfig ,
308+ labels ,
309+ name );
162310 }
163311
164312 @ Override
@@ -221,9 +369,13 @@ private Map<String, Object> toJson() {
221369 hostConfig = ImmutableMap .copyOf (copyMap );
222370 }
223371
224- return ImmutableMap .of (
225- "Image" , image .getId (),
226- "Env" , envVars ,
227- "HostConfig" , hostConfig );
372+ Map <String , Object > config = new HashMap <>();
373+ config .put ("Image" , image .getId ());
374+ config .put ("Env" , envVars );
375+ config .put ("HostConfig" , hostConfig );
376+ if (!labels .isEmpty ()) {
377+ config .put ("Labels" , labels );
378+ }
379+ return config ;
228380 }
229381}
0 commit comments