Docker 入门教程

本文是官方Getting Starts教程的阅读笔记,包含对步骤、命令的记录和解释。

教程分一系列课程,包括有:

  1. 安装 Docker
  2. 运行容器和创建自定义容器
  3. 创建高效可复用的镜像,并推送到 Docker Hub 上

Get Docker Desktop

Docker Desktop 是简单易用的 Docker 工具软件,使用 Docker Desktop 可以快速上手,避免因为不熟悉命令而卡壳(对自己说:还在入门别老想着成才)。

下载很慢,安装很快,走出第一步比较顺利,运行 welcome-to-docker 镜像,其中命令行为:

docker run -d -p 8080:80 docker/welcome-to-docker

命令执行之后会下载镜像并创建运行中的容器。

Develop with containers

使用容器进行开发(疑问:写代码也在容器中?)

在这一节中,我们下载运行了一个 todo list 的 App。

App 的容器组成:

  • 前端:React dev server (Vite) 的 Node 容器
  • 后端:Node api
  • 数据库:MySQL
  • phpMyAdmin:数据库管理工具
  • Traefik proxy:应用代理,实现一个端口对外服务,按规则转发请求到后端,前端等。

操作步骤:

  1. git clone 或者下载压缩包解压;
  2. cd getting-started-todo-app
  3. docker compose watch watch步骤可以实现主机和容器文件自动同步,浏览器自动刷新

制作和发布镜像

What is an image/Dockerfile?

Without going too deep yet, think of a container image as a single package that contains everything needed to run a process. In this case, it will contain a Node environment, the backend code, and the compiled React code.

Any machine that runs a container using the image, will then be able to run the application as it was built without needing anything else pre-installed on the machine.

A Dockerfile is a text-based script that provides the instruction set on how to build the image. For this quick start, the repository already contains the Dockerfile.

命令

docker build -t DOCKER_USERNAME/getting-started-todo-app .
docker push DOCKER_USERNAME/getting-started-todo-app

制作和发布镜像过程很顺利,对于新手比较友好。自建镜像运行后使用 sqlite 数据库、监听3000端口,并能正常访问(数据库和代理配置先不看了)。

小结

本文说明了新手如何使用容器来开发应用的工作流程,可以作为入门练手教程。然而对于多容器应用、网络、卷都未涉及,打算之后阅读 Docker 概念篇时分别做一些记录和阐述。

概念篇(只完成一篇):

  1. Docker 基本概念
posted @ 2024-08-20 11:30  螺旋质子  阅读(86)  评论(0编辑  收藏  举报