安裝後使用Docker的第一步

完成後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:alpinenode:12-alpinenginx:alpine

當你跑步docker build -t docker101tutorial .,它將首先下載第一個基本映像:

然後它將運行我們在Dockerfile中定義的所有命令。

它一直持續到我們到達終點:

現在我們有了圖像docker101tutorial我們可以基於該圖像運行一個容器。

運行命令docker run具有這些屬性:

docker run -d -p 80:80 --name docker-tutorial docker101tutorial

We’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: