r/AskProgramming • u/Neuromante • Jun 12 '20
Why is docker a thing?
I'm genuinely curious here, not trying to ask a loaded question, is just that... I don't really understand why is so widespread, so I'll just go with my particular case:
I'm on $Big_Corp. We have many stuff obviously standardized, and for our web applications we have been told to use a dockerized database. I started a small project from scratch and to connect to a $Standard_Database I just downloaded $Standard_Database, installed it on my machine, configured it and began working with it.
Of course, this is on a development environment, so more or less, who cares how I got this set up, but I'm approaching a point in which I got to "get real" with the database, which implies setting up a docker image of $Standard_Database.
Now, for what I've seen and I'm reading, this is just some kind of container that will (in theory) only have that database, with docker itself running in memory for I don't really understand what. From the point of my application and my database-access program, I will only have to change the database connection info and I would be good to go.
I kind of understand a potential need for having independent systems and all that, but, as someone who really love continuous integration and has spent lot of time making processes that deploy cleanly, I don't really see the point on making a new layer just to have this database for this application.
Maybe I'm having here a too narrow view and in a bigger org is needed to keep things tidy (thus my case of use would be just "the way corporate has told us to do things", maybe I'm missing something here. And thus, I'm asking here about it.
9
Jun 12 '20
Many have covered it already, but adding to those answers. It’s like an isolated environment that guarantees it hasn’t been “contaminated” by your development work. Granted I see your point that it’s not something that should be used for every single use case, but once you get the hang of it, it can actually make development a lot easier. It’s like having an army of VMs on your machine without actually having the power of a VM server. It also really doesn’t kill memory because it usually only uses exactly what is needed to run the particular image you want to run. As opposed to installing a full DBS on your PC or a VM. Not to mention, you could easily dispose of a docker image. At the heart of it, it is meant to eliminate the “works on my machine” issues.
Take your example, you installed the $Standard_Database on your PC, configured it and have been working with it on your machine for a while, somewhere along the line maybe you changed some configuration to make your life easier. Maybe you adjusted a few things or for whatever reason, the $Standard_Database adjusted to your development environment. Now, you’re ready to deploy and everything works perfectly on your machine, and you deploy it to a fresh VM and something isn’t working. The VM doesn’t have your environment, the configuration changes you made, etc. but you may have not even changed anything or at least you don’t remember or you don’t realize what you changed because as a developer you may not be an expert in managing $Standard_Database which is no longer “Standard”.
Using Docker, you know you have the most basic environment and you could easily test this by setting up a new container with the same data which you could never do on your own PC in your own environment because even if you uninstall, it doesn’t guarantee that there is no residue from the previous installation.
2
u/Neuromante Jun 12 '20
Ah... gotta say I liked your answer the most (Got some stuff to read yet, so..) most of the time my development is focused to small stuff and I usually document almost everything, so reproducing it should be not issue (not work related, but on my raspberry is how I've set up everything I could just reinstall everything from scratch with not much issue), but I can see how a "standard database installation" can become less standard because I forgot to mention that you had to change this or because we are 10 guys working on the same project.
What I find interesting here is the performance part. Is it really "so good"? I mean, is "good enough" for a beefy server or is actually "good enough" for a limited resources environment? (Asking more towards something like my raspberry or similar, hah)
1
Jun 12 '20
As far as performance, that’s why I said it really depends on your use case. If it’s a smaller project, might be quicker and more efficient to set up locally.
I’m not sure about the specifics of the performance side on Docker, but in my experience it hasn’t been too much of a memory eater as long as you manage the containers well and close them when you’re done.
4
u/sbcretro Jun 12 '20
Docker + k8s enables on the fly, fully automated scaling. The ability to turn shit off when you don't need it is absolutely necessary for heavy duty services running in the cloud - you get high availability, but you only get (AND PAY FOR) EXACTLY what you need. You did that super bowl advertisement and are getting 1000% more traffic? Your infrastructure doesn't crumble (assuming you are using it correctly). It's off hours on a slow day and requests are almost non-existent? That shit can scale back down. And this happens within parameters (minimums, maximums, rules for when new instances get spun up) that you define.
With Docker and k8s, domains can easily break apart your services for security's sake. ALL your configuration for re-deploying your entire organization's infrastructure can be managed with scripts, which are just code that can be in version control with audit trails.
On the desktop, developers can work inside docker containers that have standard environments set up. A desktop tech can set up the software you need for a specific project, then that project's architect can dump a few configuration files (or more likely the desktop tech dumps the ones needed for that particular project that were created by the archetect), and you have a dev environment guaranteed to have exactly everything you need when working on said project even if your laptop takes a dump. No sitting through installer after installer, and no need to even run a full blown VM if the host OS supports the container type.
There is also all sorts of automated CI/CD that you can do as well, including spinning up temporary test environments when needed for security or load testing, or even just clean slates for running automation against.
Most of that is k8s AND docker, but that's where a lot of the power is. Even just Docker itself is useful for segregating services without requiring a full on, separate virtual machine.
Do you remember the old unix tool chroot? It's still used to this day when bootstrapping linux. A container is like a chroot environment, where exactly the needed files are hard-linked into the chroot folder, and the whole thing lives in its own little image file.
1
u/Neuromante Jun 12 '20
Huh, never ran into problems regarding scaling (this was usually the problem of other department), and I'm seeing myself having to care about this in the future. Care to share any article on the topic?
1
u/sbcretro Jun 12 '20
Sorry, you are going to have to look up autoscaling with k8s. It's not my area of expertise - I know build pipelines and integrating testing into them (that is my area of expertise). I wish I knew more.
1
u/Neuromante Jun 12 '20
No prob. We have a workshop next week on kubernetes, so I guess I'll be able to catch up from there!
1
u/flagbearer223 Jun 13 '20
The super cool thing about docker & k8s is that it turns configuring servers and infrastructure into pure programming (for the most part). It means that it no longer has to become a problem of the other department - your programming skills directly apply to building out Dockerfiles and k8s infrastructure definitions. Once you get over the hump of figuring out how to use them in a meaningful way, you no longer have a wall between operating the software and developing the software - you can do both :D
104
u/truh Jun 12 '20
Have you ever heard of the phrase works on my machine? Docker does a fairly good job of solving those issues.
28
u/YMK1234 Jun 12 '20
That's only one aspect ... also that you have all your application-relevant machine setup in your repository in the form of the docker-file is a huge thing. as you don't have two different teams (ops and devs) messing around on your target machine. And let's not even talk about easily scaling to more instances instead of (semi-)manually having to configure machines.
9
u/edgargonzalesII Jun 12 '20
You also have quicker startup than a VM, so a bit easier to spin up new clusters of needed.
8
Jun 12 '20 edited Jun 12 '20
To add to that, 1 small added benefits is that modifications to the codes are not permanent. Restarting the docker container will reload the project to a state that the development knew it was working. Turning it off and on works even better with Docker.
The primary draw for the docker for the application code, and having database in it is just simplifying the process by having everything in the same eco system. (You can easily ensure that the database cluster is up and running before application code is launched.).
Eidt: As /u/truh, pointed out, reCREATING the container will reset its contents. You don't have to wipe everything one restart. My projects have always been set up to remove containers on docker daemon service restart that I totally forgot do you don't have to wipe them.
4
u/truh Jun 12 '20
Restarting the docker container will reload the project to a state that the development knew it was working.
That's not true, only when you create a new container, not when you restart it.
7
Jun 12 '20
Thats actually not even that correct. Stuff that works on docker with linux containers under windows is not guaranteed to work on docker under linux if you get into some advanced stuff. Under the hood you are using the kernel resources, and kernels can differ in implementation.
If "works on your machine but doesn't work in prod" is an issue, the solution is to set up the dev environment to mirror prod, not to dockerize. Or alternatively go hardware virtualization with full VMs so you can control for things like kernels.
5
u/truh Jun 12 '20
Stuff that works on docker with linux containers under windows is not guaranteed to work on docker under linux if you get into some advanced stuff.
That's why I qualified it as doing just a fairly good job. I'm using docker for obscurish hardware, CUDA and realtimeish networking on different host OSs and are somewhat familiar with its limitations.
2
u/julianeone Jun 12 '20 edited Jun 13 '20
I know Docker is pretty serious about getting things to work in Windows; I interviewed there, and that was really clear. Under Nadella at Microsoft, I would think that's even more true. They are incentivized to play nicely with Windows. To the extent that they don't, they're not giving Windows users or companies a reason to use Docker, and believe me, Docker cares deeply about the Windows market, almost more than any former startup I've seen in the Bay Area.
2
Jun 12 '20
Yeah but in most cases you're comparing a development environment to a production environment, not your local development machine.
Anyone who goes from laptop directly to prod is being irresponsible anyway.
7
u/n1c0_ds Jun 12 '20
I can run exactly the same code and configs as the production environment. If it runs fine on my machine, it runs fine in production.
I can deploy the code on a new machine in under 5 minutes, and guarantee it's configured properly. I just need to run one command.
I can maintain 10 different projects on the same computer and never worry about conflicting software versions or configurations.
The last 5 years of config changes, performance tweaks and version upgrades are under source control.
If a web app gets hacked, it won't affect anything else on the host machine.
6
u/TrickyTramp Jun 12 '20
Docker really shines in the cloud. You can take a self contained unit of code or a database or whatever else and replicate it to scale infinitely.
1
u/amykamala Jun 12 '20
for web development a containerized local allows you to mimic the server environment that the website actually lives on, which is key unless all of your clients host on the exact same platform
1
u/bluefootedpig Jun 12 '20
So i'm going to bring in a little context to this, docker isn't new. It is how Macs handled programs. During the early days when memory was expensive, microsoft solved this by creating the GAC, a central place to store all dlls so that they can be shared. Why should you have more than 1 copy of .net on your computer?
What ended up happening is knowns as dll hell. There was many times when a dll was missing, it would be in the development machine GAC but not on the client machine, or they had the wrong version, etc.
Apple never cared, I think Steve Jobs had the idea to basically never worry about memory. It hurt it in the early days but as memory got cheap, it made more sense. Now memory and disk is so cheap, it makes the most sense. We should contain all data we need into a single container, and let that program load and run in an isolated environment.
The great thing about this is that everything is there, so you can unload it, zip it up, ship it to another computer, and continue running. I am not sure on Docker, but I imagine if not already, soon the call stack will be included so you can stop mid process, and move it to another computer. Microsoft's workflow framework did this back in like 2010.
1
u/Zeroflops Jun 12 '20
Something else people may not have mentioned.
Have you ever noticed over time your system slowing down and then you do a fresh install and it’s like a new system. Removing lots of old crap or previous info-installs that didn’t quite remove all the setting to paths etc.
By keeping thing in a container your base system is less impacted by changes and stays cleaner.
1
u/julianeone Jun 12 '20
The advantage of docker is 100% when you set it up for other people to use it on the Internet, or in other words: when you deploy it. So to answer this question, we have to consider how your app will be deployed.
On your machine, you have your app - which is, let's pretend, a node server and an index.js file - and you have your database - say, MongoDB. It runs great. For running where you are the only user and you're not connected to the Internet, it's great.
Now how do we run this online, for other people to use it?
Let's say we use AWS, we are AWS customers.
First, we create a virtual server on AWS, an EC2 instance; that holds our app. We install MongoDB on this EC2 instance.
But communicating directly with MongoDB ends up being kind of a pain, so we decide to add an API, a natural addition to any app. The app communicates with the API and the API communicates with the database - that's literally its purpose, to be the middleman.
So now our AWS setup has: an EC2 instance running our app (node server), our API (another node server), and our database, MongoDB, installed directly on the container.
But we've had problems with our app, and our EC2 instance, crashing before. (Note: I've never had a long-running EC2 instance not crash on me). Every time it crashes, we have to restart the app; sometimes its offline for a long time. How can we make it better, by making the app more stable?
One way would be to separate things out: to set up our EC2 instance, and then set up our database. We search on AWS and we decide to AWS DynamoDB, which is another NoSQL DB just like MongoDB. Then we use API Gateway on AWS to set up our API.
So now our "app" has three separate components, in 3 different places on AWS: our EC2 instance for the app, API Gateway for the API, and a DynamoDB table for the database.
But... wouldn't it be nice to simplify this, somehow? That's now three different places I have to go to modify my app.
One way would be to combine the EC2 instance and the API. That's what Docker is for! (We could even throw in the MongoDB instance too, though that's not recommended - it's better for that to be separate).
So now we create a Docker container that has the app (node server) and the API (another node server). It works great, and we've now simplified things.
And there's another advantage!
Remember how I said our EC2 instance would crash? That usually happens at the worst moments - when our app experiences heavy load.
We want to avoid this if at all possible. With Docker, we can.
If we get a ton of customers, now we have a simple container with our app and our API that can be scaled up. If we need 10 containers, no problem. Orchestrating containers is its own subject, but the point is, it's better than the solution we were using before, and it's a lot easier to upgrade.
I wrote up this answer here:
https://javascriptpage.com/why-use-docker
You might also find this reddit answer helpful:
https://www.reddit.com/r/webdev/comments/a285o4/why_and_when_to_use_docker/
1
u/myusernameisunique1 Jun 12 '20
Press button. Deploy Server.
What, You want another 10? Ok, I'll press button 10 times.
1
u/Ratstail91 Jun 13 '20
I'm fleshing out a project of mine to sustain a bigger workflow, and a friend is all over me about docker... but I don't get it either. It doesn't feel necessary to me at all.
0
0
Jun 12 '20
[deleted]
1
u/Neuromante Jun 12 '20
Ah, that's an interesting case of use. In $Big_Corp we have all standardized, but I did put the italics for a reason, as the reality is what it is. Will keep this in mind for the future, as I'm really here seeing a case that will come in handy, thanks for the reply!
0
Jun 12 '20
[deleted]
1
u/khoyo Jun 12 '20
OS isolation is containers. The idea is that containers use the host OS kernel for underlying functionality like accessing memory and network, but there is a virtual translation process.
You may be thinking of type 2 hypervisor - so VMs, just without hardware support. There is no translation process with containers, they work by using namespacing (and cgroups), but even processes not running in a container are subject to namespaces (they just run in the default one). They are essentially chroots but not only for filesystems.
This really only works with linux, because of the "everything is a file" design, so things like network devices are just simply files that you write to. This is why you can't run windows docker containers in linux, and under windows, any linux container runs in essentially a VM.
No, that's not the reason, which is why there is very little support on other UNIX systems. There reason is that Linux has support for namespaces and cgroups, and that docker was implemented using these Linux features.
The problem comes when people want to dockerize EVERYTHING. Dockers aren't the solution for running stuff locally just because you can.
Why ?
If you are running everything in docker, and then you take all your containers and run them on the same VM or server for production, you are wasting time with dockerizing as it doesn't give you any real advantages other than some vague notion of safety to protect against dumbass developers.
It still gives you a way out of dependency hell, and a reproducible environment between your local machine and your production server - even if it isn't the only way to achieve these.
The "vague notion of safety" is useful too - mitigating vulnerabilities and defense in depth are a thing...
33
u/[deleted] Jun 12 '20 edited Jun 12 '20
This is why - because you should care. A container contains all project dependencies (and configuration files, and environmental variables). These don't necessarily come with just installing a database. It's not just about isolation - its also about abstracting away all the worries of is application X compatible with application Y, config files, and environmental variables you just happen to have installed in your system.
85% of the time I set up a project on my dev machine, package it up (via rpm, for example), and install it in another environment - I've missed something. Some dependency that I have to go hunt down and install - and hope all the versions are compatible. Docker gives you certain guarantees regarding these issues. Namely, any incompatibilities are at the OS layer.