- Create Dockerfile file
FROM node:node:14.20.0
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
# Expose app ports
EXPOSE 6075
# Start application
CMD [ "npm", "start" ]- Create dockerignore file
node_modules
npm-debug.log
- Building your images:
docker build -t eric/atlas-app .- Check docker image list
docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE eric/atlas-app latest d6e01bdfc655 40 minutes ago 1.24GB
- Run the image
docker run -p 8888:6075 -d -it eric/atlas-appIn the example above, Docker mapped the 6075 port inside of the container to the 8888 port on your machine. so you should access the first 6075(1) port to test. You may use the different ports.
- Get container ID
docker ps- Print app output
docker logs <container id>-
Test your app Access url http://localhost:8888
-
Enter the container, run command inside container
docker exec -it <container id> /bin/bash🔗 Dockerizing a Node.js web app
- Create
docker-compose.yml
app:
build: ./
volumes:
- ./:/usr/src/app
ports:
- 8888:6075
environment:
- NODE_ENV=development
- PORT=6075
command: 'npm start'
restart: always8888 is the post of host machine,6075 is the port of app inside docker container.
- Build and Run
docker-compose -f ./docker-compose.yml up -d- List container and related logs
docker psdocker logs <container id>- Execute bash inside docker container
docker exec -it <container id> /bin/bashReference:
🔗 A Docker/docker-compose setup with Redis and Node/Express