在你完成Docker安装后,你应该有一个新的窗口,引导你完成用Docker创建镜像和容器的第一个步骤。

这是一个有趣的方法,可以让你加快下载你的第一个镜像,并将其作为一个容器运行。

你可以在右侧的终端中运行命令,内置到这个应用程序中,但我更喜欢在我自己的shell中运行它。
我打开macOS终端,运行cd dev ,进入我的主页dev ,我创建了一个docker 子目录,在那里我将托管所有的Docker实验。我运行cd docker ,进入其中,然后我运行
git clone https://github.com/docker/getting-started
这个命令创建了一个新的getting-started 文件夹,里面有存储库的内容github.com/docker/gett…。

现在从这个文件夹中,以这种方式运行命令docker build 。
docker build -t docker101tutorial .
这将从你所在的当前文件夹的内容中建立镜像,标签名称为docker101tutorial 。
这就是Docker文件
*# Install the base requirements for the app.*
*# This stage is to support development.*
FROM python:alpine AS base
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
*# Run tests to validate app*
FROM node:12-alpine AS app-base
WORKDIR /app
COPY app/package.json app/yarn.lock ./
RUN yarn install
COPY app/spec ./spec
COPY app/src ./src
RUN yarn test
*# Clear out the node_modules and create the zip*
FROM app-base AS app-zip-creator
RUN rm -rf node_modules && \
apk add zip && \
zip -r /app.zip /app
*# Dev-ready container - actual files will be mounted in*
FROM base AS dev
CMD ["mkdocs", "serve", "-a", "0.0.0.0:8000"]
*# Do the actual build of the mkdocs site*
FROM base AS build
COPY . .
RUN mkdocs build
*# Extract the static content from the build*
*# and use a nginx image to serve the content*
FROM nginx:alpine
COPY --from=app-zip-creator /app.zip /usr/share/nginx/html/assets/app.zip
COPY --from=build /app/site /usr/share/nginx/html
正如你所看到的,它不只是从一个,而是从3个基础镜像中创建我们的镜像。python:alpine,node:12-alpine 和nginx:alpine 。
当你运行docker build -t docker101tutorial . ,它将从下载第一个基础镜像开始。

然后它将运行我们在Docker文件中定义的所有命令。

它一直在运行,直到我们到达终点。

现在我们有了镜像docker101tutorial ,我们可以基于这个镜像来运行一个容器。
运行带有这些属性的命令docker run 。
docker run -d -p 80:80 --name docker-tutorial docker101tutorial
我们使用选项-d ,在后台运行容器并打印容器的ID。如果你错过了这个标志,你将不会立即回到shell,直到容器退出(但如果它是长寿的,例如它运行一个像Node应用或其他的服务,它将不会自动退出)。
-p 选项用于将容器的80端口映射到主机的80端口。容器在80端口暴露了一个Web服务器,我们可以将我们计算机上的端口映射到容器所暴露的端口。
--name 为容器分配一个名称,最后我们有了我们应该用来创建容器的镜像名称( )。docker101tutorial
如果你对某个命令选项有任何疑问,可以运行docker <command> --help ,在这里是docker run --help ,你会得到一个非常详细的解释。

这个命令非常快,你会得到容器的ID。
