惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
量子位
IT之家
IT之家
Jina AI
Jina AI
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
人人都是产品经理
人人都是产品经理
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
AWS News Blog
AWS News Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客
P
Proofpoint News Feed
S
Schneier on Security
Spread Privacy
Spread Privacy
The Hacker News
The Hacker News
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
L
LINUX DO - 热门话题
博客园 - 聂微东
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
Security Latest
Security Latest
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
K
Kaspersky official blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
Last Week in AI
Last Week in AI
博客园 - Franky
G
GRAHAM CLULEY
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
L
LINUX DO - 最新话题
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
P
Privacy International News Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
Schneier on Security
Schneier on Security
V
V2EX
V
Visual Studio Blog
S
Security @ Cisco Blogs
博客园 - 叶小钗
H
Hacker News: Front Page
小众软件
小众软件
WordPress大学
WordPress大学
V2EX - 技术
V2EX - 技术
美团技术团队

Natalya Kosenko’s Blog

First six months of being a developer First six months of being a developer My dream to work as a developer came true My dream to work as a developer came true Enjoying Android development through pain Enjoying Android development through pain Life of a developer is not what I thought Life of a developer is not what I thought Dockerizing Next.js application Little joys of engineering manager Little joys of engineering manager Failed to get freelance programmer job Failed to get freelance programmer job Study Master’s in NTU - continuation of my story Study Master’s in NTU - continuation of my story What it feels like to study Master’s in NTU Singapore What it feels like to study Master’s in NTU Singapore Writing Context Managers in Python Writing Context Managers in Python
Dockerizing Next.js application
Natalya Kosenko · 2019-06-13 · via Natalya Kosenko’s Blog

One former colleague of mine used to say that even a monkey can become a DevOps engineer if put some effort. So I asked him to make DevOps engineer out of me. “Sure”, he said and gave me my first assignment: to read a book “How Linux Works: What Every Superuser Should Know” by Brian Ward. I abandoned the book in the middle, not because it was a boring book, but because it was hard to read the pure theory without applying it. However since then I got some interest in devops stuff.

Yesterday I tryied to dockerize my next.js application and was surprised how easy it was. If you the same as me before think that Docker is too complex, I can say that it’s not (or maybe I have not got into complexity yet). Below are the steps I performed to get my app into a docker container.

Install Docker

You can download Docker installer from the official site. Installation went smooth on my Mac machine, however I got into minor issues when was doing the same on Linux CentOS. Here are the installation steps for CentOS:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce
sudo usermod -aG docker $(whoami)
sudo systemctl enable docker.service
sudo systemctl start docker.service

If there is an error during docker-ce installation, this command might help:

sudo yum install http://vault.centos.org/centos/7.3.1611/extras/x86_64/Packages/container-selinux-2.9-4.el7.noarch.rpm

Create Dockerfile

Create Dockerfile and place it in the root of your Next.js application. Here is my Dockerfile:

FROM node:alpine

# Set working directory. All the paths will be relative to WORKDIR
WORKDIR /opt/dist

# Install PM2 globally
RUN npm install --global pm2

# Install dependencies
COPY package*.json ./
RUN npm install

# Copy source files
COPY . .

# Build the app
RUN npm run build

# Run the app
CMD [ "pm2-runtime", "start", "npm", "--", "start" ]

Btw, do not repeat my mistake: originally the first line of my docker file was FROM node:12. As a result a final docker image was huge, about 1.3 Gb. I spent almost 1 hour trying to understand what makes the image so large, then found the github issue which recommends to use node:slim and node:alpine variants that are much more reasonable in size.

Build Docker image

Run this command to create a docker image:

Run Docker container from the image

This is the last step. To run Docker container type:

docker run -d -p 3000:3000 my_app:latest

After that your application will be available at localhost:3000. Easy, right?

A few useful Docker commands

To wrap it up, here are few Docker commands that I found useful during setup:

docker ps # to see a list of running containers
docker stop [container_id] # to stop given container
docker images # to see a list of docker images
docker logs [first 3 characters of container id] # to see a log of a given container
docker rmi -f $(docker images -a -q) # to remove all docker images

Coming back to the beginning of the article, have I became a DevOps after getting my Next app into Docker? Of course, no! It’s just baby steps, but I enjoy it a lot.