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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
Cloudbric
Cloudbric
G
GRAHAM CLULEY
S
Securelist
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
Spread Privacy
Spread Privacy
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
T
Tailwind CSS Blog
博客园_首页
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
O
OpenAI News
J
Java Code Geeks
T
Tenable Blog
K
Kaspersky official blog
AWS News Blog
AWS News Blog
S
Security @ Cisco Blogs
The GitHub Blog
The GitHub Blog
T
Threatpost
月光博客
月光博客
H
Heimdal Security Blog
Security Latest
Security Latest
The Hacker News
The Hacker News
Y
Y Combinator Blog
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
C
Cisco Blogs
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
C
CERT Recently Published Vulnerability Notes
D
Docker
Google Online Security Blog
Google Online Security Blog
D
DataBreaches.Net
V
Visual Studio Blog
H
Help Net Security

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.