来自于Youtube Net Ninja 的Docker Crash Course。
Docker是一个轻量级(lightweight)的“虚拟机”。它可以让队伍不同的成员在相同的环境下运行应用。
image: 是container的blueprint(蓝图)。像是装修时的脚手架,是建筑的外壳。Image说明了container的配置。Image可以理解为Container的说明书,是用来配置Container的。
container:Docker的运行实例。实际应用在container中运行。
Dockerfile(就是这个名字,不用后缀):配置自己的image时需要的文件。程序配置时会一行一行运行dockerfile中的内容,搭建自己的image。
// Parent image as the base
FROM node:17-alpine
// Set working directory
WORKDIR /app
// copy current terminal files to docker working directory
COPY . .
// RUN means to run the following command during the building time
RUN npm install
// Expose the port
EXPOSE 4000
// CMD means to run the following command during the run time of the container instance.
CMD ["node", "app.js"]
.dockerignore:这里列出不需要被COPY这个命令拷贝到image中的内容(例如node_modules,敏感内容,日志等)。
写法:一行一个。
node_modules
*.md
这句话用来创建一个新的image. -t 代表 -tag,用来命名。最后一个.指的是把当前目录下所有文件放进image。
docker build -t myapp .
这句话用来创建一个新container。 --name 代表给新container命名, -p代表指定port。左边的4000代表本地计算机上的port,右边的代表container expose的port。最后是image的名字。
docker run --name myapp5_c -p 4000:4000 myapp5