Docker 搭建服务器

一、创建 Dockerfile

新建一个 Dockerfile 文件并打开,然后输入以下内容:

FROM node:12.18.0

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

# Switch to Ali mirroring
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list \
&& sed -i 's|security.debian.org/debian-security|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list \
&& apt-get update && apt-get install -y \
gconf-service libasound2 libatk1.0-0 \
libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 \
libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 \
libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 \
libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 \
libxcb1 libxcomposite1 libxcursor1 libxdamage1 \
libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 \
libxss1 libxtst6 ca-certificates fonts-liberation \
libappindicator1 libnss3 lsb-release xdg-utils wget

RUN npm install && npm config set puppeteer_download_host = https://npm.taobao.org/mirrors
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8082
CMD [ "npm", "start" ]

在文件夹内创建 .dockerignore 并输入下面内容:

node_modules
npm-debug.log

二、编译镜像

docker build -t node-web-app .

三、查看所有镜像:

$ docker images

# Example
REPOSITORY      TAG        ID              CREATED
node            12.13.0    1934b0b038d1    5 days ago
node-web-app    latest     d64d3505b0d2    1 minute ago

四、运行镜像

docker run -p 49160:8080 -d node-web-app

五、查看输出

# Get container ID
$ docker ps

# Print app output
$ docker logs <container id>

# Example
Running on http://localhost:8080

六、进入容器目录
 

找到需要进入目录的正在运行的容器的id:
docker ps -a
进入该容器的目录:
docker exec -it 容器id /bin/bash
pwd 
ls -l

七、停止一个正在运行的容器

1、docker stop 此方式常常被翻译为优雅的停止容器

docker stop 容器ID或容器名
参数 -t:关闭容器的限时,如果超时未能关闭则用kill强制关闭,默认值10s,这个时间用于容器的自己保存状态
docker stop -t=60 容器ID或容器名

2、docker kill

docker kill 容器ID或容器名 :直接关闭容器

参考文档:

Docker Downloadicon-default.png?t=M276https://www.docker.com/products/docker-desktop
在Docker容器中安装Nodejs应用icon-default.png?t=M276https://www.cnblogs.com/jlmx/p/11731322.html
node镜像版本icon-default.png?t=M276https://hub.docker.com/