Syntax Video
you should be using dev containers
- 0:00 demo
- 3:04 what are containers?
- 4:48 the dev containers spec
- 5:13 dev container templates
- 6:20 dev container setup
- 7:38 dev container features / common utilities
- 9:15 docker in docker feature
- 10:41 more useful features
- 11:41 what makes up a feature
- 12:19 claude code example dev container
- 13:48 my firewall feature
- 14:42 feature based claude code dev container
- 15:30 claude code inside a dev container
- 16:34 editor customizations
- 19:15 thanks
demo
What's up? CJ here. Devont Containers were awesome and I want to show you all about them. So, let's dive right in. As you can see, I'm on a fresh install of Mac OS. I've only installed VS Code, Chrome, and Docker. And as you can see, VS Code is also in its default state with no customizations or themes or anything like that. But I have this devontainer.json file. And this one file describes my entire dev environment. It describes the base image, which is going to give me Node.js. It gives me tools like oh my zish or Oh my zsh. Um, I've pointed at a specific project that I'm going to be editing and I've specified all my preferred extensions and VS Code settings and themes. So, all I have to do is say reopen in container. And then instantly it's going to look at that file, pull everything down, and now I have a development environment that I'm used to. If you've ever seen me coding on this channel, this is my VS Code, right? It's my layout. It's my theme, but it's actually running inside of a container. So, all of the code, everything is running in here. Now, I've enabled some other features as well. So,
the project that I'm editing is just a regular plain old project. It it hasn't been set up directly for dev containers, but in this video, I'll show you how to do that. But it this is just a folder that's a regular plain old project, but it does have a docker compose file, and I've enabled this feature called Docker in Docker. So, if I do docker compose up, I'm actually starting Docker containers inside this container. So, this started up a Reddus database and a MySQL database. So, now I can start up my app. And the beauty of all of this is everything's running inside the container. And you'll see that as it exposed ports, um, those are popping up and ask me if I want to actually preview them. So for instance, we can open it in the browser. But because everything is running in a container, in the age of AI and, uh, malware spreading across npm, this locks down my development environment. So the only thing this development environment has access to is this project folder and the container itself. So, it doesn't have access to my full file system or anything else like that. It's completely locked down. So,
this is a great way to develop and it's really fast. So, check this out. If I edit the file, it instantly updates in the browser. And if you've tried to do containerbased development in the past, it might have felt clunky and slow, but the hot reload, hot refresh here is extremely fast because I'm actually editing the files directly on my file system. They're not having to sync into the container. So the development, the snappy development experience that you're used to is still there, but all of your development is completely containerized. And as I showed, you basically can get your preferred development environment without having to go through the process of like setting up a new computer and setting up all your preferred settings and everything else. So this is awesome. In this video, I'm going to show you how I got this particular devcontainer.json file going. I'll show you lots of other things like other features they have available and other templates. I'll show you how to take a project and add dev containers to it so all the developers on your team can benefit from it and a few other things as well. So if that sounds good, let's dive in. My name is
CJ. Welcome to Syntax. All right, if you're completely new to containers, check out the what is a container section on the Docker website. Uh Docker is just one implementation of containers, but it's one of the most popular. But it's essentially operating system level virtualization. So you have your host operating system and then you have Docker which is an application running and then it allows you to create containers which when code is running inside of that container it thinks it's on a full operating system but it's actually a sectioned off piece of the operating system. That's a rough explanation of it. Um a quick diagram is like this is in contrast to virtual machines. If you don't even know what a virtual machine is, ignore it. Just just start using containers. But u with operating system level virtualization we have a single hardware layer. So this is your computer itself and then the operating system is running and the Linux kernel is actually what enables this containerization feature which essentially allows us to create containerized file systems and containerized process that can't affect
what are containers?
or touch each other. So Docker engine taps into all of those APIs. So when you spin up a container, it's basically giving it its own isolated file system, its own isolated set of processes, its own isolated set of users so that these containers cannot break out and uh interfere with each other. Now, if you want to get this going on your computer, you need to have some kind of container runtime installed. Now, Docker is the most popular, but there are a lot of other ones. If you're completely new to all of this, definitely check out my Docker crash course. I run through all of this stuff in more detail and talk about some alternatives if you don't want to use Docker Desktop, but I show you how to set up a basic container, how to do Docker Compose, and all the things that you might need to know for Docker containers. Now, I will say with the dev container spec, you don't need to know as much, but it is fun to set up your projects to be more reproducible once uh you have a good grasp on how all of that works. Now, let's talk about dev containers. So, if you head to containers.dev, this is a specification that is actually created by Microsoft
the dev containers spec
and it is the specification for that devontainer JSON file that I showed you. They talk about all the various properties and the various things that can exist within that JSON file. and it runs deep. Now, the place that you should likely start is look at available templates because these are going to be the base templates that give you your base development environment. And so, personally, I'm a Node.js and TypeScript developer. So, if I search for templates here, you can see there are several base templates that have been set up by the dev container spec maintainers for Node with JavaScript, Node with MongoDB, Node with Postgress. So, you don't have to know anything about Docker files or Docker Compose. Literally all you have to do is paste in this template URL inside of a devontainer.json file and you instantly have an environment that you can start coding in. And they have templates for all sorts of things. So if you want to do Go development or Rust development or C++ or C sharp.net, they pretty much have base containers for all the types of development that you would want to do. So you start here
dev container templates
basically find your base template that you want to work from. In this case, I'm going to use the node TypeScript one from the dev container spec maintainers. Now, you might be tempted to use this URL. It works for some, but I'm on a Mac. I don't know if that has anything to do with it. I know that if you go to the template, typically it will show the URL for where this image is published on the MCR registry. And this is the one that I got working for me. So, inside of your editor, make sure you have the dev containers extension installed. So in VS Code, if we look at extensions, there is one for dev containers. You just want to make sure that you have that one installed. And so this is the main one.
dev container setup
35 million downloads at the time of this recording. That's the one you want. Other editors support dev containers as well. You'll need to find that particular extension there. So we have the extension. Now all we need is adevcontainer folder. And then inside of there, we can create a dev container.json file. In this JSON file, we have an image key. And this just points to the particular template that we want to use.
That is it. That's all we need. With that, I can just say reopen in container. It's going to look at that file, see the image that needs to be pulled down, and boom, we now have VS Code running inside of a dev container. So, if I pull the the uh terminal up, we're in the the dev container now, and I can do all of my work. And so, this is just the base setup. It has all of the stuff that I need. So, it has node. I believe it has PNPM, it has npm, it has all the things you would need to to work with uh node and npm. And you can do this in any project. So I would add this dev container folder at the root of any project that I want to set up to use dev containers. Now let's reopen this locally and look at what else we can do.
dev container features / common utilities
So if you look on the dev container spec, there's also these things called features. And these are essentially container layers that get layered on top of this base template to give you extra stuff. Now, one of the most useful ones is this thing called common utilities. And a lot of base images use this by default, but let's check it out because this is the one that gives us oh my zish. And yes, it's pronounced zish. Um, and by adding this feature now, you'll get all of this set up by default. So, we can copy this. And in our dev container.json file, we're going to add this features section. And one thing I'm going to do is say configure zish as the default shell because I didn't show it before, but when we opened up that dev container, it was defaulting to bash.
So, if you prefer zish, then you'll just set this up and uh set this to true. And now, when you reopen in a container, it should prompt you to rebuild. Yeah. So, if you ever change that.json JSON file, you will need to rebuild the container. But if we rebuild the container now, once it's done, when I open up a terminal, it's going to default to zish.
You're tired of me saying zish because a lot of people don't like how I pronounced csh. Um, now you will notice the first time that you start up these containers. It does take a little while because it has to pull all of those images down, but everything gets cached. So after the first time of of waiting, it's fairly instant like I showed at the beginning of this video. And so now you'll notice when I open up a new terminal, I'm now using [laughter] Zish. I swear people are going to freak out in the comments.
Um, but yeah, if we echo our shell, zish is now the default. So that's one cool thing that you can do. Let's open this locally again and look at some of the other features. Now, another useful one is Docker in Docker. So this is what I showed at the beginning of this video, and essentially this allows you to spin up Docker containers inside of your container. Now there's also Docker outside of Docker and the idea there is those containers will be running beside your dev container and not inside. Um so you can pick which one you want to use but again for any one of these features that you find from this feature search page you will see the particular setting you need to add in the feature section you can throw that in and customize it.
docker in docker feature
Now, with the particular base image that I'm using, the TypeScript node one, I do have to set this option to false. Your mileage may vary, but now let's reopen, rebuild, and we should get access to Docker inside of Docker. So, it's up and running. And if we take a look at Docker desktop, you can see this is the dev container that's running. But if I attempt to do something like Docker run hello world, this is going to run it inside of uh the container. So, we actually won't see these things spinning up in Docker Desktop cuz it's inside the container itself. You'd have to do something like Docker PS to see all of those images. But, this is a really easy on-ramp if you have a project that already has something like a Docker Compose and you don't want to go through the process of setting up your dev container to point to the Docker Compose because that's also possible. But, this is a way to just get up and going, use what exists, and then as you get more familiar with dev containers, you can start to add other containers inside of this JSON file. So, this is basically how you'll get started. Learn a little bit about containers, install the Devont
more useful features
Containers extension in your preferred editor, find the specific base template that you need, and then fill it out with all of the features that make up your development experience. And this one page has hundreds and hundreds of them. Another really useful one is the SSH server, and this basically makes it so that once the container is spun up, you can SSH into it from outside of VS Code.
And so this is nice if you prefer like using Vim or VI or something like that and you don't want to use the terminal inside of VS Code. Install this SSH feature, expose port 2222, just check out their docs and then you can just SSH into your dev container. And another one that's very useful and why I started looking into dev containers to begin with are things like Claude. So there's a few people that have set up a Claude code feature. To get going with these, you essentially just add them to the features section. they'll get automatically set up. But if you take a look at this one for instance, you can actually see how they do this. So there's a way that you can develop your own features. It's fairly straightforward. You can see that their feature.json file just describes what their feature does and maybe some extra customizations to automatically add to that devcontainer.json.
what makes up a feature
And then there is a bash script that sets things up. And if we look at this, literally all this does is install uh cloud code from npm when the container starts up. So it's nothing crazy, but it's a nice feature because you don't have to write this code yourself and you don't have to like write a custom install script for your dev container. And so essentially, if I add this feature, part of the build of the container is to install cloud code and then I'll have cloud code accessible inside the container. Now, typically you want to lock things down even further than that. And the cloud code repository actually has an example dev container that you can look at. And in their documentation, they recommend that you just copy it. So, if you head to anthropics/cloudcode on GitHub, look at that. Devcontainer folder, you'll see they have a devontainer.json file. This one's a lot more custom because instead of choosing a pre-existing image, they actually defined their own docker file and set up their own image. And I I wouldn't recommend this when you're working on your own features because this is actually a lot more to maintain. They probably should have chose like the node
claude code example dev container
TypeScript base image and then customized it from there. But, you know, you could do that if you wanted to or I could do that, which I have. Um, but that essentially sets up the container and one of the other aspects of this is they set up the firewall. So you can see it has a few capabilities like net admin and net raw. And if you look at their shell script, this actually uses IP tables inside of this Debian container to automatically block everything except for all of the domains used by VS Code and Cloud, but then anything else would get blocked. So if you have an agent running inside of this dev container and it goes rogue and tries to reach out to other stuff that isn't anthropic or VS Code, that'll automatically stop it from reaching out outside of the domains that are defined inside of here. So as you can see, dev containers get a lot more interesting once you start diving into creating your own custom features, creating your own base template Docker files. It's a wide wide wonderful world out there. Now, personally, I started working on my own dev container feature
my firewall feature
called firewall, and it essentially wraps up what that bash script is doing from the cloud code devontainer example, but makes it nice and configurable. So, essentially, you can add this feature and then specify which services you would like to add to the allow list of the firewall. And so, if we look at that particular feature, I have it preconfigured for a bunch of different things. So things like the Docker registry, the npm registry, the Python registry. Um you can allow list the entire Google uh cloud IP space or cloudflare. Um there's all these other AI services that you could enable. Let's say you're using open code and you want to talk to like a different AI API.
Essentially, you can install my feature and then in the settings just say something like hugging face API true and it will only allow those IP addresses plus whatever else you enable. So what we can do is we can combine and composite. So if we take a look here, let's use this feature from Steuart Bell that installs cloud code but doesn't set up a firewall. Right? If we look at the container itself, it's nothing special.
feature based claude code dev container
It literally just has a post install script that goes through and then just runs the cloud install from the cloud website. So it doesn't set up a firewall or anything like that. So let's use this feature in our container. Throw that right here. And then we'll use my firewall feature as well. So we'll get access to cloud code that's inside this container working in this particular project folder. And we'll get the firewall feature which is going to lock it down and not allow it to talk to anything else except for GitHub and the cloud on anthropic IP addresses. So let's reopen in container and rebuild and test it out. Okay, we're in the container now. We should have access to claude. We do. Now we can actually go beyond the base setup because this is just a bare container running cloud. But if we wanted to use our existing cloud history and everything else, we can actually do a container bind mount to the cloud directory on our local machine. So that that's a bit more advanced. I'll say if you're interested in that, take a look at the install script from the example dev container in
claude code inside a dev container
the cloud code repo because they have a section where they're actually mounting those particular folders. But regardless, we have just a base installation. So we'll have to go through the setup. We can log in or we can set up an API key, but we can start using cloud. The other aspect is our firewall is up and ready to go. So if I attempt to make a request to google.com, it fails, right? The only things that I have access to are github.com and all of the related um GitHub APIs and everything else. And then also the anthropic API. So this is a nice little stack where I have all of my features. I can use Docker, I can use Claude. Um and then I also have lock down the firewall as well. Now, the last piece of this is customizing your particular VS Code setup. Um, at the beginning of this video, I showed you how to do this from a completely fresh install of Mac OS.
editor customizations
And here's one more level of uh [laughter] indirection. This is actually a virtual machine running Mac OS on my Mac that in turn is using Devontainer. So, I have all kinds of levels of of um of isolation here. But um as I showed earlier, there is this customization section. And this is where you can go in and specify like your preferred theme and color and also like uh various like formatting rules and different stuff like that. Now, these are typically specific to each individual developer.
So, I wouldn't recommend that you go through and dump your entire settings into here for a project that multiple people work on. You should just put things in here that every developer on the team needs. Same thing with extensions. Now, this has every extension possible that that I would want to use because this setup basically allows me to point it at any folder without having to create a dev container folder in that particular project. So, basically, I have my virtual machine here. I have access to the files on my local machine and then I just point it to that specific directory and then I get my full development environment, but I'm running in a VM and also running in a container. [laughter] So essentially, this is a setup that will allow me to have all of my settings on a machine that isn't hasn't been configured at all. But for your particular project, you might be using things like Tailwind or Prettier or Prisma or SpeltKit. And so what you can do in the extensions array for your particular project is just list those extensions. So that way you
automatically get those features for the particular project that you're working on. Now all of this is documented in the VS Code docs. So the dev container spec actually just specifies the customizations key, but this will be different for every single editor. And so you can see inside of here, I have a key for VS Code. And if you check out the container specific settings here, this lets you know, hey, specify them here. And essentially, all of the settings you're used to for VS Code can be specified inside of here. So you literally could take your settings.json and just paste it in here and just like pull out the properties that you need.
But all of that's supported and all of it's documented specifically in the Visual Studio Code docs. And you can also set up custom dot files. So if you check out the this dot file section, you can specify your dot files repository and target path. And that way every dev container you spin up will have all of your specific git config and everything else that you might want to set up as well. So anything that you do to customize your development environment, they essentially have a way of doing it in a dev container and making it reproducible. All right, that's all I got for you. As you can see, there's so much more we could talk about. So, if there's any particular feature or aspect of Devont Containers that you have questions about, leave it down in the comments. Also, if there's other aspects of this you'd like to see me explore, let me know as well. And u if you like this, throw a like on the video, subscribe, all that good YouTube stuff, because uh sharing and liking and subscribing, that that's basically how we can keep doing what we're doing here at Syntax. All right, that's all I got for you. I'll see you in the next one.
thanks
Quote it with credit and a link. Quoting Syntax