Docker for development

Hi, it’s not entirely clear to me what is the flow for doing development into a Docker container.
Right now I find myself automatically rebuilding my image after every change, which is unlikely the right thing to do. From what I understood I should be able to start the container and, for example, run tests inside it after every change.
How can I accomplish that?

I can tell you what I do. TL;DR: I do most of my development outside of Docker, and do docker build frequently. (I don’t do the “everything in Docker all the time” thing that a lot of people seem to do, and my development process basically involves docker exec or interactive shells in containers.)

I have a good, comfortable, no-Docker-at-all development environment on my laptop. My IDE can work my local source code. I try to be good about writing unit tests as appropriate. I can run my application locally on my laptop and fire some requests at it using a browser or curl. No Docker.

Once all this works, I run docker build and turn it into a self-contained image. The application is built into the image. If I’m bind-mounting directories into the image with docker run -v, it’s either especially involved configuration files or directories that will receive log files. I can again fire some requests at it using a browser or curl. Once that works I can docker push the image, or commit my code and have our CI system build the image for me.

My current day job is using Kubernetes, so it’s especially important the images be self-contained. We have a developer cluster where I can launch things and test them against more of the combined system. This is a fairly late step in the process.

If things are going wrong and I’m already running them under Docker, usually my approach is to put some sort of debugging print statement in my code, rebuild, and redeploy the image. This takes a minute or two; if I’m running remotely then the bulk of the time is actually in uploading the image and waiting for Kubernetes to catch up.

My Docker images are exactly what I would run in production, and so they generally don’t include dedicated test code. I run unit tests locally before I commit; my CI system runs them too after I commit. We have some integration tests that make external requests against the service but they don’t depend on anything in the image itself, and we can run them against either a local copy of the service or something running in Kubernetes.

Looks quite problematic, because the application is configured to run in Docker. For example, the database points to “docker.for.mac.localhost”. I could use environments but does it mean that every time I want to run my tests (which is every 2 minutes) I need to rebuild the container? Ouch…