Dockerfile构建镜像

使用Dockerfile文件可以构建镜像,通过镜像可以运行多个实例,镜像是静态的,而容器是根据镜像生成的,活动在内存中。
我们来学习一下如何来写Dockerfile文件

  1. 我们需要新建一个文件夹,在文件夹内生成一个Dockerfile文件(Dockerfile文件名是规范首字母必须大写,其余为小写),和构建镜像需要的index.js
  2. Dockerfile文件的编写
    a. 文件要指定一个基础镜像,FROM baseimage
    b. COPY source target 该命令有两个参数,source代表文件相对于Dockerfile的路径,target代表文件相对于镜像的路径
    c. CMD 表示要在环境下运行应用程序,第一个参数表示要运行的应用程序,第二个参数表示运行应用程序的参数
  3. 在文件夹路径下运行docker build -t hello-docker
  4. docker images 查看生成的镜像
  5. docker run hello-docker

编写一一个Dockerfile大致需要:
指定基础镜像
确定应用程序
运行应用程序

# Dockerfile
FROM node:14-alpine
COPY index.js /index.js
CMD node index.js

# index.js
console.log('123')
posted @ 2023-10-05 20:32  wzpro  阅读(52)  评论(0)    收藏  举报