Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create an adhoc sandbox with a custom Dockerfile? #11

Open
punkpeye opened this issue Dec 28, 2024 · 4 comments
Open

How to create an adhoc sandbox with a custom Dockerfile? #11

punkpeye opened this issue Dec 28, 2024 · 4 comments

Comments

@punkpeye
Copy link

punkpeye commented Dec 28, 2024

I have tried:

const sdk = new CodeSandbox(CODESANDBOX_API_KEY);
const sandbox = await sdk.sandbox.create({
  hibernationTimeoutSeconds: 300,
});

await sandbox.fs.writeTextFile(
  '.devcontainer/Dockerfile',
  'FROM python:3.10\nENV FOO=BAR\n',
);

await sandbox.fs.writeTextFile(
  '.devcontainer/devcontainer.json',
  stringifyJson({
    build: {
      dockerfile: './Dockerfile',
    },
    name: 'Test',
  }),
);


const result = await sandbox.shells.run('env | grep FOO');
@punkpeye
Copy link
Author

Based on my reading of the documentation, I would expect that anything that's run in shell, will execute inside the Dockerfile, but that does not seem to be the case.

await sandbox.fs.writeTextFile(
  '.devcontainer/Dockerfile',
  multiline`
    FROM python:3.11  
    RUN echo 'Hello, World!' > index.html
  `,
);

await sandbox.fs.writeTextFile(
  '.devcontainer/devcontainer.json',
  stringifyJson({
    build: {
      dockerfile: './Dockerfile',
    },
    name: 'Test',
  }),
);

console.log(await sandbox.shells.run('ls -lah .'));

This returns the sandbox contents as opposed to Docker's shell.

In short, I want to deploy a service using Dockerfile and access it through a port.

@CompuIves
Copy link
Member

Heya! What you're sharing should work, but it requires a restart of the sandbox to apply the dockerfile (sandbox.reboot). I would recommend to use the snapshot builder (https://codesandbox.io/docs/sdk/snapshot-builder) to precreate a template that has the Docker image loaded, and use that template for creating new sandboxes.

@punkpeye
Copy link
Author

punkpeye commented Jan 2, 2025

I eventually figured out that if I restart it works, but the restart time was way too slow for my use case.

Is snapshot something that can be built/deployed faster than using this and then doing restart?

@CompuIves
Copy link
Member

Yes, absolutely. I would recommend creating a snapshot with the docker image, and then using that when creating new sandboxes. That will allow you to create new sandboxes with that docker image in ~1s, instead of having to wait for the full reboot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants