27
loading...
This website collects cookies to deliver better user experience
├── node_modules
├── index.js
├── package.json
└── yarn.lock
const express = require('express');
const app = express();
const PORT = process.env.PORT || 4000;
app.get('/', (request, response) => {
response.status(200).json({
message: 'Hello Docker!',
});
});
app.listen(PORT, () => {
console.log(`Server is up on localhost:${PORT}`);
});
"build": "esbuild --bundle src/index.js --outfile=build/app.js --minify --platform=node"
Dockerfile
FROM node:14-alpine AS development
ENV NODE_ENV development
# Add a work directory
WORKDIR /app
# Cache and Install dependencies
COPY package.json .
COPY yarn.lock .
RUN yarn install
# Copy app files
COPY . .
# Expose port
EXPOSE 4000
# Start the app
CMD [ "yarn", "start" ]
docker-compose.dev.yml
. Here we'll also mount our code in a volume so that we can sync our changes with the container while developing.version: "3.8"
services:
app:
container_name: app-dev
image: app-dev
build:
context: .
target: development
volumes:
- ./src:/app/src
ports:
- 4000:4000
package.json
scripts"dev": "docker-compose -f docker-compose.dev.yml up"
-d
flag to run in daemon modeyarn dev
Attaching to app-dev
app-dev | yarn run v1.22.5
app-dev | $ nodemon src/index.js
app-dev | [nodemon] to restart at any time, enter `rs`
app-dev | [nodemon] watching path(s): *.*
app-dev | [nodemon] starting `node src/index.js`
app-dev | Server is up on localhost:4000
FROM node:14-alpine AS builder
ENV NODE_ENV production
# Add a work directory
WORKDIR /app
# Cache and Install dependencies
COPY package.json .
COPY yarn.lock .
RUN yarn install --production
# Copy app files
COPY . .
# Build
CMD yarn build
FROM node:14-alpine AS production
# Copy built assets/bundle from the builder
COPY --from=builder /app/build .
EXPOSE 80
# Start the app
CMD node app.js
docker-compose.prod.yml
for productionversion: "3.8"
services:
app:
container_name: app-prod
image: app-prod
build:
context: .
target: production
docker-compose -f docker-compose.prod.yml build
80
with the name react-app
docker run -p 80:4000 --name node-app app-prod