<p>Having needed several times in the last few days to upload a 1Gb image after some micro change, I can't help but wonder why there isnt a deploy path built into docker and related tech (e.g. k8s) to push <em>just the application files</em> (Dockerfile, docker-compose.yml and app related code) and have it build out the infrastructure <em>from within</em> the (live) docker host?</p>
<p>In other words, why do I have to upload an entire linux machine whenever I change my app code?</p>
<p>Isn't the whole point of Docker that the configs describe a purely deterministic infrastructure output? I can't even see why one would need to upload the whole container image unless they make changes to it manually, outside of Dockerfile, and then wish to upload that modified image. But that seems like bad practice at the very least...</p>
<p>Am I missing something or this just a peculiarity of the system?</p>

Hari Honor
<p>Good question.</p>
<p>Short answer:</p>
<p>Because storage is cheaper than processing power, building images &quot;Live&quot; might be complex, time-consuming and it might be unpredictable.</p>
<p>On your Kubernetes cluster, for example, you just want to pull &quot;cached&quot; layers of your image that you know that it works, and you just run it... In seconds instead of compiling binaries and downloading things (as you would specify in your Dockerfile).</p>
<p>About building images:</p>
<p>You don't have to build these images locally, you can use your CI/CD runners and run the <code>docker build</code> and <code>docker push</code> from the pipelines that run when you push your code to a git repository.</p>
<p>And also, if the image is too big you should look into ways of reducing its size by using <a href="https://docs.docker.com/build/building/multi-stage/" rel="nofollow noreferrer">multi-stage building</a>, using lighter/minimal base images, using few layers (for example multiple <code>RUN apt install</code> can be grouped to one <code>apt install</code> command listing multiple packages), and also by using <code>.dockerignore</code> to not ship unnecessary files to your image. And last read more about <a href="https://docs.docker.com/build/cache/" rel="nofollow noreferrer">caching in docker builds</a> as it may reduce the size of the layers you might be pushing when making changes.</p>
<hr />
<p>Long answer:</p>
<p>Think of the Dockerfile as the source code, and the Image as the final binary. I know it's a classic example.</p>
<p>But just consider how long it would take to build/compile the binary every time you want to use it (either by running it, or importing it as a library in a different piece of software). Then consider how indeterministic it would download the dependencies of that software, or compile them on different machines every time you run them.</p>
<p>You can take for example Node.js's Dockerfile:
<a href="https://github.com/nodejs/docker-node/blob/main/16/alpine3.16/Dockerfile" rel="nofollow noreferrer">https://github.com/nodejs/docker-node/blob/main/16/alpine3.16/Dockerfile</a></p>
<p>Which is based on Alpine: <a href="https://github.com/alpinelinux/docker-alpine" rel="nofollow noreferrer">https://github.com/alpinelinux/docker-alpine</a></p>
<p>You don't want your application to perform all operations specified in these files (and their scripts) on runtime before actually starting your applications as it might be unpredictable, time-consuming, and more complex than it should be (for example you'd require firewall exceptions for an Egress traffic to the internet from the cluster to download some dependencies which you don't know if they would be available).</p>
<p>You would instead just ship an image based on the base image you tested and built your code to run on. That image would be built and sent to the registry then k8s will run it as a black box, which might be predictable and deterministic.</p>
<p>Then about your point of how annoying it is to push huge docker images every time:</p>
<p>You might cut that size down by following some best practices and well designing your Dockerfile, for example:</p>
<ul>
<li>Reduce your layers, for example, pass multiple arguments whenever it's possible to commands, instead of re-running them multiple times.</li>
<li>Use multi-stage building, so you will only push the final image, not the stages you needed to build to compile and configure your application.</li>
<li>Avoid injecting data into your images, you can pass it later on-runtime to the containers.</li>
<li>Order your layers, so you would not have to re-build untouched layers when making changes.</li>
<li>Don't include unnecessary files, and use <code>.dockerignore</code>.</li>
</ul>
<p>And last but not least:</p>
<p>You don't have to push images from your machine, you can do it with CI/CD runners (for example <a href="https://github.com/docker/build-push-action" rel="nofollow noreferrer">build-push Github action</a>), or you can use your cloud provider's &quot;Cloud Build&quot; products (like <a href="https://cloud.google.com/build/docs/building/build-containers" rel="nofollow noreferrer">Cloud Build for GCP</a> and <a href="https://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html" rel="nofollow noreferrer">AWS CodeBuild</a>)</p>
