2

I want create a complete Node.js environment for develop any kind of application (script, api service, website ecc.) also using different services (es. Mysql, Redis, MongoDB). I want use Docker to do it in order to have a portable and multi OS environment.

I've created a Dockerfile for the container in which is installed Node.js:

FROM node:8-slim

WORKDIR /app

COPY . /app

RUN yarn install

EXPOSE 80

CMD [ "yarn", "start" ]

And a docker-compose.yml file where adding the services that I need to use:

version: "3"
services:
  app:
    build: ./
    volumes:
      - "./app:/app"
      - "/app/node_modules"
    ports:
      - "8080:80"
    networks:
      - webnet
  mysql:
    ...
  redis:
    ...
networks:
  webnet:

I would like ask you what are the best patterns to achieve these goals:

  • Having all the work directory shared across the host and docker container in order to edit the files and see the changes from both sides.

  • Having the node_modules directory visible on both the host and the docker container in order to be debuggable also from an IDE in the host.

  • Since I want a development environment suitable for every project, I would have a container where, once it started, I can login into using a command like docker-compose exec app bash. So I'm trying find another way to keep the container alive instead of running a Node.js server or using the trick of CMD ['tail', '-f', '/d/null']

Thank you in advice!

1 Answer 1

3

Having all the work directory shared across the host and docker container in order to edit the files and see the changes from both sides.

  • use -v volume option to share the host volume inside the docker container

Having the node_modules directory visible on both the host and the docker container in order to be debuggable also from an IDE in the host.

  • same as above

Since I want a development environment suitable for every project, I would have a container where, once it started, I can login into using a command like docker-compose exec app bash. So I'm trying find another way to keep the container alive instead of running a Node.js server or using the trick of CMD ['tail', '-f', '/d/null']

  • docker-compose.yml define these for interactive mode

    stdin_open: true tty: true

Then run the container with the command docker exec -it

Sign up to request clarification or add additional context in comments.

6 Comments

I use docker-compose up to start all services. When and where have I use the -v option? Furthermore, using stdin_open: true tty: true in the docker-compose.yml I'm not able to login in the container using docker exec -it <container-name> and the console returns this error Error response from daemon: Container 447b0f43ghy42e8fc7bb87ed846cfd532978b2e9c6d56851e7b536f07a278a16 is not running
volumes: - ./ghost/config.js:/var/lib/ghost/config.js
Volume example : volumes: - ./ghost/config.js:/var/lib/ghost/config.js Ref : linux.com/learn/docker-volumes-and-networks-compose . Do docker logs / docker inspect to check the problem.
One more recommendation, you need to copy package.json and do the npm install . Because it creates a layer and ignores if their is any future changes it will get ignored and start serving from cached layer. Ref 1 : forums.docker.com/t/making-volumes-with-docker-compose/45657/7 Ref 2: here a full example blog.codeship.com/using-docker-compose-for-nodejs-development
Thats great to hear, if you want to share the folders between docker container and host you can do it using volumes . Follow this link blog.codeship.com/using-docker-compose-for-nodejs-development . Note : volumes: – This section will mount paths between the host and the container. .:/usr/app/ – This will mount the root directory to our working directory in the container. /usr/app/node_modules – This will mount the node_modules directory to the host machine using the buildtime directory.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.