You've got your Python API or app running in a Docker container.
Great.
Are you ready to ship it to that hosted container service and head off to production?
Not so fast.
Have you considered how you'll manage evolving the dependencies and addressing security updates
over time?
Not just for the base OS, but for the installed packages?
How about your pip installed dependencies?
Are you running as root?
If you don't know, the answer is yes.
We'll discuss these and many more issues with Itamar Turner-Trowing on this episode.
It's Talk Python To Me.
Episode 323, recorded June 14th, 2021.
Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem,
and the personalities.
This is your host, Michael Kennedy.
Follow me on Twitter where I'm @mkennedy.
And keep up with the show and listen to past episodes at talkpython.fm.
And follow the show on Twitter via at Talk Python.
This episode is brought to you by Sentry and Linode.
And the transcripts are brought to you by Assembly AI.
Please check out what they're offering during their segments.
It really helps support the show.
Hi, all.
I have a quick announcement before we dive into the interview.
Over at Talk Python Training, we just released our latest course, Python-powered chat apps with Twilio and Syngrid.
Have you ever wanted to create a chatbot using Python?
In this course, we'll be building an ordering system for a tech-savvy bakery.
Our customers can place orders over WhatsApp.
And during the chat back and forth, we'll integrate data from our Flask API, answering questions such as, what's the menu?
What does that cost?
And so on.
Then we'll integrate this into a sweet backend that sends receipts as PDFs over emails and allows our bakers to see new orders and then mark them as fulfilled and notify the customers when they're done.
In short, it's cool tech and a super fun project.
Oh, and one more thing.
This course is free for everyone.
So if this six-hour course sounds fun, just click the link in your podcast player's show notes and jump into the course.
Now, on to the Docker best practices.
Itamar, welcome to Talk Python to Me.
Welcome back to Talk Python to Me.
It's been some time.
Feels like a year or so.
I'm not sure exactly how long it's been.
Last time we were on, we were talking about an entirely different topic.
So you get two bits on the mind map connective sort of relationship of topics here.
We talked about fill and profiling data science.
That was fun.
Yeah.
I sort of have found myself talking about a bunch of different subjects and like some people are interested in both.
Some people are interested in the other.
And Docker is the other thing I've spent a lot of time sort of researching and writing about.
Yeah, it's I think the data science profiling one was really interesting because profiling has all these challenges and much of it is more focused around profiling running applications or profiling code that's all in Python.
And so if you need to profile like, say, Fortran code or other weird sort of mix and match libraries, then that was sort of that topic, right?
Yeah, so feel as a memory profiler for Python and the kind of and specifically for batch processes like data science, scientific computing.
And so if you're doing scientific computing, they'll there'll be a bunch of code in Fortran and C++ and Rust.
And so you want to access that memory, like sort of profile memory across all the languages you're using.
Yeah, because if you've got some big glob of C code, Python thinks it's just a pointer, that little tiny pointer to some, but it turns out to be huge.
Yeah, so people can check out that episode.
They're interested.
And yeah, just give us an update on what you've been doing since then.
I've actually been trying to turn feel into a sort of make an alternative version of it that you can run in production.
Profilers often have performance overheads of feel take like 40% performance off the top.
Trying to make something that will run with like 1%, 2% overhead.
So you can run on production and just always get reports about your memory usage for any job.
So if it's like six hours in it, it crashes out of crashes or just uses too much memory, go back and look at that.
Oh, that would be fantastic.
We have that for like profiling in terms of performance.
On some systems, you can plug them in and they'll kind of give you real time.
How is my app doing in terms of, you know, here's where it's spending its time or it got slower.
Maybe it's even just measuring like request response.
But memory profiling has typically been pretty intensive, right?
So that'd be cool if you could get it down to that level.
Yeah, and this is a very good pen.
Both Phil and this project are very good.
Pandemic projects.
It's like really, it's quite difficult to do.
And it's like, but it's something that is sort of completely under my control.
Like we'll get to Docker and Docker is like, there's this giant ecosystem and they all have
differing opinions about how you do things.
And like everything's so broken around the edges.
Whereas here it's like, I have a box and it's a very complicated box, but it's under my control.
And so I can do so.
It's kind of relaxing in an environment where the world is not under my control.
It's been a crazy time, hasn't it?
Yeah.
I feel like we're getting used to it.
It's odd, but you know, people just get used to whatever water they swim in eventually, I guess.
Yeah.
Let's talk about Docker a little bit.
So it hasn't been that long since I had, that's, I also want to shout out quick episode 274 is when
we talked about Phil, people want to go back and check that out.
But I had Peter McKee from Docker over there to come and talk about sort of what is Docker, you know, give us an update on Docker, the company and like sort of set the stage for Python developers, right?
To kind of get going on the dev side and just start using Docker.
So that was episode 308 and that was fun.
But then recently you gave a talk at PyCon called Zero to Production Ready, a best practices process for Docker packaging.
And so I thought that was really interesting and wanted to have you on the show so we could dive into Docker best practices for Python, but also your focus is really on production, not necessarily development, right?
Yeah.
Maybe we start there.
Like what's Docker look like for software development as a, I just need to make my stuff run so I can code it and test it out versus, I don't know, zero downtime Kubernetes or whatever it is you're trying to do type of thing.
What do those two worlds look like and maybe tell folks about when they should care about what are the advantages or whatever.
Yeah.
So what Docker gives you is a sort of package that contains all the files you need for the file system, contains Python.
It contains all the system libraries you need to run your Python extensions.
It contains all your Python dependencies, contains all your code and contains a script to launch your code.
And so as a starting point, this is useful for development because if you're say on macOS or in Windows and you're deploying to Linux, you can run something locally that is the same on across different computers.
Even if you are on Linux, like I have one machine that's Fedora 33, I have another machine that's Ubuntu, like they're different in a bunch of subtle and lots of subtle ways.
And so by having a Docker container when I'm developing, I can have a completely consistent environment.
And then know that that environment when I, when I then take that code and run in production, it'll be exactly the same there.
Right.
I had a, somebody reach out to me a little while ago and ask something to the effect of, I've got a bunch of different developers on my team and I want to make sure that they all have the same version of Python in the same packages.
Right.
And that's a legitimate thing that you might want to do.
You might want to make sure that those are exactly the same.
I think maybe in general, there's probably more of a concern about that than an actual problem there.
You know, a lot of times either these things are going to basically work or they're going to utterly fail.
I think one of the scenarios maybe where it matters more is data science, where there's slight changes in algorithms, which might lead to different, you know, ways.
You train the model, which might lean to different, like it could, those kinds of changes, but say in like web apps or UI apps or something like that, it's, it's either just going to work or it's going to completely break.
That said, you know, this situation you're talking about with Docker for development, like kind of solves that, but to a much bigger degree.
Right.
Cause you could specify in this image, we have exactly this version of Python compiled in this way.
We have these libraries installed with this version.
We have these environment variables set and this subsystem of Linux installed as well, but not that other way.
You can completely control it way more than just, I want the same version of Python, right?
And you can then go further with something like Docker compose or compose lets you start up a little network of containers.
And then it's very easy to say, okay, I want to spin up Postgres or I want to spin up Redis.
Whereas traditionally this would be a pain in the ass with Docker and Docker compose.
You can spin up a little, all your dependency servers really easily.
So then even if you're not using Docker for your own code, you can use Docker for the services
you depend on to really easily spin them up.
Right.
I need Redis running in this way and I need Postgres in that way.
And I just need them all configured and to be able to talk.
So Docker compose up, right?
Something like that.
Yeah.
Docker compose is a way to sort of run a little network of services, makes that really easily.
Yeah.
Another big advantage before we get off of the development side of things is onboarding new
people and new hardware, right?
If you've got something really complicated like that and you get somebody on the team, instead
of spending a lot of time trying to get their system put together in the right way, you just
go, sell Docker, do this.
Yeah.
There's, there's open source projects where like you can set up the development environment
that will also provide a Docker file to just let you run some tests easily, just because
you're a ongoing, you're only submitting one patch.
You want to run some, like some tests on it.
You don't want to go the whole thing.
So it's very nice when they provide a way to run the code in Docker.
Yeah, absolutely.
That said, I don't generally do my development in Docker.
I just, just have virtual environments and roll with that.
So it's not always required.
A couple of thoughts from folks out in the live stream.
Kim Van Wick.
Hey Kim.
Says Docker Compose is an excellent way to make sure all the developers are using the same
tools and versions.
And it's just much easier to pass around a YAML file.
Yeah.
Compose is like, I remember when Compose first came out and it was called Fig, I think.
And it was, it took Docker from something really neat to something really useful.
Yeah.
Right.
The promise of Docker is that I can have all these different, if I want to run, like we just
described, I want to run Redis.
I want to run maybe a Celery backend.
I want to run Postgres.
And then my dev code is going to run and talk to all that.
Well, keeping those all up to date, you know, start making sure they all build those files
and they all run and maybe they run in the right order.
Well, that all of a sudden isn't fun anymore.
But if you can create a Compose file and just say, here's the set of containers that needs
to work together, bring them all up in the right order and make sure they're all up to
date, you know, got their recent build and so on.
Like that's a whole nother level of the promise of containers.
Yeah.
Also, I don't know if you know anything about this.
I'll maybe take a wild guess here.
But Daniel Chen out in the live stream also says, question, is there in Windows, is there
any difference between WSL 2 for the Docker backend compared to Hyper-V or is Hyper-V more
for backwards compatibility legacy support?
I, in general, don't use Windows that much.
But basically, there's two, like, since you're running Linux and Windows is not Linux, you
need to have some way of running Linux.
In the past, the way you do that, you would run a virtual machine.
This is what it does in macOS 2, I believe.
Windows subsystem for Linux is a way to transparently run Linux applications on Windows and Docker supports
it these days.
I suspect they would be faster, but that's just a guess.
So I don't have a good...
Yeah, that's my thought as well, that they would be a little more integrated.
Probably you could more easily do things like mount Windows file system folders from your
Docker container.
I don't know.
Maybe you still can't with the others.
Probably you can.
But...
I'd expect to be faster at least, yeah.
Yeah.
It seems like if you want to run on Linux, you're probably...
I think you're probably closer.
It's definitely more lightweight.
Hyper-V would be running a full-on Linux VM and then hosting Docker in that, I'm pretty
sure.
Yeah.
Cool.
All right.
Well, hopefully our guesses there, Daniel, are helpful.
All right.
Well, let's talk about your talk that you gave at PyCon.
I mean, giving talks today at conferences, like we started the show off, like it's a weird
world, right?
I'm giving a talk and both doing a live stream podcast on a conference tomorrow at the Manning
Developer Productivity Conference.
How can I do that?
We record it.
We publish it.
And then we have a live Q&A afterwards.
So the presentation of my recording will be during the Python bytes recording tomorrow,
but then the live interactive bit will be actually after.
So yeah, that's the commerce world we live in.
And so PyCon this year was virtual.
You put together a really nice presentation sort of in this format.
And yeah, like I said, I got a lot out of it and I liked what you covered there.
Yeah.
And so the starting point is you have the service, now you want to run it in production.
And this is a very dramatic departure from running things locally, because locally, the
thing you're prioritizing is basically your feedback loop, like your development feedback
loop.
Like if you're a web developer, you do like it and you save your code, like can you reload
the page to have the new stuff running?
Other applications of feedback loops are a little different.
But like your goal is just as quickly as possible to interact with your code.
When you're in production, you have to worry about a whole bunch of other issues because
it's actually, you have users who are going to be interacting with the software or the data
it's emitting will be used in the real world.
It's no longer just something you're working on.
It's a thing that actually has some, the output actually has some weights and meaning, some
importance.
You have to approach it in a different way.
So some of the things that come to mind here would be downtime, you know, in a perfect
world, zero downtime in a reasonable world, a couple of seconds of downtime in the world
of some bizarre web companies that I cannot, I literally cannot understand eight hours of
downtime because we're deploying the new version of the site.
So Sunday it'll be down.
Like what?
I just literally, I got this a while, you know, a couple of months ago for something
I was using.
There's going to be hours of downtime for a site.
That's just, do, do, as you said, do you upgrade to the new version of the site?
That should be a button folks.
That should be not long.
Anyway, one of the things is downtime, right?
You want to focus on that and you don't care about that at all with development.
I mean, you want it to be somewhat responsive, but it doesn't matter if it's down for a moment.
This portion of Talk Python Army is brought to you by Sentry.
How would you like to remove a little stress from your life?
Do you worry that users might be having difficulties or are encountering errors in your app right
now?
Would you even know it until they send that support email?
How much better would it be to have the error and performance details immediately sent to
you, including the call stack and values of local variables and the active user recorded
in that report?
With Sentry, this is not only possible, it's simple.
In fact, we use Sentry on all the Talk Python web properties.
We've actually fixed a bug triggered by a user and had the upgrade ready to roll out as we
got their support email.
That was a great email to write back.
We saw your error and have already rolled out the fix.
Imagine their surprise.
Surprise and delight your users today.
Create your Sentry account at talkpython.fm/sentry.
And if you sign up with the code talkpython2021, it's good for two months of Sentry's team plan,
which will give you up to 20 times as many monthly events as well as other features.
So just use that code talkpython2021 as your promo code when you sign up.
Another one that you made a big deal out of that can matter is security.
You don't want to be in the newspaper or the news website front pages for leaking the largest
data breach ever or something like that, right?
Yeah, that's embarrassing.
Basically, once you're packaging for production, you're at the intersection of a whole bunch
of processes.
This is where it starts getting complicated.
You're coding, and then you have this image, and then you might want to run some tests with
it, maybe integration tests, and you're going to deploy it.
And then when you deploy it, you might be upgrading an existing package, existing server.
It's a server.
It's a batch process.
Things are a little different.
So there's deployment, and then things might go wrong in production, and then you might
have some sort of feedback mechanism, and maybe you're going to try to reproduce the
bug locally.
So all of these different technological organizational processes have some interact in some way with
your packaging.
And so it basically makes it a lot more complicated.
And then you add into it all the different technologies that are intersecting in packaging.
There's a lot of details to get right, and it gets complicated very quickly.
Another area that you want to get into has to do with making sure that you're running
the latest version, but you're not necessarily every deployment just grabbing the latest version.
So you need some way to inject stability, and you need some way that that stability doesn't
lock in computer vulnerabilities or any of those kinds of issues.
It also allows it to keep growing, right?
Yeah.
And this is sort of one of the more significant examples, but an example of the bigger picture,
which is packaging is a process.
And so it's not just about writing some configuration files.
It's going to interact with the way you write code, and it's a thing that parts of it are going
to continue over time.
So when you're packaging for production, you're not just writing a few config files and calling
it a day.
You actually need to think about, need to set up these ongoing processes for things
like security updates and for things like dependency updates.
Right.
It's one thing to get it running on a cluster, a container cluster.
It's another to say, and here's how we're going to keep this software healthy and running
over time, right?
Yeah.
You need to sort of think through the implications of what you're doing, and it's not just a
one-off thing.
It's an ongoing thing.
Yeah, absolutely.
All right.
Let's dive into some of the details.
So it turns out I discovered today, as I was pulling up your website, that you've actually
written a whole bunch of stuff about production-ready Docker packaging, and that you're actually
working on a handbook.
I end up doing this a lot as well.
I end up, I'll spend a month doing tons of research and examples and thinking about a course,
and I'm like, oh, there's a couple of nice presentations or conference talks I could pull
out of here.
And yeah, it's a good way to do it, right?
So you've been thinking a lot about this, not just for this talk, but beyond, right?
Yeah.
I'm sort of spending two years on it so far.
Like, I have three different products up there.
I've done training.
There's like a lot of articles these days.
It just adds up.
And yeah, I've spent a lot of time looking into this because it turns out it's, I should say,
I don't like Docker packaging.
Okay.
This isn't a thing I'm doing because this is fun.
It's not actually fun.
It's kind of a pain.
It's just, it's very useful and it's very easy to get it wrong or to miss things.
And so what I've been trying to do is sit and say, here's this really useful thing.
Here are the details you need to get right.
And now that I've written it down, you don't have to waste your time trying to figure this
out because much of it is not, it's really useful, but it's not like, you don't feel like
you're a better person for having figured this out.
It's just, it's the getting, it's an obstacle.
And I'm trying to get people past those obstacles that can use this useful technology.
Yeah.
Well, there's a lot of stuff that you talk about that is not necessarily something that
would be front of mind, like security, like how to manage the versioning over time and
so on.
But I think also it would be quite satisfying to have, you know, take something that's janky
and maybe it does have that like one hour banner on Sunday, we're going to be down from three
to four upgrading our site and, and be able to remove that and say, no, we just deploy a
couple of times a week now.
We don't think about it because it's get push prod and then wait 30 seconds.
And then prod is now the new one.
Right.
Yeah.
I think that's a very good feeling.
Yeah.
And there's a bunch more you need to do that than what I talk about.
I'm talking about one piece.
There's also the deployment process and that there's having good observability and
logging, even just the packaging part of it.
Like there's a lot of details to get right that can make it a lot easier.
Yeah.
So the way you started the presentation, your first thought for this was that packaging,
this whole Docker as production, packaging your app and Docker for production is an iterative
process and maybe also layers, right?
Like, so you sit, you don't necessarily start with the whole, well, we want zero downtime.
You start with, can we make it run in Docker?
Yeah.
And you don't have to do it as an iterative process.
Like if you can manage to keep all this in your head, which honestly, I can't, but there's
too many details.
Or if I were to start Dockerizing something, I would probably do a bunch of it in one go
because I remember some of it.
But if you're doing this at your job and you probably are like someone's, there's going
to be an emergency or someone's going to pull you over or there's going to be a bug that
you have to fix.
And so you're going to get pulled away at some point.
And so what you'd like to do is build your packaging in a way where if you're interrupted,
you have to stop, you have to put it on hold.
You can put it aside and know you're a good stopping port and you want to sort of prioritize
the most important ways.
So if you run, like you may just run out of time budget, like you just have limited time
and you want to do the highest priority things first.
And if you ever have more time, you can-
Well, it sounds like what you're describing is a little bit of what happened to the software
development side of things when it went from waterfall to agile or waterfall to something
better, right?
So many projects used to say, well, what we're going to do is we're going to build it until
we're going to work on it for six months.
And it's not going to be actually usable in any meaningful way until that six month point.
And then maybe it drags on and it goes over budget and gets canceled.
And there's just all sorts of, you know, you get no user feedback.
There's all these kinds of problems trying to build software.
So it doesn't surprise me that there would be lots of advantages to trying to apply that
same sort of iterative thinking.
Like, let's make sure that each step along the way, we have something that's useful and
more useful than it was before.
Yeah.
And also, even if you know you have the time budget to do all of it, having a good understanding
of the priorities means you can focus on the really important things.
Yeah.
Like large images are very visible.
Like it's very easy.
Like you can look at your image and say, why is this two gigabyte?
Like this is ridiculous.
And then you can go into this rabbit hole of like trying to make your image smaller.
And that's a fine thing to do.
But if you deploy an image that's insecure and nice and small, that's not the best trade-off.
I would venture to say that your organization would not praise you for your efforts to make
it small.
But rather, they would be pretty upset about the security problems, right?
Yeah.
And so like security seems like a high priority, like automation's high priority.
And the order you might actually do it, it might be slightly different depending on your
particular domain you're working on.
Right.
But yeah, you want to think about.
It's a bit of a priority stack, right?
Like what is most important to me or what is most foundational to the whole, this whole
packaging and Docker process.
Yeah.
So you actually put up some six points that you thought were sort of stages in your talk.
Yeah.
Get something working.
That's just, can you use Docker at all?
Obviously, if it doesn't work, it's not going to be useful.
And then number two, even before continuous integration, security.
I can see that people would overlook that, but that's not trivial, right?
Yeah.
And like security is sort of a never ending thing, like, because you have to deal with security
updates.
But if you want to, like, you don't need complete automation to like spin up a server somewhere
and test something.
Like if you're using Heroku, you can like push a Docker image to Heroku and spin up a server.
Yeah.
You could manually do a get push.
Yeah.
You can push stuff.
But, and then try it out.
But if it's talking to like a production database and it's not secure, that's the problem.
And packaging is only one small part of the security and most of it's going to be application
security, but it still comes into it and it's still important.
Right.
Little Bobby tables is still a problem, even if it's running in Docker.
Yes.
That's right.
Okay.
So number two is security.
And then number three is continuous integration.
So making sure that like when we check in code that it's tested in Docker, guessing the tester
run, the system, something of a Docker compose up type of thing happens and it's all good.
Then correctness and debugability.
So correctness is obvious, right?
It needs to work.
It needs to have fresh data.
It can't have like stale caches and weird things like that.
But debugability is interesting.
Maybe you want to focus on that for just a sec?
Sure.
So the idea is that like once you have automated builds, you might start actually running things
for product in production for real.
Or even if not, you're going to have, if you're building an image for every pull request, now
you have a bunch of images.
And so someone files a bug.
How do you know what version of your code, like which Docker image, do they know which Docker
image they were using?
What version of the code it matches to?
If something crashes, like, are you going to get actual, are you going to get logs that someone
can report or not?
If you don't go to the effort of exporting the volume that has where the logs get written,
every new deploy gets a new fresh set of logs.
Yeah.
So yeah, like logging to standard out or standard error is the other way you deal with logs in
Docker.
But you need to like put some minimal thought into like, where are my logs going to go?
Yeah.
Another thing that gets really tricky around that kind of stuff, I feel, has to do with
the fact that there's just so many moving parts a lot of times.
You know, you've got your Celery Docker container.
You've got your Redis Docker container.
You've got your Postgres Docker container.
You've got your app Docker container.
You're doing microservices, like who knows how many.
And then all of those things have logs.
Do you use anything, any of the services that try to bring all those logs into one place?
Not a huge amount of experience on any particular one.
I have, or I've built a system like that, which these days I wouldn't recommend using, but
it's more for this point only for scientific computing.
Yeah.
But before the log as a service, log aggregation as a service was a thing.
Yeah.
I've worked on like airline reservation system.
I had one of these.
And this was a like eye opener for me being able to see logs going between five services
in like multiple different protocols.
So you could make debugging vastly simpler.
Yeah.
So you really like anything that lets you trace across services with a tracing ID will
make your, like once you have more than one service, it'll make your life much easier.
However, I, my recommendation is avoid microservices unless your company has 500 people or something
like that.
Yeah.
It's an amazing architectural design pattern when there, there needs to be autonomy for
different parts of the application.
Like this team works on the front end little bits here.
This team works on the user authentication and identity part APIs.
And, but you know, looking at the most recent PSF JetBrains survey, that is not the number
of employees, number of people on a team type of description for most Python developers.
It's like a handful of folks all working on a lot of it.
Right.
Yeah.
And if you look at the companies that are doing microservices successfully at scale,
they will have a team of, you know, three to five people working on one service.
So if you have a team of five people, 20 services, you are doing like a hundred times more services
per developer than the big companies.
Yeah.
And that's a lot of complexity you've just added to your life and it is often unnecessary.
Yeah.
Here, let's, I'll pull up the survey.
So Python developer survey 2020 results.
If you search in that for team, not team city, employment work, working in a team versus
working independently, about half the people work on a team.
But if you look at the team size, 75% are two to seven.
Yeah.
That should be one microservice.
Yeah.
It's really microservices.
And there are applications that are actually, there are distributed systems where you actually,
it makes sense to do more, but like any time you make something more distributed, you
are adding a vast amount of complexity.
And so if you can avoid it, avoid it.
Yeah.
Well, here's the way that I think about it.
I think about microservices.
They have a tons of value and they move where the complexity of your application lives.
So what you end up with is very simple, relatively simple, small, easy bits of code.
But what you also end up with is a much more complex deployment DevOps coordination story.
So when I think about microservices, the farther you go towards microservices, the more you're
taking the code complexity of a large app and architectural patterns and separation.
And you're saying, well, we don't need any of that.
Let's make it real simple.
And we move that complexity to coordinating a bunch of services that are always up, that are
debuggable across services and versioned and all those things.
And when I think about that for me, I'm way better at software complexity than I am at deployment
complexity.
So I'm more successful not putting it where I don't have my experience or skillset.
You know?
It's actually software complexity too, because if you call a function, you're going to call
a function.
If you send a message to another service, it may never arrive.
It may be delayed arbitrarily.
Yeah.
And so the communication becomes and the reliability of a thing you're calling, the switch from a
function within the same process to remote service is a sort of huge increase in unpredictability
and sources of error.
That's a good point.
Because as for many things that could go wrong with, say, calling some function or a
sort of class level method, not having it get called is not one of the things you'd have
to worry about.
Yeah.
Right?
You might crash because the file system isn't there, isn't accessible.
The database isn't there.
But it's not that you couldn't even call it, right?
That's going to happen.
Yeah.
Before we move on, Kim says, FileBeat, Logstash, Prometheus, and Protainer.
Oh, Protainer.
I've never heard of that one.
Can all help with logs from Docker in various ways.
Awesome.
Good resources to check out.
Okay.
Last one on your quick hit list before we dive into some of the details is faster builds
and smaller images.
I think we skipped reproducible builds.
Who needs reproducibility?
Yeah.
Let's go with reproducibility.
Yeah.
And so this is the static versus dynamic change of dependencies that you talked about,
where on the one hand, you really don't want every time you reinstall your application to
get the latest dependencies because a new version of Django comes out.
You don't want your code to suddenly start running on it just because it came out.
Right.
And maybe you're not aware, right?
Because you have the older version of Django, as you suggest, and you're working on it.
You get push.
It goes to CI.
And then that ideally is going to like some sort of continuous delivery and it's grabbed
just the latest, which is not what you had.
And then it runs with it.
Like that could be bad news.
Yeah.
This happens a lot of like dev tools.
And then like, oh, I'm going to go check if there's been a new release today.
And the matter is less.
But when it's your actual production software, that's not, oh, I wasted 20 minutes figuring
out a failed build.
It's my code is acting weird in production.
On the other hand, if you just freeze all your dependencies and never change them, then
at some point you're going to be running on a version of software from two or three or
five years ago.
Like I've, the extreme cases, like there are organizations still running on Python too.
And this is, becomes very problematic because upgrades become more and more terrifying the more
you put them off.
Because it's not just like Django.
It's like, you have to upgrade Python and Django and three other major libraries you depend
on.
And it's, it's like this project.
And it's a project that's not features or bug fixers or anything.
It's just risk.
But if you put it off more, then it's more risk.
And so you need a process that's ongoing.
So you need both in the short term to be, make sure your builds are identical, reproducible
and long or mostly identical.
And then the longterm you need the process to continuously update so that upgrades are
not this terrifying thing.
They're just a standard part of your development process.
Yeah.
I had Carlton and Will on from Django chat a while ago talking about deployment.
And we came up with the idea that there are basically two types of applications.
There's ones that you're going to continue to add features to, and you're going to care
about, and you have a, maybe a team dedicated to it.
And for those, you never want to be that far from the latest thing.
Just like you described, the farther you get, the more frightening and the more potential
problems you have if we take on the latest, right?
Because if you're on Django 2 and it's Django 4.5 is out in some future world, you're like,
well, we finally need to move to get that because the other one's gone fully unsupported.
Well, that's like you said, as a project.
Because if you're always kind of just in dev, sort of rolling into later one and then deciding
to roll that out, like that's a much smaller challenge.
So those should absolutely stay there.
Also, we talked about a set of apps, a type of app that falls into the, please don't
touch it.
And if you do touch it and break it, it's now your baby.
It's some horrible legacy code.
The person who created it probably doesn't work at the company anymore.
Nobody really likes it.
It's not important, but it needs to be there.
Like it's some internal app or something, right?
Like maybe those, you just freeze those in time.
They're very likely not public facing or something.
But certainly if you care about continuing to work on this thing and adding features to
it and it matters, then keep it not too far.
That's the tension.
You don't want to just constantly ship the latest thing because maybe that's a major release of
some library, but at the same time, you don't want to freeze it.
This portion of Talk Python to me is sponsored by Linode.
Visit talkpython.fm/Linode to see why Linode has been voted the top infrastructure
as a service provider by both G2 and TrustRadius.
From their award-winning support, which is offered 24, 7, 365 to every level of user,
to the ease of use and setup, it's clear why developers have been trusting Linode for
projects both big and small since 2003.
Deploy your entire application stack with Linode's one-click app marketplace or build
it all from scratch and manage everything yourself with supported centralized tools like Terraform.
Linode offers the best price-to-performance value for all compute instances, including GPUs
as well as block storage, Kubernetes, and their upcoming bare metal release.
Linode makes cloud computing fast, simple, and affordable, allowing you to focus on your
projects, not your infrastructure.
Visit talkpython.fm/Linode and sign up with your Google account, your GitHub account,
or your email address, and you'll get $100 in credit.
That's talkpython.fm/Linode, or just click the link in your podcast player's show notes.
And thank them for supporting Talk Python.
Quick comment back to the monolith.
Tolines says, can you speak to microservices versus monolith, in particular for ML applications?
I think that's a little bit different.
I haven't really thought of it from an ML perspective.
You got thoughts?
My decent, a decent rule of thumb is, are you working on a web application where there's
hundreds of developers working on that application?
And the answer is yes.
Then someone in the organization is going to bring up microservices.
Anything smaller than that, just don't think about it.
Once you're small enough, I tend to feel the same way about Kubernetes.
There's a lot of technologies for a company with 5,000 people or 500 people or 50 people
or five people or one person.
Each organizational size, you're going to want different technologies because different
architectures because, when I say applications, we'll have 500 developers or five developers
because your ability to specialize, your ability to build infrastructure are different.
And so if it's a thing that an organization that has thousands of developers working, like,
are you building Pinterest?
Probably not.
Then the technology sources Pinterest makes may not be relevant to you.
Yeah.
I think maybe another consideration is how much is part of that functionality shared?
Are you building an API that has some models that make some prediction that a whole bunch
of your company and different apps and websites and such might need?
And, you know, maybe that's its own thing.
But if it's only being shared in one place, maybe not.
There's an interesting article that was from 2019.
Might be worth people checking out.
It's called Give Me Back My Monolith from Craig Kirsten's.
Anyway, I'm not going to go into it here, but it's kind of an interesting read.
People can check that out if they want.
All right.
We talked about faster builds and small images now.
We're there.
Tell us about that.
People who are new to Docker, haven't done a lot with Docker.
There's a lot of things you can do to result in a smaller, physically a smaller image size,
right?
A smaller file on disk.
Yeah.
It's very easy to get a giant image in Docker because the Docker image format is basically
in many ways like a Git history.
So every time you make a change, it's not overriding.
It's adding.
So there's a history there.
The history is always there.
So if you delete a file, it doesn't make the Docker image any smaller if it was added in
a previous layer.
Right.
If it was added in a different layer, that's right.
Yeah.
If you structure things right, there's a bunch you can do to make your images smaller.
And similarly, Docker has a bunch of features to allow you to not have to run pip install
every single time you rebuild your image because the dependencies haven't changed.
So it can just cache those files for you, but you have to set it up right.
So it does that.
And you can go from a half an hour build to a one minute build depending on how you built
your Docker image.
So LP Linux being my current favorite example, although maybe that's going to get fixed over
the next year.
One of the things that's super interesting about that as well is the ordering as well as the
grouping of those commands can really matter.
So for example, if the first thing that you do in your Docker file is to copy over your source
code, then the next thing to do is to do an apps update, apps upgrade.
Then the next thing to do is to install the dependencies and so on and so on.
Right.
Every time any file that you're working on, even like an unrelated CSS file changes, everything
below that has to be rebuilt.
Right.
And if you make a point to say, well, let's reorder that.
So the very last thing we do is copy our files over.
Then as you make changes, like you won't, those other layers will just be up to date.
And one cool trick that I've seen that you can make that even better is somewhere in that
intermediate bit.
You could even copy your requirements or pyproject.toml file over and then install those and then
copy the rest of your code over, which looks like a stupid duplication.
Like, why are you just copying this one file?
You already copied it in the next one, but you can cache that pip install, pip compile step,
make it faster.
Yeah.
But basically, if you understand how Docker caching works, then you can sort of structure
Docker file in the right way.
And then you get caching.
Then you need extra steps to get it working in CI, but you can get a much faster build.
So it'll work.
The fast scene is super clear.
Tell us about smaller though.
What do you really focus on for smaller?
So some of it is just things where various packaging tools are optimized for development
by default.
So if you do pip install and you do something like NumPy, this can be, or if you're, let's
say, let's go, the big packages, like things like TensorFlow, like these packages, some of
these packages are hundreds of megabytes.
Like they're just huge.
So you download the package and then it unpacks it and installs all the files.
And then by default, pip will keep a copy of that downloaded file in a directory.
Probably the intermediate build output.
The wheel file you downloaded.
Potentially all that stuff.
Yeah.
It'll keep a copy of the wheel file.
The idea is like you might be doing another virtual end in another two hours.
And so when you do pip install, this time it can just, it doesn't have to download it.
It can just use the cache version.
And for development, that's great.
But for a Docker image, you are never going to call pip install again.
So keeping this file is just like an extra 400 megabytes of this space.
And so there's a command line option for pip install.
It says --no cache there.
Yeah.
And then it doesn't keep a copy.
And now you're, you haven't, you're free to put a bunch of space by adding, which can be
fairly considerable if you're doing certain, especially for data science tools.
Right.
You add it up across all the dependencies and their transitive dependencies and so on.
Right.
Yeah.
That's like just not storing files you don't need.
One of the mind shifts you got to get into to work with this stuff is you will never, ever
change the Docker configuration.
Right.
It's not like, oh, there's updates to Linux.
So I'm going to go in and like app update it, or there's an update to my requirements.
So I'm going to reinstall the requirements.
You recreate a new Docker image and you throw away the old Docker image.
Right.
So there's a lot of the things that are there to make that next step.
Right.
Pip installing, again, work well are just liabilities and negative effects on your Docker image.
Right.
Yeah.
Because Docker images are sort of designed to be treated as immutable artifacts, which is
sort of great.
But also like you're dealing with a whole bunch of tools that don't really have that assumption.
And so you have to figure out ways to make those two conflicting goals work together.
Yeah.
Another interesting thing that Peter brought up, Peter McKee in the other episode we did
not too long ago, was intermediate frameworks and all sorts of stuff.
It doesn't apply super well to the Python space, but maybe there's certain aspects, especially
in the data science side that might.
So for example, if you're going to install like the development setup for Python, not
just the ability to run, but to do pip installs and do all sorts of things.
The example he gave was if you're going to have something that runs in Go, well, one of the
steps you might install might do is like, well, we're going to install the Go compiler and
all that business.
And we're going to compile the artifact.
And then you would try to run it.
It's like, you create a separate container that will take the code and compile it and
give you the binary, just copy the binary without the compiler back in there.
So maybe there's some techniques like that.
I don't know.
I mean, I don't see it quite as well in Python because we can't fully package it up as reliably.
But yeah.
So a common, so this happens a bunch if you're compiling your own custom C extensions.
Yeah.
So one way you can deal with that is like, you can have a thing that generates wheels and
then like you build your Docker image, you just download the compiled wheel.
But if you want to do it in your Docker image, you're going to have to install a compiler,
but then you don't, that compiler package is going to be in your final image.
So it just makes your image bigger.
You don't need GCC.
And so you can use a multi-stage build, which is probably what he was describing.
Yeah.
So the one easy way to do that is you create a virtual env, install all your code, and then
you copy just the virtual env into a new Docker image.
And then the new Docker image just has the resulting self-contained virtual env, doesn't
have any of the compilers that you needed to build it.
Right.
No matter what else might've been over there.
Yeah.
Maybe you could even use something like PEX if you really cared to like compile that or
to bundle that into a zip and then run that directly.
I'm not sure, but possibly.
Yeah.
Tim out there in the live stream says, dash dash, no cash dir has made my evening.
Thanks.
I never thought about it.
The intermediate files from pip only from app.
Yeah.
Then also Docker on build option can help a bit with that scenario.
Yeah.
But not including the dev tools.
Okay.
That's cool.
I may have deprecated on builds, but I could be misremembering.
That would be too bad because I just learned about it.
Okay.
Those are the six things that you talk about in this iterative process or this layered process.
It's like step one, your first deliverable or your first package of this as Docker sprint
would be get something working in either the single container or the suite of containers
from Docker Compose.
Step two is make sure they're secure.
Step three, getting them running in CI.
Step four, make sure that they're correct and debuggable.
Number five is reproducibility with that balance of not exactly the latest, but not super old
and stale.
And then finally fast builds and small images.
Yeah.
And then along the way, there's a whole bunch of different things you can do depending what
tools you're using and like what's your priorities are.
And yeah.
Can maybe give some examples if we have time.
Yeah, absolutely.
We got a little bit of time.
I thought that'd be fun.
So we could dive into just get something working, which is like a hand line of a couple of lines
in Docker, like see Docker symbol.
Yeah.
You choose a base image, copy your code and run pip install and say, this is what I want
you to run when you start up.
And for many applications, I'll do the trick.
So I'm always wondering what is a good container base to start from, right?
So you have this Python 3.9 Slim Buster version as the base.
Yeah.
There's a bunch of different options, right?
What do you think?
So the first thing is you want a, these are all based on Linux distributions typically.
So you want a Linux distribution that's some sort of long-term support where like they are
both guaranteeing backwards compatibility in terms of like binary ABIs, but also in terms
of features, but they're also doing security backboards.
So like Debian stable, Ubuntu long-term support, Red Hat Enterprise Linux, they all are going
to give you this stability guarantee.
They'll say, we'll give you a stable operating system with security updates to it.
So you want something that's based on one of those probably.
And then you need to, you typically are going to want a up-to-date Python.
And these distributions will sometimes like backport, new versions of Python.
And so you can use that.
So you can say, I'm going to use like Ubuntu long-term support from 2020.
And like it has Python 3.8 and maybe they just added 3.9.
I'm not sure.
I saw something to that effect.
And then you can go with that.
Or Docker maintains these things for the official, in quotes, Docker images for Python.
And basically what they do is they take Debian stable and then they compile all the different
versions of Python for it.
So you can get 3.7 or 3.8 or 3.9 and 3.10 when it comes out, regardless of what's in Debian
stable.
So it's Debian stable plus an extra Python.
So Python colon 3.9 is Debian stable plus 3.9.
Then they have two variants.
One has a bunch of extra packages and one has fewer.
One with fewer packages is the dash slim.
And then the dash buster is which version of Debian you're using.
And the reason you don't have to specify that, but like maybe like at the end of the year,
maybe early next year, there's going to be a new version of Debian stable.
And so you don't want overnight to go from Debian 10 to Debian 11 as your base image.
You would probably want to just at least do that consciously, right?
And so saying dash buster means I want to stick to Debian 10 buster.
And for those who don't know, Debian Linux releases are based on Toy Story characters.
Nice.
So Buster is one of them, right?
Yeah.
And I don't remember what the next one is.
There's Debian unstable, which is named CID.
It's always Debian unstable and it's always CID.
They never release it.
That's cool.
All right.
So in this example, it's the Docker file says from Python colon 3.9 dash slim dash buster,
which means all the stuff that you described there.
And then you copy your files over, you run pip install to install the dependencies.
And then you just basically start your app as the entry point.
And that is, hey, we got something working.
This is probably an oversimplification.
There might be a database thing that also needs to start up and run its bits and so on.
But yeah, that's pretty much it, right?
It's pretty simple.
Typically pretty simple to just get everything's working because it's just install some packages
and then tell it to run the scripts when you run a container.
Yeah.
It's pretty much whatever you need to do to get a new machine set up to run this,
do that in this file and you're good to go.
Yeah.
And I guess seeing a comment on the chat and I should add that as far as I can tell,
Docker on build is not deprecated, but I'm not sure.
So maybe.
All right.
I'll have to look into it.
Sounds good.
So getting it working is super straightforward.
Yep.
But getting something secure is interesting.
Let me go back.
I think we might be skipping around a bit,
but you're talking about having that version specified there of Slim Buster.
I know how we'll get new dependencies for the Python code up there.
And if there's some kind of security problem,
what will probably happen is Dependabot on GitHub will send me a PR that says,
warning, warning, your version of web framework has such and such CVE.
We've created a PR.
You accept it.
You push it back to the right branch.
That kicks off the whole process and everything goes again, right?
That keeps like the flow of the somewhat fresh code and dependencies going through your system.
However, how do I keep that same thing happening for Linux, right?
Suppose Linux has some security vulnerability in the version that I've got,
or I've got Nginx running and it has something like that I need to update.
Like, what is the trigger that helps me know?
Like, what is the process that helps me know?
Oh, you need to, even if this is a somewhat stable, stale project that we haven't touched for a month,
you need to somehow go give it a kick to like force it to get the latest and do that again.
Because there's no auto app upgrade running there.
Yeah.
So one thing to note is some people assume that the Debian,
like the official Python or even official Debian or whatever,
the official base images from Docker get security updates every time they come out.
They don't.
Some of them get updated fairly frequently.
Some of them, like the CentOS one, which I guess people are probably switching away from,
but for a while it was a lot of people probably using it.
The CentOS base image will not be updated for months at a time.
And so they are relying on, and the Debian ones,
well, I have seen them like lag on security updates by two weeks.
So Debian has released a new security update, but the Docker image hasn't been updated.
Which is not ideal because you're telling all the hackers,
here's the problem that you can just go look for in systems that lag on getting their patches.
Yeah.
And so you as someone who is creating a Docker image cannot rely on the base images to be up to date.
You need to install security updates when you build your Docker image.
But what's that look like?
The step two is apt update, apt upgrade, dash Y, or something like that?
Yeah.
apt get update, and then apt get minus Y upgrade.
You can add a few more command line options to make your image smaller.
But yeah, it's basically you do an upgrade.
But there's a problem.
Docker has, as we talked about, has this caching thing,
where if you rebuild an image and nothing's changed, it will just use a cache layer.
So when you rebuild your image, if you are using caching to speed up builds,
it'll look at Docker apt get, look at the apt get update, apt get upgrade,
and say, well, this is unchanged.
Same command.
And so it'll just use a cache layer.
Yeah.
And you absolutely will very likely be doing that.
Because it's like five minutes versus three seconds to restart and build and test your app.
So everyone is going to be using the caching.
Maybe not CI, CD, but everywhere else.
I mean, you probably typically will want it in CI, CD too.
And so the result is that if you've set up caching to speed things up,
that caching will ensure you don't get security updates.
And so basically what you have to do is just have this process where once a day or once a week,
or in response to CVs coming out, you rebuild your image from scratch without caching.
So you can just say every night at 3 a.m. when no one's working,
we are going to rebuild our image from scratch without caching.
And so our image will always have the latest security updates.
And then if you're in a system that has continuous deployment,
you can then just automatically deploy that.
Wait, how do you make the little banner that says you're going to be down Sunday from 3 to 5
when you do that part?
Just kidding.
Yeah.
So this is easier if you have a process that you trust enough to do automatic deploys like anytime you want.
But you basically have to rebuild your image from scratch without caching,
either whenever a security update comes out or just on a regular basis and redeploy if it's a server,
because you have these immutable artifacts that if you're running a VM,
like you can just have like a cron job that installs security updates nightly.
Like there's unattended upgrades package in Debian.
For Docker images, you can't do that.
And so you have to rebuild from scratch with security updates and then redeploy.
And this is an ongoing process.
Yeah, I'm glad you pointed out the caching because it's not enough to go out and say,
oh, every once a day or once a week, we'll just do a Docker build.
Oh, it's up to date.
Actually, we're good.
Yeah, no.
Not so much.
Yeah.
And that's why I brought this up because I think it's tricky.
Like there's a natural flow that like kicks that refresh cycle off for code,
but not for the infrastructure itself, unless you think about it.
Yeah.
So you need to explicitly think about and set up these processes,
either some way to get notified of CVs
or you can probably, if you have a bunch of registries, have security scanners.
They'll scan your images for security problems.
And so you can run those in a schedule, maybe.
Honestly, the easiest though is probably just do a forced rebuild 5 a.m. or something.
Yeah.
And that next time every developer that comes in and runs the command Docker compose up,
going to do a Docker build.
It's going to see the things out of date and it'll just trigger a let's get the fresh.
Yeah.
And it turns out security scanners are also have some,
bad defaults.
So you'll get,
there's a lot of security problems that are not really problems.
Like the upstream maintainer has closed it as won't fix,
or it's not going to get fixed in Debian stable until the next release of Debian stable.
And so the Debian maintainers are basically have decided that it is not worth fixing.
And so there's nothing you can do.
Most security scanners will flag those.
And so you'll run a scanner on an updated image and it'll say,
you have 60 security vulnerabilities.
But you,
if you turn on the flag that says only tell me about security vulnerabilities that I can actually fix,
that actually have updates from Debian.
And then you run that and it'll say you're fine.
And that is probably a much more realistic assessment of your risk because it's like,
there are bugs that are never going to be fixed because the GLAB C maintainers have said,
no,
won't fix.
This is not our problem.
It's like,
it's not a real bug.
Yeah.
I suspect you could also get notified about things that like are not observable really.
So,
oh,
there's a problem in the system,
but we actually have a firewall blocking that port and we have no interaction with it.
Right.
It's like,
how much do you worry about those kinds of things?
You may as well upgrade and redeploy because maybe one day your firewall will fail,
but like there's a whole bunch of just like utter noise if you don't configure your securities
kind of correctly.
Yeah.
All right.
Wrapping up this bit of the topic,
Kim says forced rebuild is a great for your own images based on Debian or other OS.
You probably still need some kind of scanning.
Yeah.
If you're not able to build it yourself.
Yeah.
Makes sense.
All right.
We got a little bit of time to touch on a couple of things.
One of the areas stage two was security.
You always want different layers.
I talked about a firewall.
We're talking about security updates and patches,
but there's layers of security you want.
And one of the very straightforward ones is you probably don't want to run this as root.
And like certain systems will even warn you about this.
So if I try to pseudo brew something on my Mac,
it'll complain.
Like you should never,
ever run brew as root.
What are you doing?
Are you crazy?
Stop doing that.
I think micro whiskey might warn that you're running this root.
If you look at the logs when you started up.
So when I run Docker and I just get that simple,
get started one,
what does that run as?
Yeah.
So by default,
Docker runs as root.
Oh,
okay.
That kind of makes sense because all these system packages are designed to be installed as
root.
And so if you're going to install system packages or install security updates,
you have to be a root by default.
But as soon as you've,
you've switched to like installing your Python code,
you should stop being root and create a new user and switch that user because otherwise
your application will be running as root and root inside a container is more restricted,
restricted, but it's still not as restricted as a normal user.
And different runtime systems might take more aggressive steps to restrict what you can do.
And so sometimes it might be okay, but just a good best practice.
You're, you don't know where your things are going to run.
Things might change around.
Just don't run as root.
What you're saying is basically,
if you run as root in a Docker container and somebody takes over your container,
well, the worst thing they can do is like crash around inside of the container.
It's not like they now have full access to the machine, but, you know,
maybe those rights are propagated onward.
Like maybe they can do something else to, I don't know,
decrypt something that then gets them further in the network.
Like there's, there's challenges that could happen, right?
So it is much easier to escape a container and onto the host if you're running as root,
because you, like in Linux, security access is granted by these things called capabilities.
And if you're root, you have a little bit more capabilities.
It gives you a larger attack service on Linux kernel.
And so if there's a bug in the Linux kernel, it's easier to take it over if you're root.
There are other things you can do to like restrict all capabilities to containers,
even if you're running as a normal user.
So like if you're running the ping utility, for example, it gets a little bit of extra,
it gets an extra capability often.
So it can do a ping.
And then if there's a bug in the ping command, then you can sort of,
and you can insert code into it somehow,
then it'll execute it with elevated privileges and you can do more stuff.
And so.
Yeah.
You don't want that.
Yeah.
And so you want to like run as a normal user that will restrict the attack surface on the Linux kernel,
like removing all capabilities or restrict the attack surface even more.
And you do these things.
And for many applications, it won't really matter too much,
but it's very, it's not a lot of work.
And it's like a little bit more assurance that if someone does somehow take over,
you'll limit the damage they can cause because they're only restricted to this container.
Yeah.
Okay.
Good advice.
And that uses the add user Docker command.
Very cool.
And then let's see, what was the next one here?
We talked about the security updates.
Like that's, that's a challenge.
So what do I need to think about for continuous integration automated builds,
specifically with regard to Docker?
Is there anything special?
Like.
So first it's just doing the actual work of automating it.
So like you really, it's really nice if every time you push to your Git repository,
every time you pull it across, it builds a Docker image for you.
Because then like you can test it.
Maybe you can write additional tests to actually use a Docker image,
do integration tests, that sort of thing.
Yeah.
Like for example, there was a really cool framework or library.
I can't remember exactly what it is.
We talked about it on Python Byte.
That instead of trying to mock out, say your database or,
it was mostly databases.
There's a Docker, there's like a testing library you can use that will bring up a Docker container
running Mongo or Postgres or something,
and then fill it with test data.
And you just connect those things and say,
yeah, you can talk to the database.
You don't care.
It's test data.
Might as well.
Yeah.
Testing with a real database is so much easier these days.
It'd just be the default.
Like you shouldn't be, if you're didn't deploy with Postgres,
you shouldn't test with SQLite because like they're different enough
that there'll be bugs that you're going to miss.
Yeah.
And so once you have that automation of like building for every pull request,
you don't, you start having this issue where you don't want the image you built
for the feature one, two, three branch to overwrite your production image.
That would be awkward.
But you would still like continuous integration to do its job and say,
you checked in this thing.
It was okay or not okay.
Yeah.
And it's useful to have like images uploaded for every pull request that you can download
and maybe play around with it.
But you don't want feature branches images to interfere with your production image.
One easy way to do that is to name your Docker images based on the Git branch.
So like you can just use the Git branch as the part after the colon, the tag.
So it can be like your image colon main, if it's the main branch or your image colon feature
one, two, three, it's the feature one, two, three branch.
Yeah.
That works really well with like Git flow feature branch style programming as well.
I created an issue.
Then I create a branch named something along those lines.
Then I create a PR along those lines.
And oh, guess what?
Here's the container that goes with that thing, right?
Yeah.
And you can also do things like name your Docker image based on the Git commit.
So you can sort of go from Git commit to corresponding Docker image really easily.
Yeah.
That's a really cool idea.
I did find that package, by the way, in case people are interested.
It's called test containers dash Python.
Anyway, the idea is you just say like with my SQL container, do your tests.
And it like literally creates a Docker container with your test data and all that stuff.
So people can check that out.
That's kind of cool.
All right.
Well, we're getting a little bit short on time here.
What else do you want to throw out for people who are thinking about a lot of these best practices?
We touched on a lot of them, but I know there's plenty more to go.
Like for example, faster builds.
You talk about, say, pre-compiling the PYC files.
Yeah.
That's more for that actually do slower builds, but it'll give you a faster startup.
That's what I mean.
Yeah.
Sorry.
Since this comes up a bunch, Alpine Linux is not a thing you want, is often not a thing you
want to use for your Docker base image.
And the reason is Alpine Linux is highly recommended for if you want small images.
And it's kind of nice because you install, like installing the Alpine packages somehow, I don't
know what they do, but it's vastly faster than like installing Debian packages.
And you get small images and it's kind of nice.
Problem is Alpine Linux uses a different standard C library than most Linux distributions.
Most Linux distributions use Glybcy.
Alpine uses Musil or Musil.
I don't know how to pronounce it.
And binary wheels are compiled by default on Linux for Glybcy.
And so if you install Python packages on Alpine Linux, you will not get binary wheels.
You're going to have to compile them from scratch.
And so what happens is people say, oh, I'm going to use Alpine Linux.
It's going to make my images smaller.
And they try to install like a Postgres package, which is pre-compiled and doesn't work.
They're like, okay, so now I'll install a compiler and I'll install the Postgres headers.
Now you have this image that has compiler and Postgres headers in it, and you have to compile
stuff.
And like when you get to like data science or scientific computing, you're like compiling
these massive packages that take a really long time to compile and all your builds are super
slow.
And you can do a whole bunch of work to then use a multi-stage build so your image is small.
And then you can use caching so the builds are fast.
And then, but, or you can just use a different Linux distribution and use binary wheels.
Don't cause yourself the challenge.
Just use something else, huh?
But there is a PEP that I believe was accepted to start the process of building wheels for Alpine.
And I've started seeing some packaging tools who started adding support.
And so it may be that in a year or two, it'll be just like everyone builds binary wheels for
like many Linux, just the Glibsie.
People might start building wheels, binary wheels for Alpine.
And when that happens, there'll be much less of an issue.
But until then, avoid using Alpine Linux as your base image.
Yeah.
You want to close it out with a PYC thing?
Sure.
So when you start up a Python program, it loads in your Python source files and then compiles them.
And compilation here is not really the same as compiling a C extension.
It's basically a one-to-one translation.
It compiles them to bytecode and writes them out as PYC files.
And then the next time you start, it can just load the PYC file and that will make your application
start up quickly.
And so if you're doing some sort of like, many applications, it doesn't matter.
If you're doing like a serverless kind of thing where like you want things to start up
really quickly, like having to compile the PYC is, it's going to add some startup time.
I guess any times where the container lifetime, the life cycle is short, right?
Let's say with a web app, you would start it and it would run for hours.
And so it doesn't matter, right?
Yeah.
So it's like it took you another 20 milliseconds to start off.
It's going to be run for three days.
But if you're doing like a serverless thing where like 20 milliseconds might be a significant
chunk of the latency of your service.
So when you build your Docker image, you can pre-compile all your PYC files and then it'll
be in the image and you won't have, then your startup will be faster.
And the reason you have to actually think about this is that Docker images are immutable.
So like your container starts up, it compiles and writes the PYCs, but those PYCs never make
it to the original image.
Every time you start the image, it has the same immutable artifact, unlike your local home directory.
And so if you really want the fastest startup, you can make your image a bit larger and compile
the PYCs.
Basically that becomes a step in your Docker build files to compile the PYCs ahead of time.
Yeah.
Okay.
Awesome.
Great advice.
Many, many tips.
I think we're going to have to leave it there.
We're getting basically running out of time, but yeah, really nice talk.
I'll link to your talk that you did at PyCon and thanks for coming here and sharing the audio
version with us.
Thanks for inviting me.
Of course.
Before you get out of here though, there's the final two questions.
If you're going to write some Python code, what editor do you use?
I use SpaceMax, which is kind of like they took Emacs and they configured it like 20 years.
That's like you're jumping 20 years into the future.
It's like it's Emacs, but with all the things you need pre-configured to actually have a
nice development environment.
And it has VI bindings and Emacs bindings.
I use the Emacs bindings.
But if you like Vim, you can use the VI bindings.
Yeah.
Cool.
Their subtitle and sub-subtitle is a community-driven Emacs distribution.
The best editor is neither Emacs nor Vim.
It's Emacs and Vim.
I honestly don't use the Vim bindings at all.
I'm using it for like, it does all the IDE stuff you want out of the box.
So it's just, it's a much more modern experience.
Okay.
Really cool.
And then notable PyPI package?
Py03, which is a way to create Python extensions in Rust.
I've used it to create both for Phil, my memory profiler, but also I wrapped some Rust library
with it.
It's really, really nice way to create fast, safe extensions for Python.
And it comes, there's a packaging tool called Maturin, M-A-T-U-R-I-N, which was probably the
nicest Python packaging I've experienced I've ever had.
Like you add like three lines, like you had a PyProject line file, which is like three
lines.
You add like a tiny bit of metadata and now you can build wheels and you can pip install
and it just works.
And it's just amazingly smooth development experience.
That's fantastic.
Yeah.
So basically if you're going to write a C extensions, maybe reconsider that and write
them in Rust and use this possible.
Yeah.
It's like Rust is like, gives you the same performance that you would get from C or C++, but it's
much safer.
And as someone used to write C++ long ago, like I, I've learned it in the, over the past
couple of years.
And it's like, it is the language I always wanted C++ to be.
Yeah.
I hear you.
I did a lot of C++ as well.
I was always bringing in these things like smart pointers and other stuff.
It's like, why does it have to be hard?
Can't we just like make this better?
It's not a simple language because if you want performance, you need to do work.
And it, the way it has a very different paradigm, but it's really lovely language.
You'll write much safer code and Py03 makes it really nice to write Python extensions.
Yeah.
Cool.
And then Talden's out there has a interesting comment.
Is that like an isotope?
I think it's a reference to like oxidization, like O3, but.
Yeah.
Because it rusts.
And there's a lot of oxidizing happening.
Yeah.
Around.
There's also PyOxidizer.
Yeah.
There's another project.
Yeah.
That's like a completely different project, but yeah, it's another rust pun.
Packaging.
Yeah, exactly.
Very, very cool.
All right.
Final call to action.
People are interested in this.
They want to go deeper.
You've got some various things that you can find on your website, pythonspeed.com slash
Docker.
Yep.
Where do they go?
What do you tell them?
Yeah.
So if you go to pythonspeed.com slash Docker, there's a whole bunch of free articles about
various best practices.
If you're specifically interested in the process that we covered today, there's a PyCon talk,
but also if you go to pythonspeed.com slash Docker process, it's also linked on that page.
It's like an introduction to Dockerizing for production.
It's basically like a little mini ebook I wrote that's about 10 pages, but it goes over
this process that we talked about today and sort of prose and talks about sort of the
decisions you have to make and how it integrates your organizational processes.
That's super cool.
Yeah.
And on that site, the slash Docker, it has a bunch of articles and it has a very small
scroll bar and a lot of stuff below it.
So yeah, there's a lot of things going on.
People can go check out for more resources there, right?
Yeah.
And I have a bunch of paid products if anyone's interested about Docker packaging from intro
to much more detailed one.
And if you use the code talkpython, you can get a 15% discount.
Oh, fantastic.
Awesome.
Yeah.
So be sure to do that.
Thank you so much for being on the show and sharing a lot of your hard-earned Docker experience.
Yeah.
Thanks for inviting me.
You bet.
Great to talk to you.
You too.
Bye.
This has been another episode of Talk Python to Me.
Our guest in this episode was Itamar Turner-Trowing.
It has been brought to you by Sentry, Linode, and Assembly AI.
Take some stress out of your life.
Get notified immediately about errors in your web applications with Sentry.
Just visit talkpython.fm/sentry and get started for free.
And use the promo code talkpython2021 when you sign up.
Simplify your infrastructure and cut your cloud bills in half with Linode's Linux virtual machines.
Develop, deploy, and scale your modern applications faster and easier.
Visit talkpython.fm/Linode and click the Create Free Account button to get started.
Transcripts for this and all of our episodes are brought to you by Assembly AI.
Do you need a great automatic speech-to-text API?
Get human-level accuracy in just a few lines of code.
Visit talkpython.fm/assemblyai.
Want to level up your Python?
We have one of the largest catalogs of Python video courses over at Talk Python.
Our content ranges from true beginners to deeply advanced topics like memory and async.
And best of all, there's not a subscription in sight.
Check it out for yourself at training.talkpython.fm.
Be sure to subscribe to the show, open your favorite podcast app, and search for Python.
We should be right at the top.
You can also find the iTunes feed at /itunes, the Google Play feed at /play,
and the direct RSS feed at /rss on talkpython.fm.
We're live streaming most of our recordings these days.
If you want to be part of the show and have your comments featured on the air,
be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
This is your host, Michael Kennedy.
Thanks so much for listening.
I really appreciate it.
Now get out there and write some Python code.
I'll see you next time.
Thank you.