> [!info] 章节导航
> [[docker学习路径|Docker 学习索引]] · [[06-Docker Compose 多容器应用|上一章]] · [[08-Docker 底层技术|下一章]]

## 7. Docker Desktop 开发实践与故障排查

现在您已经安装了 Docker Desktop，可以进行一些应用程序开发了。具体来说，您将执行以下操作：

1. 克隆并启动开发项目
2. 对后端和前端进行更改
3. 立即查看更改

### 7.1 开发环境组成

现在环境已经启动并运行了，它里面到底有些什么呢？概括来说，它由多个容器（或进程）组成，每个容器（或进程）都服务于应用程序的特定需求：

React前端

Node 后端

MySQL数据库

phpMyAdmin：基于Web的界面

Traefik代理：应用代理，将请求路由到正确的服务。



使用此环境，作为开发人员，您无需安装或配置任何服务、填充数据库模式、配置数据库凭据或任何其他操作。您只需要 Docker Desktop。其余操作均可运行。

### 7.2 npm install 失败

```bash
------ >
 [backend-dev 2/4] RUN npm install:
 327.3 npm error Exit handler never called!
 327.3 npm error This is an error with npm itself. Please report this error at:
...
 COPY backend/spec ./spec  60 |     
 COPY backend/src ./src--------------------failed to solve: process "/bin/sh -c npm install" did not complete successfully: exit code: 1
```

**1. 升级 npm（最常见解决方案）**

在你的 Dockerfile 里， Stage: backend-base下的`RUN npm install` 前加上：


`RUN npm install -g npm@latest`

### 7.3 nodemon not found


```bash
❯ npm run dev
> backend@1.0.0 dev
> nodemon src/index.js
sh: 1: nodemon: not found

```

安装`sudo npm install -g nodemon`

### 7.4 热重载未生效

> 是因为 **Docker 运行的后端容器仍然使用旧的、构建时复制进去的代码**，
>  并没有使用你刚刚在本地编辑的文件。

换句话说，你改的是“外面”的文件，容器跑的是“里面”的文件。

方式 1：**重建镜像**



每次修改代码后重新构建镜像：

```
docker compose build
docker compose up
```

方式 2：**使用挂载卷 (Volume)**

（让容器实时使用你的本地代码）

修改你的 `docker-compose.yml`：

```
services:
  backend:
    build:
      context: .
      target: backend-dev
    ports:
      - "3000:3000"
    volumes:
      - ./backend:/usr/src/app
```

> 这样容器会“实时同步”你本地的 `backend` 目录，
>  一保存文件，容器内的代码就更新。

然后运行：

```
docker compose up --watch
```



### 7.5 查看 Ubuntu 版本

怎么知道我的ubuntu是哪个版本的？

**使用 `lsb_release` 命令（最标准、最直接）**

bash

```
lsb_release -a
```

### 7.6 切换 npm 镜像源

切换npm的镜像源

如果你不想安装`nrm`，也可以直接通过npm命令来切换镜像源。

- **设置镜像源**：例如，将镜像源设置为淘宝源。

  bash

  ```
  npm config set registry https://registry.npmmirror.com
  ```

  

- **恢复官方源**：如果想换回npm官方源，只需执行：

  bash

  ```
  npm config set registry https://registry.npmjs.org/
  ```

### 7.7 Git 加速方法

#### Git 使用 SSH 连接优化（避免 HTTPS 限制）

SSH 一般比 HTTPS 稳定（尤其走代理时）。

1. 生成 SSH key：

   ```
   ssh-keygen -t ed25519 -C "your_email@example.com"
   ```

2. 添加公钥到 GitHub：
    打开 https://github.com/settings/keys

3. 测试连接：

   ```
   ssh -T git@github.com
   ```

4. 使用 SSH 克隆：

   ```
   git clone git@github.com:user/repo.git
   ```

#### 配置 Git 全局代理（HTTP / SOCKS5）

如果你能使用代理（VPN、Clash、V2Ray、ShadowSocks等），可以让 Git 走代理加速：

HTTP(S) 代理：

```
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
```

SOCKS5 代理：

```
git config --global http.proxy socks5://127.0.0.1:7890
git config --global https.proxy socks5://127.0.0.1:7890
```

关闭代理：

```
git config --global --unset http.proxy
git config --global --unset https.proxy
```

 检查代理配置：

```
git config --global -l
```



### 7.8 总结

- 无需任何安装，即可启动完整的开发项目。容器化环境提供了开发环境，确保您拥有所需的一切。您无需直接在计算机上安装 Node、MySQL 或任何其他依赖项。您只需要 Docker Desktop 和代码编辑器。
- 
  进行更改并立即查看。这之所以成为可能，是因为：1）每个容器中运行的进程都在监视并响应文件更改；2）这些文件与容器化环境共享。
