Whenever I try to start a container using Testcontainers (NodeJS) using VSCode devcontainer (docker-ouside-of-docker approach) and I define a Network for the container and I expose a port, it fails with the following error:
Port 32803 not bound after 60000ms
It doesn’t happen if I do not add a custom network to the container, but I need to do so.
It works just fine when executed outside the devcontainer.
This is my devcontainer.json file:
{
// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "npm ci",
// Configure tool-specific properties.
"customizations": {
"vscode": {
"extensions": [
"pkief.material-icon-theme",
"dbaeumer.vscode-eslint",
"christian-kohler.path-intellisense",
"esbenp.prettier-vscode",
"vscode.git",
"ms-azuretools.vscode-docker",
"cucumberopen.cucumber-official",
"kakumei.ts-debug",
"alexkrechik.cucumberautocomplete",
"abhinabaghosh.cucumberquick"
]
}
},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "root"
}
This is my (typescript) code:
const network = await new Network().start()
const startPostgresContainer = async () => {
return (
new GenericContainer('postgres:12.13')
.withEnvironment({ POSTGRES_PASSWORD: 'xxx' })
.withEnvironment({ POSTGRES_USER: 'xxx' })
.withNetwork(network)
.withNetworkAliases('db')
.withExposedPorts(5432)
.start()
)
}
try {
this.postgresContainer = await startPostgresContainer()
} catch (err) {
logger.error('Error starting container:', err)
}