@@ -33,7 +33,8 @@ public class DockerService implements DisposableBean {
33
33
private final DockerClient client ;
34
34
private final Config config ;
35
35
private final ExecutorService executor = Executors .newSingleThreadExecutor ();
36
- private final ConcurrentHashMap <StartupScriptId , String > cachedContainers = new ConcurrentHashMap <>();
36
+ private final ConcurrentHashMap <StartupScriptId , String > cachedContainers =
37
+ new ConcurrentHashMap <>();
37
38
private final StartupScriptsService startupScriptsService ;
38
39
39
40
private final String jshellWrapperBaseImageName ;
@@ -109,15 +110,15 @@ private void pullImage() throws InterruptedException {
109
110
*/
110
111
public String createContainer (String name ) {
111
112
HostConfig hostConfig = HostConfig .newHostConfig ()
112
- .withAutoRemove (true )
113
- .withInit (true )
114
- .withCapDrop (Capability .ALL )
115
- .withNetworkMode ("none" )
116
- .withPidsLimit (2000L )
117
- .withReadonlyRootfs (true )
118
- .withMemory ((long ) config .dockerMaxRamMegaBytes () * 1024 * 1024 )
119
- .withCpuCount ((long ) Math .ceil (config .dockerCPUsUsage ()))
120
- .withCpusetCpus (config .dockerCPUSetCPUs ());
113
+ .withAutoRemove (true )
114
+ .withInit (true )
115
+ .withCapDrop (Capability .ALL )
116
+ .withNetworkMode ("none" )
117
+ .withPidsLimit (2000L )
118
+ .withReadonlyRootfs (true )
119
+ .withMemory ((long ) config .dockerMaxRamMegaBytes () * 1024 * 1024 )
120
+ .withCpuCount ((long ) Math .ceil (config .dockerCPUsUsage ()))
121
+ .withCpusetCpus (config .dockerCPUSetCPUs ());
121
122
122
123
return client .createContainerCmd (jshellWrapperBaseImageName + Config .JSHELL_WRAPPER_IMAGE_NAME_TAG )
123
124
.withHostConfig (hostConfig )
@@ -140,14 +141,15 @@ public String createContainer(String name) {
140
141
* @param startupScriptId Script to initialize the container with.
141
142
* @return The ContainerState of the newly created container.
142
143
*/
143
- public ContainerState initializeContainer (String name , StartupScriptId startupScriptId ) throws IOException {
144
+ public ContainerState initializeContainer (String name , StartupScriptId startupScriptId )
145
+ throws IOException {
144
146
if (cachedContainers .isEmpty () || !cachedContainers .containsKey (startupScriptId )) {
145
147
String containerId = createContainer (name );
146
148
return setupContainerWithScript (containerId , true , startupScriptId );
147
149
}
148
150
String containerId = cachedContainers .get (startupScriptId );
149
151
executor .submit (() -> initializeCachedContainer (startupScriptId ));
150
- // Rename container with new name.
152
+
151
153
client .renameContainerCmd (containerId ).withName (name ).exec ();
152
154
return setupContainerWithScript (containerId , false , startupScriptId );
153
155
}
@@ -163,7 +165,8 @@ private void initializeCachedContainer(StartupScriptId startupScriptId) {
163
165
startContainer (id );
164
166
165
167
try (PipedInputStream containerInput = new PipedInputStream ();
166
- BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (new PipedOutputStream (containerInput )))) {
168
+ BufferedWriter writer = new BufferedWriter (
169
+ new OutputStreamWriter (new PipedOutputStream (containerInput )))) {
167
170
attachToContainer (id , containerInput );
168
171
169
172
writer .write (Utils .sanitizeStartupScript (startupScriptsService .get (startupScriptId )));
@@ -185,12 +188,14 @@ private void initializeCachedContainer(StartupScriptId startupScriptId) {
185
188
* @return ContainerState of the spawned container.
186
189
* @throws IOException if an I/O error occurs
187
190
*/
188
- private ContainerState setupContainerWithScript (String containerId , boolean isCached , StartupScriptId startupScriptId ) throws IOException {
191
+ private ContainerState setupContainerWithScript (String containerId , boolean isCached ,
192
+ StartupScriptId startupScriptId ) throws IOException {
189
193
if (!isCached ) {
190
194
startContainer (containerId );
191
195
}
192
196
PipedInputStream containerInput = new PipedInputStream ();
193
- BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (new PipedOutputStream (containerInput )));
197
+ BufferedWriter writer =
198
+ new BufferedWriter (new OutputStreamWriter (new PipedOutputStream (containerInput )));
194
199
195
200
InputStream containerOutput = attachToContainer (containerId , containerInput );
196
201
BufferedReader reader = new BufferedReader (new InputStreamReader (containerOutput ));
@@ -206,6 +211,7 @@ private ContainerState setupContainerWithScript(String containerId, boolean isCa
206
211
207
212
/**
208
213
* Creates a new container
214
+ *
209
215
* @param containerId the ID of the container to start
210
216
*/
211
217
public void startContainer (String containerId ) {
@@ -219,35 +225,38 @@ public void startContainer(String containerId) {
219
225
* Logs any output from stderr and returns an InputStream to read stdout.
220
226
*
221
227
* @param containerId the ID of the running container to attach to
222
- * @param containerInput the input stream (containerInput) to send to the container
228
+ * @param containerInput the input stream (containerInput) to send to the container
223
229
* @return InputStream to read the container's stdout
224
230
* @throws IOException if an I/O error occurs
225
231
*/
226
- public InputStream attachToContainer (String containerId , InputStream containerInput ) throws IOException {
232
+ public InputStream attachToContainer (String containerId , InputStream containerInput )
233
+ throws IOException {
227
234
PipedInputStream pipeIn = new PipedInputStream ();
228
235
PipedOutputStream pipeOut = new PipedOutputStream (pipeIn );
229
236
230
237
client .attachContainerCmd (containerId )
231
- .withLogs (true )
232
- .withFollowStream (true )
233
- .withStdOut (true )
234
- .withStdErr (true )
235
- .withStdIn (containerInput )
236
- .exec (new ResultCallback .Adapter <>() {
237
- @ Override
238
- public void onNext (Frame object ) {
239
- try {
240
- String payloadString = new String (object .getPayload (), StandardCharsets .UTF_8 );
241
- if (object .getStreamType () == StreamType .STDOUT ) {
242
- pipeOut .write (object .getPayload ()); // Write stdout data to pipeOut
243
- } else {
244
- LOGGER .warn ("Received STDERR from container {}: {}" , containerId , payloadString );
245
- }
246
- } catch (IOException e ) {
247
- throw new UncheckedIOException (e );
238
+ .withLogs (true )
239
+ .withFollowStream (true )
240
+ .withStdOut (true )
241
+ .withStdErr (true )
242
+ .withStdIn (containerInput )
243
+ .exec (new ResultCallback .Adapter <>() {
244
+ @ Override
245
+ public void onNext (Frame object ) {
246
+ try {
247
+ String payloadString =
248
+ new String (object .getPayload (), StandardCharsets .UTF_8 );
249
+ if (object .getStreamType () == StreamType .STDOUT ) {
250
+ pipeOut .write (object .getPayload ()); // Write stdout data to pipeOut
251
+ } else {
252
+ LOGGER .warn ("Received STDERR from container {}: {}" , containerId ,
253
+ payloadString );
248
254
}
255
+ } catch (IOException e ) {
256
+ throw new UncheckedIOException (e );
249
257
}
250
- });
258
+ }
259
+ });
251
260
252
261
return pipeIn ;
253
262
}
0 commit comments