阿里云 docker-compose 部署

561 阅读2分钟

近日写了一个官网,让后通过docker 部署到了阿里云服务器,再此做一个记录,阿里云安装的是ubuntu,首先是在ubuntu中安装docker 和 docker compose

ubuntu 安装docker 和docker compose

docker安装:docs.docker.com/engine/inst…

docker compose 安装:docs.docker.com/compose/ins…

docker镜像打包问题

本人的电脑是mac m1 打包好的镜像在本地的docker可以正常启动,然而到了Linux的云服务器中启动报错

standard_init_linux.go:228: exec user process caused: exec format error

然后通过查找资料发现 mac m1 打包出的镜像是 arm64 而Linux需要 amd64 所以要改变一下docker的打包方式,

docker build --platform linux/amd64 -t [镜像名称] .

改变打包方式后发现无法打包成功,然后查阅资料后指定dockerfile 中node版本为14后打包成功

# Install dependencies only when needed
FROM node:14-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# Rebuild the source code only when needed
FROM node:14-alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN yarn build && yarn install --production --ignore-scripts --prefer-offline

# Production image, copy all the files and run next
FROM node:14-alpine AS runner
WORKDIR /app

ENV NODE_ENV production

# RUN addgroup -g 1001 -S nodejs
# RUN adduser -S nextjs -u 1001

# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json

# USER nextjs

EXPOSE 3000

ENV PORT 3000

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
# ENV NEXT_TELEMETRY_DISABLED 1

CMD ["node_modules/.bin/next", "start"]

上传云服务器

  1. 先将镜像保存.tar文件到本地
docker save -o [本地路径] [镜像]:[版本号]
  1. 通过scp 将文件传至服务器
scp [本地文件路径] root@[云主机公网IP]:[云主机文件夹路径]
scp /user/churongchang/document/... root@118.190.145.47:/dockerImages/

3.云主机中导入docker镜像

docker load --input [云主机镜像.tar 文件路径]

4.docker-compose 启动容器

docker-compose up -d //后台启动
docker-compose up //前台启动