M
M
mescalito902016-04-13 16:24:38
Node.js
mescalito90, 2016-04-13 16:24:38

How to set up Docker for nodejs development?

I want to make friends between Docker and NodeJs. The main goal is to create a container that will pull in all NodeJs dependencies when launched. It should also be possible to develop using this container without having to install nodejs on the local machine.
If you mount the entire app directory (along with packages.json), there is no way to run npm install automatically from the Dockerfile (because when you run the RUN npm install command in the Dockerfile, there will be no packages.json file yet, since it is mounted in docker-compose. yml later). You will have to manually enter the container and do npm install.
All tutorials suggest using COPY inside Dockerfile ( https://nodejs.org/en/docs/guides/nodejs-docker-we...
COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app
The application gets up, the dependencies get pulled up, but we can't edit the code from the local machine since it was copied to the docker container instead of being mounted. There is no way to add npm modules inside a container and store them in packages.json. That is, in this case, we will have to install nodejs locally and do a composer-compose build every time the code changes.
Maybe I misunderstood the concept of docker and it's not intended for development? Maybe it's just for deployment?
It seems to me that there will be a similar problem with composer PHP packages.
If you advise, or throw an example of your solution, or a link to a project where this is implemented, I will be very grateful.
I want to be able to take the docker image in the future and raise it on any server with one click. Without the need to configure the external environment.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
E
Egor Sh, 2016-04-13
. @EgorkZe

As I do with Angluyar2, I develop on a local machine, then I build and send the compiled code to the VOLUME folder (A folder that is visible both on the server and inside the container). That is, the container links the folder on the server to itself, and the changes that occur to this folder on the server - they also occur in the container

D
D', 2016-04-13
@Denormalization

Why docker for development? He's not talking about that at all.
For development, if you need a virtual machine, you should use Vagrant.

S
SabMakc, 2016-04-13
@SabMakc

For development inside a docker container, I deploy the C9 IDE within the container. We get a completely convenient Web-IDE for development, available from anywhere. I mount the folder with projects (it's easier to backup).
If you need a console, then we go into the container and fully work from it.
The option with an external editor, but with npm in the container is not entirely clear to me personally. If you really want to, you can opt out of "npm install" in the Dockerfile and mount the folder. But even for such a case, you can write a simple script that would translate local npm calls to the container.

D
Dmitry Lebedev, 2016-04-13
@k3NGuru

It may come in handy https://docs.docker.com/compose/rails/
The working folder is stored on the host, 2 dockers are raised: Rails and Postgre. Made changes in the file, they will immediately be in the container.
Running bundle install is done like this: edit the Gemfile after docker-compose build. If you do a bundle install inside the container, then when the container is restarted, it simply will not start, swearing at dependencies.
The following construction is used to run commands: docker-compose run web %command_name%
I think it will be possible to adapt for NodeJS.

N
Nicholas, 2016-04-13
@healqq

Instead of COPY share your project directory to your nodejs container via volume. And then just specify the necessary commands in the Dockerfile. (such as npm install for example).
https://github.com/healqq/manacheck/tree/master/docker
Here is the config for one of my homemade crafts. Have a look at the expressjs folder and dev.yml.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question