2

The situtaion

I have to work on a VueJs (NuxtJs) spa, so I'm trying to use Docker with a Node image to avoid installing it on my pc, but can't figure out how to make it work.

The project

The source cose is in its own application folder, since it is versioned, and at the root level there is the docker-compose.yaml file

The folder structure
my-project-folder
├ application
| └ ...
└ docker-compose.yaml
The docker-compose.yaml
version: "3.3"

services:
  node:
    # container_name: prova_node
    restart: 'no'
    image: node:lts-alpine
    working_dir: /app
    volumes:
      - ./application:/app

The problem

The container start but quit immediately with exit status 0 (so it executed correctly), but this way I can't use it to work on the project.

Probably there is something I'm missing about the Node image or Docker in general; what i would like to to do is connecting to the docker container to run npm commands like install, run start etc and then check the application on the browser on localhost:3000 or whatever it is.

3
  • Since you don't have your app docker image pre built, you will have to add Dockerfile with steps to build your app first and use docker compose file to orchestrate the containers. This website outlines basic steps : bezkoder.com/docker-compose-nodejs-mongodb also you will have to publish ports on container to be able to access your app on localhost. Commented Dec 15, 2021 at 9:17
  • That's ok, but what about the container exiting immediately? Is there something particular I'll need to write in the Dockerfile to keep it alive? Commented Dec 15, 2021 at 9:20
  • 1
    Your container exists immediately because you are just building a container with just node:lts-alpine image your app is not running. Unless there is a process running containers do not stay alive. Check the sample dockerfile(with npm install and start) in the above website shared.. You can try replicate similar dockerfile/docker compose for your setup accordingly (omit whatever is not needed) Commented Dec 15, 2021 at 9:25

1 Answer 1

1

I would suggest to use Dockerfile with base image as node and then create your entrypoint which runs the application. That will eliminate the need to use volumes which is used when we want to maintain some state for our containers.

Your Dockerfile may look something like this:

FROM node:lts-alpine
RUN mkdir /app
COPY application/ /app/
EXPOSE 3000
CMD npm start --prefix /app 

You can then either run it directly through docker run command or use docker-compose.yaml as following :

version: "3.3"

services:
  node:
    # container_name: prova_node
    restart: 'no'
    build: 
      context: .
    ports:
      - 3000:3000
Sign up to request clarification or add additional context in comments.

Comments

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.