Check out a free preview of the full Complete Intro to Containers, v2 course

The "Dev Containers" Lesson is part of the full, Complete Intro to Containers, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Brian explains that dev containers provide separate tools, libraries, or runtimes that are needed to work with a codebase. They are useful for complex development environments and ensuring each team members have a consistent toolchain.

Preview
Close

Transcript from the "Dev Containers" Lesson

[00:00:00]
>> Brian Holt: So let's go on to dev containers. I think that's this is something that's going to apply more directly to a bunch of you here. How many of you have joined the company, and it's taken you two weeks to get your development environment set up? Yeah, when I was at Netflix, I don't think I was able to ship code at least for a month.

[00:00:22]
And I don't think I'm even exaggerating because at the time, they have a phenomenal interesting system for development servers, but it's so hard to get it. Cuz you just have to, what I said here, you have to get mercury and retrograde and you have to align the stars for everything to work, but when it works, it's amazing.

[00:00:41]
That's kind of the problem that dev containers are aiming to solve, that you don't have to have these bespoke, highly artisanal development environments that you have to spend weeks setting up. Ideally, you can just sit down and say, Docker compose operas or something like that and everything just starts working on your computer.

[00:01:05]
>> Brian Holt: Not always one to one that way it's like kinda the dream of vagrant, does anyone remember vagrant? Or am I old now? I think I'm old now. Anyway, vagrant was HashiCorp's very first product. And when HashiCorp was really just Mitchell Hashimoto. Anyway, let's talk about this. So we're gonna build a Ruby on Rails project, and the first thing you should say is, Brian, you don't know Ruby and you are correct.

[00:01:35]
I do not know Ruby, but I chose this project specifically to prove you to the that it doesn't matter that I don't know Ruby, that we can get this set up with like no problem whatsoever. So I'm gonna copy this, I have this running locally, or I have all the files.

[00:01:52]
Really, all I did with this Rails project is just do like the Rails new project, I didn't do anything beyond that. Because again, I do not know Ruby. So I have it in personal/container-projects, there it is. And I have it, I think it's in, is it in dev containers?

[00:02:18]
Is that where I put it?
>> Brian Holt: So all right, that's fine.
>> Brian Holt: Docker-volume, difficulty
>> Brian Holt: All right, so now I have my dev containers project, and we're gonna open that in code, I'll close that one. So spoiler alert, but we'll get there in just a second.
>> Brian Holt: Yeah, so if you do this in VS Code, which you don't have to, but I am, obviously, for purely unbiased reasons.

[00:03:10]
It's gonna ask you, do you wanna reopen this in a container? So I would say yes, to do it. I am gonna show you just really quick, cuz this used to frustrate me. If you accidentally missed that, but you still wanted to do it, that's what this little, logo down here in the bottom left, this green one.

[00:03:30]
You can just say Reopen in Container, if you click on that. You can also do it through the command palette. So command P or,
>> Brian Holt: Ctrl+P, and then just put the angle bracket there, and then you can say reopen in container, that also works as well. Anyway, we're gonna do it here, reopen a container.

[00:03:53]
It's gonna restart it, you can say, it's gonna open all of this stuff.
>> Brian Holt: It's gonna do all of my installations, it's gonna do a bunch of stuff. It's gonna have a problem with my Ruby version, apparently.
>> Brian Holt: Global, sure, I don't know, whatever, Ruby. I'm sure I'm offending so many Ruby developers, and I don't care.

[00:04:23]
Okay, that's fine.
>> Brian Holt: It wants my Ruby file in particular.
>> Brian Holt: Anyway,
>> Brian Holt: So point being, were you all seeing that as well the same kind of Ruby error? Okay, good to know.
>> Brian Holt: But you can see here I am inside of a Ruby environment. If I say cat /etc/ issue.

[00:05:01]
You can see that I am inside of Debian 11. Yeah, let me get through what the notes say here.
>> Brian Holt: An interesting, I don't think I have Ruby installed in my computer, but if I say ruby -v, you can see it's on ruby 3.3, that's good.
>> Brian Holt: And I should be able to say rails server
>> Brian Holt: And then it should ask you do you wanna open this in Browser?

