Skip to content

Commit 24688dc

Browse files
committed
Fix recreation of aliases/network config in Docker container attach
Previously we didn't touch 'NetworkMode' which meant that it retained the name of the main network the container was using, and that meant that it reconnected to that automatically, ignoring the subsequent network connection call we made with the full settings. We now connect all attached containers to 'none' initially, and then manually reconnect them to the original networks with their full settings, to preserve everything (especially aliases). This change also ensures we wait for network setup to be complete, so the container starts with this change entirely in place.
1 parent 6fe5964 commit 24688dc

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/interceptors/docker/docker-commands.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,13 @@ function deriveContainerCreationConfigFromInspection(
191191
): Docker.ContainerCreateOptions {
192192
return {
193193
...containerDetails.Config,
194-
HostConfig: containerDetails.HostConfig,
195194
name: containerDetails.Name,
196195
// You can't reconnect all networks at creation for >1 network.
197-
// To simplify things, we just connect all networks after creation.
196+
// To avoid issues, we just reconnect all networks after creation.
197+
HostConfig: {
198+
...containerDetails.HostConfig,
199+
NetworkMode: 'none'
200+
},
198201
NetworkingConfig: {}
199202
};
200203
}
@@ -204,6 +207,11 @@ async function connectNetworks(
204207
containerId: string,
205208
networks: Docker.EndpointsConfig
206209
) {
210+
// At creation, we initially connect containers to 'none', and we have to
211+
// undo that before we can connect to anything else. Note that this all
212+
// happens before container startup, so this is invisible.
213+
await docker.getNetwork('none').disconnect({ Container: containerId });
214+
207215
await Promise.all(
208216
Object.keys(networks).map(networkName =>
209217
docker.getNetwork(networkName).connect({
@@ -268,7 +276,7 @@ export async function restartAndInjectContainer(
268276
);
269277

270278
// Reconnect to all the previous container's networks:
271-
connectNetworks(
279+
await connectNetworks(
272280
docker,
273281
newContainer.id,
274282
containerDetails.NetworkSettings.Networks

0 commit comments

Comments
 (0)