This content originally appeared on DEV Community and was authored by Emma Donery
Prerequisites
- Javascript
- Docker Basics
Understanding Docker
Docker is an open source platform that provides an open platform for building, shipping, and running distributed applications. It automates routine configuration procedures and is used across the development lifecycle to create fast, simple, and portable applications.
Understanding Nodejs And Express
Node.js is a JavaScript runtime framework which is used to create networking and server-side applications.
Express is a small framework that sits on top of Node.js’s web server functionality that provides a robust set of features to develop web and mobile applications.
Why Dockerize your application
- Rapid application deployment
- Portability across machines
- Version control and component reuse
- Sharing of images/dockerfiles
- Lightweight footprint and minimal overhead
- Simplified maintenance
NodeJs app
- Create a new directory where all the files would live
- Create a package.json file in this directory to define your project and its dependencies:
{
"name": "express_app",
"version": "1.0.0",
"license": "MIT",
"description": "Node.js and express on Docker",
"author": "Firstname Lastname <mynames@example.com>",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^4.16.1"
}
}
- Using the Express.js framework, create a app.js file that describes a web app:
const express = require('express')
const app = express()
const PORT = 3000
const HOST = '0.0.0.0'
// App
const app = express()
app.get('/', (req, res) => {
res.send('Hello World')
});
app.listen(PORT, HOST)
console.log(`Our app running on http://${HOST}:${PORT}`)
Run the app
$ node app.js
Go to http://localhost:3000/ in your browser to view it.
Dockerizing the Application
- Create empty Dockerfile
Dockerfile
FROM node:12-alpine3.14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
Here is what is happening:
Set the working directory to /usr/src/app
Copy the package.json file to /usr/src/app
Install node_modules
Copy all the files from the project's root to /usr/src/app
- Create a .dockerignore
node_modules
npm-debug.log
NB: If you are working with Git then you will also want to add your .git directory and .gitignore file.
- Build Docker Image
$ docker build -t hello-world .
- Run docker container
$ docker run -p 8080:8080 hello-world
-
Sharing the docker image
For you to share a docker image, you have to first signup at Docker hub.
Docker Hub is a Docker service that allows you to locate and share container images with your team.
After signing up:- Re-create the image with your Docker Hub credentials.
$ docker build -t [USERNAME]/hello-world .
- Login to Docker Hub
$ docker login
- Push the image to Docker Hub
$ docker push [USERNAME]/hello-world
Congratulations!🎉 🎉 The image can now be used on any server or PC that has Docker installed:
docker run [USERNAME]/hello-world
Docker Compose
Docker Compose is a tool for running multi-container applications on Docker. You configure your application's services with Compose using a YAML file. Then you build and start all of the services from your setup with a single command.
Compose enables running apps in a single or more containers simple. To construct or execute containers, we don't need to remember particularly long commands. Your applications will operate smoothly as long as you can run docker-compose build and docker-compose up.
- In the project root directory, create a docker-compose.yml file.
docker-compose.yml
version: '3.8' #specifies docker compose version
services:
web:
build:
context: ./
target: dev
volumes:
- .:/src
command: npm run start:dev
ports:
- "8080:8080"
environment:
NODE_ENV: development
DEBUG: nodejs-docker-express:*
In this article, i have a service name web which has a build context and a target set to dev. This tells Docker that i want to build the Docker image with the dev stage.
The volume instructs Docker to copy and sync changes from the local directory./ of the host with /src on the Docker container.
Exposing port 8080 exposes the port where the Node.js Express web server runs by default.
Build and run your app with Compose
- start up your application by running the docker-compose up command
$ docker-compose up
Access http://localhost:8000/ in a browser to see the application running.
Stop the application from running using the docker-compose down command.
$ docker-compose down
Conclusion
Docker Compose is an excellent tool for launching numerous containers. For the sake of this article, i solely used Node.js with a single container running on Docker.
Node.js and Docker get along swimmingly. The development experience is substantially smoother when docker-compose is used. You can use this article as a starting point for learning more advanced Docker and Node.js skills.
😊 Have fun coding!😊
Be sure to leave any comments for me.
You can connect with me on twitter https://twitter.com/EmmaDonery
or Linkedin https://www.linkedin.com/in/emma-donery-0831a7188/
This content originally appeared on DEV Community and was authored by Emma Donery
Emma Donery | Sciencx (2021-10-09T12:37:49+00:00) Getting Started with Nodejs, Express and Docker. Retrieved from https://www.scien.cx/2021/10/09/getting-started-with-nodejs-express-and-docker/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.