-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpr-docker-nodejs.html
More file actions
82 lines (66 loc) · 2.61 KB
/
pr-docker-nodejs.html
File metadata and controls
82 lines (66 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Deploying a Node.js App to Docker</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<a href="index.html" class="back-button" title="Go back">
←
</a>
<h1>Deploying a Node.js Application to Docker</h1>
<p><em>By Alleoko</em></p>
<p>Docker makes it easier to build, deploy, and run applications inside containers. Here's how to package a Nodejs app into a Docker container in simple steps.</p>
<h2>1. Prepare your Node.js script</h2>
<p>Make sure you have a <code>package.json</code> file in your project directory. If you don't have one, you can create it using bash:</p>
<pre><code>npm init -y</code></pre>
<p>Create a src folder for index.js, for example <code>src/index.js</code>:</p>
<p>index.js</p>
<pre><code>const app = require('express')();
app.get('/', (req, res ) =>
res.json({ message: 'Welcome to Docker World 🐳' })
);
const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`app listening on http://localhost:${port}`) );</code></pre>
<h2>2. Create Dockerfile</h2>
<p>Create a file named <code>Dockerfile</code> in the root of your project directory with the following content:</p>
<pre><code>FROM node:12
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
ENV PORT=8080
EXPOSE 8080
CMD [ "npm", "start" ]
</code></pre>
<h2>4. Build the Docker Image</h2>
<pre><code>docker build -t nodejs-docker-app .</code></pre>
<h2>5. Run the Container</h2>
<pre><code>docker run -p 5000:8080 nodejs-docker-app</code></pre>
<p>You should now see your app running at <a href="http://localhost:5000" target="_blank">http://localhost:5000</a>.</p>
<h2>3. Create Docker Compose</h2>
<p>Optionally, you can create a <code>docker-compose.yml</code> file to simplify running your container:</p>
<pre><code>version: '3'
services:
web:
build: .
ports:
- "8080:8080"
db:
image: "mysql"
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- db-data:/foo
volumes:
db-data:
</code></pre>
<p>Run the following command to start your application using Docker Compose:</p>
<pre><code>docker-compose up</code></pre>
<p>Your application should now be accessible at <a href="http://localhost:8080" target="_blank">http://localhost:8080</a>.</p>
<h2>Wrap-up</h2>
<p>That's it! You've created a Docker container for your Nodejs app and run it successfully. This same container can be pushed to Docker Hub or deployed to cloud platforms.</p>
</body>
</html>