Development Dependencies in Dockerfile or Individual Docker Files for Production and Testing

I'm not sure if I need to create different Dockerfile for my Node.js. application One for production without development dependencies and one for testing with development dependencies included.

Or one file, which is mainly a development of Dockerfile.dev . Then the main difference between both files is the npm installation command:

Production:

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

Development / Test:

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

The question is because I want to be able to run my tests inside the container using the docker run . So I need test dependencies (usually dev dependencies for me).

It seems a little strange to establish dependencies not required when creating an image. On the other hand, creating / maintaining a second Dockerfile.dev file, which is also a minor difference, is also incorrect. So what is good practice for this kind of problem.

+7
docker npm dockerfile
source share
2 answers

No, you do not need to have different Dockerfile , and in fact you should avoid this.

The purpose of the docker is to send your application to an immutable, well-tested artifact (docker images) that is identical for production and testing, and even for developers.

Why? Because if you create different artifacts for testing and production, how can you guarantee that you have already tested, also works in production? you cannot, because these are two different things.

Given all this, if during testing you mean unit tests, then you can mount the source code inside the docker container and run the tests without creating any docker images. And that is wonderful. Remember that you can create an image for tests, but it is terribly slow and makes development calm and slow, which is not very good. Then, if your test passes, you can safely create the application container.

But if you mean acceptance tests that should actually run against your running application, you should create one image for your application (only one) and run the tests in another container (for example, the example of the original test case) and run the tests against this container. This obviously means that your version for your application is different for installing npm for your tests.

Hope this gives you some insight.

+5
source share

Well, then you will have to support several Dockerfile , which are almost identical. Instead, I recommend using a NodeJS function, such as a production profile . And one more recommendation regarding

 RUN npm install --quiet --production 

It is better to create a separate .sh file and do something like this:

 ADD ./scripts/run.sh /run.sh RUN chmod +x /*.sh 

And also think about starting to use Gulp .

UPD # 1

By default, npm install installs devDependencies . To get around this, use npm install --production OR set the NODE_ENV environment NODE_ENV to production .

Putting the script line in a separate file is a good practice, so as not to change the Dockerfile . If you need changes next time, you only have to update the script-file, and you're done. In the future, you can also do more work.

+1
source share

All Articles