Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Development dependencies in Dockerfile or separate Dockerfiles for production and testing

I'm not sure if I should create different Dockerfile files for my Node.js app. One for production without the development dependencies and one for testing with the development dependencies included.

Or one file which is basically the development Dockerfile.dev. Then main difference of both files is the npm install command:

Production:

FROM ...
...
RUN npm install --quiet --production
...
CMD ...

Development/Test:

FROM ...
...
RUN npm install
...
CMD ...

The question arises because I want to be able to run my tests inside the container via docker run command. Therefore I need the test dependencies (typically dev dependencies for me).

Seems a little bit odd to put dependencies not needed in production into the image. On the other hand creating/maintaining a second Dockerfile.dev which just minor differences seems also not right. So what is the a good practise for this kind of problem.

like image 467
kgalli Avatar asked Dec 21 '15 13:12

kgalli


People also ask

Is Docker used in development or production?

Docker Compose is an excellent tool for optimizing the process of creating development, testing, staging, and production environments.

Where should I put Dockerfiles in project?

The best way is to put the Dockerfile inside the empty directory and then add only the application and configuration files required for building the docker image. To increase the build's performance, you can exclude files and directories by adding a . dockerignore file to that directory as well.

Can you have multiple Dockerfiles in a project?

Introduction. Docker is a handy tool for containerization. It's so useful that sometimes, we want to have more than one Dockerfile in the project. Unfortunately, this goes against the straightforward convention of naming all Dockerfiles just “Dockerfile”.

What is the best approach to speed up the installation process of application dependencies in a Docker?

The easiest way to increase the speed of your Docker image build is by specifying a cached image that can be used for subsequent builds. You can specify the cached image by adding the --cache-from argument in your build config file, which will instruct Docker to build using that image as a cache source.


1 Answers

No, you don't need to have different Dockerfiles and in fact you should avoid that.

The goal of docker is to ship your app in an immutable, well tested artifact (docker images) which is identical for production and test and even dev.

Why? Because if you build different artifacts for test and production how can you guarantee what you have already tested is working in production too? you can't because they are two different things.

Given all that, if by test you mean unit tests, then you can mount your source code inside docker container and run tests without building any docker images. And that's fine. Remember you can build image for tests but that terribly slow and makes development quiet difficult and slow which is not good at all. Then if your test passed you can build you app container safely.

But if you mean acceptance test that actually needs to run against your running application then you should create one image for your app (only one) and run tests in another container (mount test source code for example) and run tests against that container. This obviously means what your build for your app is different for npm installs for your tests.

I hope this gives you some over view.

like image 54
Boynux Avatar answered Oct 18 '22 11:10

Boynux