Skip to content

Commit

Permalink
Adds a fix to kill and remove a container if it still exists
Browse files Browse the repository at this point in the history
  • Loading branch information
dakota002 committed Jan 27, 2025
1 parent ff85107 commit ac4e6c8
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions runDocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,27 @@ const [, , command, jsonifiedArgs] = process.argv
const docker = new Dockerode({ protocol: 'http' })

let containerId = ''

if (command === 'launch-ddb-local-docker-subprocess') {
const { port } = JSON.parse(jsonifiedArgs)
let container: Container
try {
container = await createDdbContainer(port)
} catch (error) {
// Fix for Windows, containers exit, but do not get removed properly.
console.log('\nExisting container, removing and recreating')
const containers = await docker.listContainers({
limit: 1,
filters: '{"name": ["dynamodb-local"]}',
})
for (const existing of containers) {
if (existing.State !== 'exited') {
await docker.getContainer(existing.Id).kill()
}
await docker.getContainer(existing.Id).remove()
}

const container = await docker.createContainer({
Image: 'amazon/dynamodb-local:latest',
name: 'dynamodb-local',
Cmd: ['-jar', 'DynamoDBLocal.jar', '-sharedDb', '-dbPath', '/tmp/'],
ExposedPorts: {
'8000/tcp': {},
},

HostConfig: {
PortBindings: {
[`${port}/tcp`]: [{ HostIp: '0.0.0.0', HostPort: `${port}` }],
},
},
})
container = await createDdbContainer(port)
}
containerId = container.id
const stream = await container.attach({ stream: true, stderr: true })
stream.pipe(process.stderr)
Expand Down Expand Up @@ -74,3 +77,19 @@ export const launchDocker: LauncherFunction = async ({ port, options }) => {
export async function removeContainer(containerId: string) {
await docker.getContainer(containerId).remove()
}

async function createDdbContainer(port: number) {
return await docker.createContainer({
Image: 'amazon/dynamodb-local:latest',
name: 'dynamodb-local',
Cmd: ['-jar', 'DynamoDBLocal.jar', '-sharedDb', '-dbPath', '/tmp/'],
ExposedPorts: {
'8000/tcp': {},
},
HostConfig: {
PortBindings: {
[`${port}/tcp`]: [{ HostIp: '0.0.0.0', HostPort: `${port}` }],
},
},
})
}

0 comments on commit ac4e6c8

Please sign in to comment.