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

推荐订阅源

IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
MyScale Blog
MyScale Blog
N
Netflix TechBlog - Medium
I
InfoQ
Jina AI
Jina AI
Martin Fowler
Martin Fowler
Recent Announcements
Recent Announcements
量子位
月光博客
月光博客
罗磊的独立博客
雷峰网
雷峰网
The Cloudflare Blog
V
V2EX
小众软件
小众软件
人人都是产品经理
人人都是产品经理
博客园 - Franky
T
Tailwind CSS Blog
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题

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.