[00:05:38]
You can say Open in Browser. And this is all now running directly from a container on my computer. Again, as you saw here, I do not know what I am doing, right? But it's pretty cool that someone smarter than me can come before me and set all this stuff up so that I can just say, yes, reopen in container and just be an off into the races.

[00:06:04]
Now I have to admit my bias here, I helped with this project when I worked at Microsoft. This is a very Microsoft initiative, but they have kind of spun it out. It's now owned by GitHub and not by Microsoft. And it works with JetBrains. There's ways to make it work with Vim, there's even a CLI now that if you don't wanna be working anywhere near an IDE, all that kind of stuff still works.

[00:06:28]
So let's just kind of talk a little bit about this. If I'm gonna be working on a project with anyone that I know, I generally will set up a DEF container for them so that they kinda have this experience. I'll even set it up for myself so that every time I open my project, it just says, hey, do you wanna reopen this container?

[00:06:44]
And I just say, yes, and I'll set them in my development environment. So this one I actually didn't have to build my own Docker file for. I was able to just basically pull directly from the Microsoft dev containers cuz I have a bunch of them that are just like ready to go.

[00:07:02]
You can see here it's pulling from mcr, that's Microsoft Container Registry, and it's pulling the ruby 3 bullseye. It's telling it directly up front, it's, hey, you're gonna have to forward port 3,000, it'll detect that even if you don't tell it that. And then on whenever you create the project, just run this setup file and that'll get all of your dependencies installed automatically for you.

[00:07:30]
Another cool feature here that we're not showing you, you can also set it up with VS code to install the correct extensions. So let's say you're in Python and you wanna have Pylance. Or if you're doing Jupyter notebooks, you can have the Jupyter one that and it'll just install those automatically inside of the container, and that'll all just work.

[00:07:49]
And then, let's look at the remote explorer. So you can see here I'm running this dev container here. It'll tell me a bunch of stuff about what I'm doing with my dev container. So there's a bunch of stuff you can play around here directly from Microsoft as well.

[00:08:09]
Pretty cool feature, I think. One thing I was gonna say, if you can create totally custom docker files as well for your dev containers, right? You don't have to use the Microsoft ones. Generally, they're pretty good. So I generally, start with the Microsoft one, and then I build on top of it.

[00:08:26]
But if you have some wild bespoke environment that you need, that's all totally possible here as well. And in case, it just bears mentioning, your dev container will not be the same as your production one. It's just good to say that out loud. In retrospect, it might seem a little obvious, but it's okay that your project has multiple docker files in it, okay.

[00:08:56]
>> Brian Holt: Cool, and then whenever I close this, it just shuts it all down for me. I don't really have to think about it, because I closed my VS code, it's just gone now. So let's look for a second here at the bottom, I showed you everything in VS code.

[00:09:12]
That's because out of the out of the box, everything just works with VS code. You'll have to install an extension, but it should prompt you to install that extension. So that shouldn't be that big of a deal. There's a dev container CLI that handles all this stuff kind of locally for you without being attached to an IDE.

[00:09:31]
This all works in Visual Studio, probably no surprise there. IntelliJ now has support for this as of last year some time. And then the other thing that I find extremely useful with this is that, if you decide to open something in Code Spaces, it just works, right? Code Spaces will see as, this person has a dev container and then it'll make whatever container that your GitHub Code Spaces is running in use that dev container.

[00:10:02]
If you're not familiar with Code Spaces, it's, if I go to github/btholds/completeintro-to-containers-V2. And I want it to just work on this without downloading it. I can just say, create Code Space.
>> Brian Holt: And this will open VS Code, this will open a real running Linux environment. This one does not have a dev container that I haven't put in there, so it has a generic universal image there.

[00:10:34]
But pretty cool, I can just start working on this directly, and it's free, right? It's free for up to 60 hours per month.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now