После завершенияУстановка Dockerу вас должно появиться новое окно, которое проведет вас через первые шаги созданияизображенийиконтейнерыс Докером:
Это интересный способ ускорить загрузку вашего первого образа и запуск его как контейнера.
Вы можете запускать команды в терминале справа, встроенном в это приложение, но я предпочитаю запускать его в своей собственной оболочке.
Открываю терминал 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
Как видите, он создает наше изображение не из одного, а из трех базовых изображений: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