完成後Docker安裝您應該有一個新窗口,它將指導您完成創建的第一步圖片和貨櫃使用Docker:
這是一種有趣的方式,可幫助您快速下載第一個映像並將其作為容器運行。
您可以在此應用程序右側內置的終端中運行命令,但我更喜歡在自己的Shell中運行它。
我打開macOS終端,運行cd dev
進我家dev
文件夾,然後創建一個docker
子目錄,我將在其中託管所有Docker實驗。我跑cd docker
進入它,然後我跑
git clone https://github.com/docker/getting-started
該命令創建了一個新的getting-started
包含存儲庫內容的文件夾https://github.com/docker/getting-started:
現在從該文件夾運行命令docker build
這樣:
docker build -t docker101tutorial .
這將根據您所在的當前文件夾的內容來構建圖像,並使用標籤名稱docker101tutorial
。
這是Dockerfile
*# 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 .
,它將首先下載第一個基本映像:
然後它將運行我們在Dockerfile中定義的所有命令。
它一直持續到我們到達終點:
現在我們有了圖像docker101tutorial
我們可以基於該圖像運行一個容器。
運行命令docker run
具有這些屬性:
docker run -d -p 80:80 --name docker-tutorial docker101tutorialWe’re using the option -d
to run the container in background and print the container ID. If you miss this flag, you will not immediately get back to the shell until the container exits (but if it’s long-lived, for example it runs a service like a Node app or something, it will not exit automatically).
The -p
option is used to map port 80 of the container to the host machine port 80. The container exposes a Web server on port 80, and we can map ports on our computer to ports exposed by the container.
--name
assigns a name to the container, and finally we have the image name (docker101tutorial
) we should use to create the container.
If you have any doubt about a command option, run docker <command> --help
, in this case docker run --help
and you’ll get a very detailed explanation:

This command is very fast and you’ll get the container ID back:

More docker tutorials:
- Introduction to Docker
- Introduction to Docker Images
- Introduction to Docker Containers
- Dockerfiles
- Installing Docker on macOS
- First steps with Docker after the installation
- Using Docker Desktop to manage a Container
- Create a simple Node.js Hello World Docker Container from scratch
- What to do if a Docker container immediately exits
- Working with Docker Containers from the command line
- Working with Docker Images from the command line
- Sharing Docker Images on Docker Hub
- How to access files outside a Docker container
- How to commit changes to a Docker image
- Updating a deployed container based on a Docker image