alexzaizar09
alexzaizar09

Reputation: 514

How to build a new app from scratch inside a docker container?

Docker is said to help isolate application environment, and help developers run the same environment. BUT all guides, tutorials, courses, etc start from an already built application. So how would i start a new application from scratch, let say in NodeJS?

So recently I've started to learn docker, and yes it is useful for packaging an already built application, if i already have everything installed in my local host machine and so on.

BUT how would I start developing a new application? For example in NodeJS, with no host installation of NodeJS, therefore I can't npm init my folder. How would I install new packages, how would the node_modules be persistent, and all that?

Here goes some very basis setup I used for packaging and app....THIS IS NOT WORKING FOR ME TO START FROM SCRATCH.

FROM node:10-alpine

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

COPY . .

CMD [ "npm", "start" ]
version: '3'

services:
  example-service:
    build: .
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - 3000:3000
      - 9229:9229
    command: npm start
dev:
  docker-compose up

As seen in the code, i have a custom image, a docker-compose, and a makefile. I can change any piece of it or all of it. What I want is to develop my app with nothing installed in my host machine apart from docker, containers should isolate everything needed for the application, best practice to persist any new package installed during development.

Upvotes: 5

Views: 1380

Answers (1)

VonC
VonC

Reputation: 1328602

You can follow "How to use Docker for Node.js development" by Cody Craven:

It does use Docker itself to develop, not just deploy/run a NodeJS application.

Example:

# This will use the node:8.11.4-alpine image to run `npm init`
# with the current directory mounted into the container.
#
# Follow the prompts to create your package.json
docker run --init --rm -it -v "${PWD}:/src" -w /src node:8.11.4-alpine npm init

Then you can setup an execution environment with:

FROM node:8.11.4-alpine AS dev
WORKDIR /usr/src/app
ENV NODE_ENV development
COPY . .
# You could use `yarn install` if you prefer.
RUN npm install

And build your app:

# Replace YOUR-NAMESPACE/YOUR-IMAGE with the name you would like to use.
docker build -t YOUR-NAMESPACE/YOUR-IMAGE:dev --target dev .

And run it:

# The `YOUR COMMAND` portion can be replaced with whatever command you
# would like to use in your container.
docker run --rm -it --init -v "${PWD}:/usr/src/app" YOUR-NAMESPACE/YOUR-IMAGE:dev YOUR COMMAND

All without node installed on your workstation!

Upvotes: 2

Related Questions