小白的docker学习全过程
参考文献
黑马程序员Docker快速入门到项目部署,MySQL部署+Nginx部署+docker自定义镜像+DockerCompose项目实战一套搞定_哔哩哔哩_bilibili
Ubuntu · Docker – 从入门到实践 (docker-practice.github.io)
Docker 教程 | 菜鸟教程 (runoob.com)
介绍
开源的应用容器引擎,基于 GO 语言
可以让开发者打包应用以及依赖报到一个轻量级可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
概念
准备 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 PS C:\Users\zhuyu> wsl --list --verbose NAME STATE VERSION * Ubuntu Running 2 PS C:\Users\zhuyu> wsl Welcome to fish, the friendly interactive shell Type help for instructions on how to use fish zyh@QAQ /m/c/U/zhuyu> neofetch .-/+oossssoo+/-. zyh@QAQ `:+ssssssssssssssssss+:` ------- -+ssssssssssssssssssyyssss+- OS: Ubuntu 22.04.3 LTS on Windows 10 x86_64 .ossssssssssssssssssdMMMNysssso. Kernel: 5.15.153.1-microsoft-standard-WSL2 /ssssssssssshdmmNNmmyNMMMMhssssss/ Uptime: 5 mins +ssssssssshmydMMMMMMMNddddyssssssss+ Packages: 521 (dpkg), 6 (snap) /sssssssshNMMMyhhyyyyhmNMMMNhssssssss/ Shell: fish 3.3.1 .ssssssssdMMMNhsssssssssshNMMMdssssssss. Terminal: Windows Terminal +sssshhhyNMMNyssssssssssssyNMMMysssssss+ CPU: 11th Gen Intel i5-1135G7 (8) @ 2.419GHz ossyNMMMNyMMhsssssssssssssshmmmhssssssso GPU: 613e:00:00.0 Microsoft Corporation Device 008e ossyNMMMNyMMhsssssssssssssshmmmhssssssso Memory: 525MiB / 7808MiB +sssshhhyNMMNyssssssssssssyNMMMysssssss+ .ssssssssdMMMNhsssssssssshNMMMdssssssss. /sssssssshNMMMyhhyyyyhdNMMMNhssssssss/ +sssssssssdmydMMMMMMMMddddyssssssss+ /ssssssssssshdmNNNNmyNMMMMhssssss/ .ossssssssssssssssssdMMMNysssso. -+sssssssssssssssssyyyssss+- `:+ssssssssssssssssss+:` .-/+oossssoo+/-. zyh@QAQ /m/c/U/zhuyu>
解释 检查 WSL 安装状态 :wsl --list --verbose
进入 WSL :wsl
展示系统信息 : neofetch
是一个命令行工具,用于显示系统信息和操作系统的 ASCII 艺术图案。
安装 我按照这个教程来的:Ubuntu · Docker – 从入门到实践 (docker-practice.github.io)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 Welcome to fish, the friendly interactive shell Type help for instructions on how to use fish zyh@QAQ ~> $ sudo apt-get update fish: Expected a variable name after this $. zyh@QAQ ~> $ sudo apt-get update fish: Expected a variable name after this $. zyh@QAQ ~> sudo apt-get update [sudo] password for zyh: Hit:1 http://mirrors.matrix.moe/ubuntu jammy InRelease Get:2 http://mirrors.matrix.moe/ubuntu jammy-updates InRelease [128 kB] Get:3 http://mirrors.matrix.moe/ubuntu jammy-backports InRelease [127 kB] Get:4 http://mirrors.matrix.moe/ubuntu jammy-security InRelease [129 kB] Get:5 http://mirrors.matrix.moe/ubuntu jammy-updates/main amd64 Packages [2058 kB] Get:6 http://mirrors.matrix.moe/ubuntu jammy-updates/main Translation-en [355 kB] Get:7 http://mirrors.matrix.moe/ubuntu jammy-updates/main amd64 c-n-f Metadata [17.8 kB] Get:8 http://mirrors.matrix.moe/ubuntu jammy-updates/restricted amd64 Packages [2495 kB] Get:9 http://mirrors.matrix.moe/ubuntu jammy-updates/restricted Translation-en [429 kB] Get:10 http://mirrors.matrix.moe/ubuntu jammy-updates/universe amd64 Packages [1124 kB] Get:11 http://mirrors.matrix.moe/ubuntu jammy-updates/universe Translation-en [261 kB] Get:12 http://mirrors.matrix.moe/ubuntu jammy-updates/universe amd64 c-n-f Metadata [26.1 kB] Get:13 http://mirrors.matrix.moe/ubuntu jammy-backports/universe amd64 Packages [28.8 kB] Get:14 http://mirrors.matrix.moe/ubuntu jammy-backports/universe amd64 c-n-f Metadata [672 B] Get:15 http://mirrors.matrix.moe/ubuntu jammy-security/main amd64 Packages [1839 kB] Get:16 http://mirrors.matrix.moe/ubuntu jammy-security/main Translation-en [298 kB] Get:17 http://mirrors.matrix.moe/ubuntu jammy-security/main amd64 c-n-f Metadata [13.3 kB] Get:18 http://mirrors.matrix.moe/ubuntu jammy-security/restricted amd64 Packages [2431 kB] Get:19 http://mirrors.matrix.moe/ubuntu jammy-security/restricted Translation-en [418 kB] Get:20 http://mirrors.matrix.moe/ubuntu jammy-security/universe amd64 Packages [903 kB] Get:21 http://mirrors.matrix.moe/ubuntu jammy-security/universe Translation-en [177 kB] Get:22 http://mirrors.matrix.moe/ubuntu jammy-security/universe amd64 c-n-f Metadata [19.3 kB] Fetched 13.3 MB in 5s (2688 kB/s) Reading package lists... Done zyh@QAQ ~> sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg \ lsb-release Reading package lists... Done Building dependency tree... Done Reading state information... Done lsb-release is already the newest version (11.1.0ubuntu4). lsb-release set to manually installed. ca-certificates is already the newest version (20230311ubuntu0.22.04.1). ca-certificates set to manually installed. gnupg is already the newest version (2.2.27-3ubuntu2.1). gnupg set to manually installed. The following additional packages will be installed: libcurl4 The following NEW packages will be installed: apt-transport-https The following packages will be upgraded: curl libcurl4 2 upgraded, 1 newly installed, 0 to remove and 144 not upgraded. Need to get 485 kB of archives. After this operation, 172 kB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://mirrors.matrix.moe/ubuntu jammy-updates/universe amd64 apt-transport-https all 2.4.13 [1510 B] Get:2 http://mirrors.matrix.moe/ubuntu jammy-updates/main amd64 curl amd64 7.81.0-1ubuntu1.18 [194 kB] Get:3 http://mirrors.matrix.moe/ubuntu jammy-updates/main amd64 libcurl4 amd64 7.81.0-1ubuntu1.18 [289 kB] Fetched 485 kB in 0s (1154 kB/s) Selecting previously unselected package apt-transport-https. (Reading database ... 28748 files and directories currently installed.) Preparing to unpack .../apt-transport-https_2.4.13_all.deb ... Unpacking apt-transport-https (2.4.13) ... Preparing to unpack .../curl_7.81.0-1ubuntu1.18_amd64.deb ... Unpacking curl (7.81.0-1ubuntu1.18) over (7.81.0-1ubuntu1.14) ... Preparing to unpack .../libcurl4_7.81.0-1ubuntu1.18_amd64.deb ... Unpacking libcurl4:amd64 (7.81.0-1ubuntu1.18) over (7.81.0-1ubuntu1.14) ... Setting up apt-transport-https (2.4.13) ... Setting up libcurl4:amd64 (7.81.0-1ubuntu1.18) ... Setting up curl (7.81.0-1ubuntu1.18) ... Processing triggers for man-db (2.10.2-1) ... Processing triggers for libc-bin (2.35-0ubuntu3.4) ... zyh@QAQ ~> curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/ docker-archive-keyring.gpg zyh@QAQ ~> echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null fish: $(...) is not supported. In fish, please use '(lsb_release)'. $ (lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null ^ zyh@QAQ ~> echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/d ocker-ce/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null fish: $(...) is not supported. In fish, please use '(lsb_release)'. $ (lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null^ zyh@QAQ ~> echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/d ocker-ce/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/d ocker-ce/linux/ubuntu \ (lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null fish: $(...) is not supported. In fish, please use '(lsb_release)'. $ (lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null ^ zyh@QAQ ~> echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/d ocker-ce/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/d ocker-ce/linux/ubuntu \ (lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null ^C zyh@QAQ ~> echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/d ocker-ce/linux/ubuntu \ (lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null zyh@QAQ ~> sudo apt-get update Hit:1 http://mirrors.matrix.moe/ubuntu jammy InRelease Hit:2 http://mirrors.matrix.moe/ubuntu jammy-updates InRelease Hit:3 http://mirrors.matrix.moe/ubuntu jammy-backports InRelease Hit:4 http://mirrors.matrix.moe/ubuntu jammy-security InRelease Ign:5 https://mirrors.aliyun.com/docker-ce/linux/ubuntu (lsb_release InRelease Err:6 https://mirrors.aliyun.com/docker-ce/linux/ubuntu (lsb_release Release 404 Not Found [IP: 36.250.255.105 443] Reading package lists... Done E: The repository 'https://mirrors.aliyun.com/docker-ce/linux/ubuntu (lsb_release Release' does not have a Release file. N: Updating from such a repository can't be done securely, and is therefore disabled by default. N: See apt-secure(8) manpage for repository creation and user configuration details. zyh@QAQ ~ [100]> lsb_release -cs jammy zyh@QAQ ~> deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-c e/linux/ubuntu jammy stable Command 'deb' not found, did you mean: command 'dub' from snap dub (1.19.0) command 'den' from snap den (1.2.0-0) command 'dub' from deb dub (1.27.0-2) command 'dcb' from deb iproute2 (5.15.0-1ubuntu2) command 'dab' from deb bsdgames (2.17-29) command 'dex' from deb dex (0.9.0-1) command 'debc' from deb devscripts (2.22.1ubuntu1) command 'edb' from deb edb-debugger (1.3.0-1) command 'deb3' from deb quilt (0.66-2.1) command 'debi' from deb devscripts (2.22.1ubuntu1) command 'derb' from deb icu-devtools (70.1-2) See 'snap info <snapname>' for additional versions. zyh@QAQ ~ [127]> echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun. com/docker-ce/linux/ubuntu jammy stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null zyh@QAQ ~> sudo apt-get update Hit:1 http://mirrors.matrix.moe/ubuntu jammy InRelease Hit:2 http://mirrors.matrix.moe/ubuntu jammy-updates InRelease Hit:3 http://mirrors.matrix.moe/ubuntu jammy-backports InRelease Hit:4 http://mirrors.matrix.moe/ubuntu jammy-security InRelease Get:5 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy InRelease [48.8 kB] Get:6 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy/stable amd64 Packages [39.3 kB] Fetched 88.2 kB in 1s (122 kB/s) Reading package lists... Done zyh@QAQ ~> sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: dbus-user-session docker-buildx-plugin docker-ce-rootless-extras libslirp0 pigz slirp4netns Suggested packages: aufs-tools cgroupfs-mount | cgroup-lite The following NEW packages will be installed: containerd.io dbus-user-session docker-buildx-plugin docker-ce docker-ce-cli docker-ce-rootless-extras docker-compose-plugin libslirp0 pigz slirp4netns 0 upgraded, 10 newly installed, 0 to remove and 144 not upgraded. Need to get 122 MB of archives. After this operation, 440 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy/stable amd64 containerd.io amd64 1.7.22-1 [29.5 MB] Get:2 http://mirrors.matrix.moe/ubuntu jammy/universe amd64 pigz amd64 2.6-1 [63.6 kB] Get:3 http://mirrors.matrix.moe/ubuntu jammy-updates/main amd64 dbus-user-session amd64 1.12.20-2ubuntu4.1 [9442 B] Get:4 http://mirrors.matrix.moe/ubuntu jammy/main amd64 libslirp0 amd64 4.6.1-1build1 [61.5 kB] Get:5 http://mirrors.matrix.moe/ubuntu jammy/universe amd64 slirp4netns amd64 1.0.1-2 [28.2 kB] Get:6 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy/stable amd64 docker-buildx-plugin amd64 0.16.2-1~ubuntu.22.04~jammy [29.9 MB] Get:7 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy/stable amd64 docker-ce-cli amd64 5:27.2.1-1~ubuntu.22.04~jammy [15.0 MB] Get:8 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy/stable amd64 docker-ce amd64 5:27.2.1-1~ubuntu.22.04~jammy [25.6 MB] Get:9 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy/stable amd64 docker-ce-rootless-extras amd64 5:27.2.1-1~ubuntu.22.04~jammy [9572 kB] Get:10 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy/stable amd64 docker-compose-plugin amd64 2.29.2-1~ubuntu.22.04~jammy [12.5 MB] Fetched 122 MB in 1min 7s (1832 kB/s) Selecting previously unselected package pigz. (Reading database ... 28752 files and directories currently installed.) Preparing to unpack .../0-pigz_2.6-1_amd64.deb ... Unpacking pigz (2.6-1) ... Selecting previously unselected package containerd.io. Preparing to unpack .../1-containerd.io_1.7.22-1_amd64.deb ... Unpacking containerd.io (1.7.22-1) ... Selecting previously unselected package dbus-user-session. Preparing to unpack .../2-dbus-user-session_1.12.20-2ubuntu4.1_amd64.deb ... Unpacking dbus-user-session (1.12.20-2ubuntu4.1) ... Selecting previously unselected package docker-buildx-plugin. Preparing to unpack .../3-docker-buildx-plugin_0.16.2-1~ubuntu.22.04~jammy_amd64.deb ... Unpacking docker-buildx-plugin (0.16.2-1~ubuntu.22.04~jammy) ... Selecting previously unselected package docker-ce-cli. Preparing to unpack .../4-docker-ce-cli_5%3a27.2.1-1~ubuntu.22.04~jammy_amd64.deb ... Unpacking docker-ce-cli (5:27.2.1-1~ubuntu.22.04~jammy) ... Selecting previously unselected package docker-ce. Preparing to unpack .../5-docker-ce_5%3a27.2.1-1~ubuntu.22.04~jammy_amd64.deb ... Unpacking docker-ce (5:27.2.1-1~ubuntu.22.04~jammy) ... Selecting previously unselected package docker-ce-rootless-extras. Preparing to unpack .../6-docker-ce-rootless-extras_5%3a27.2.1-1~ubuntu.22.04~jammy_amd64.deb ... Unpacking docker-ce-rootless-extras (5:27.2.1-1~ubuntu.22.04~jammy) ... Selecting previously unselected package docker-compose-plugin. Preparing to unpack .../7-docker-compose-plugin_2.29.2-1~ubuntu.22.04~jammy_amd64.deb ... Unpacking docker-compose-plugin (2.29.2-1~ubuntu.22.04~jammy) ... Selecting previously unselected package libslirp0:amd64. Preparing to unpack .../8-libslirp0_4.6.1-1build1_amd64.deb ... Unpacking libslirp0:amd64 (4.6.1-1build1) ... Selecting previously unselected package slirp4netns. Preparing to unpack .../9-slirp4netns_1.0.1-2_amd64.deb ... Unpacking slirp4netns (1.0.1-2) ... Setting up dbus-user-session (1.12.20-2ubuntu4.1) ... Setting up docker-buildx-plugin (0.16.2-1~ubuntu.22.04~jammy) ... Setting up containerd.io (1.7.22-1) ... Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service. Setting up docker-compose-plugin (2.29.2-1~ubuntu.22.04~jammy) ... Setting up docker-ce-cli (5:27.2.1-1~ubuntu.22.04~jammy) ... Setting up libslirp0:amd64 (4.6.1-1build1) ... Setting up pigz (2.6-1) ... Setting up docker-ce-rootless-extras (5:27.2.1-1~ubuntu.22.04~jammy) ... Setting up slirp4netns (1.0.1-2) ... Setting up docker-ce (5:27.2.1-1~ubuntu.22.04~jammy) ... Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service. Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket. Processing triggers for man-db (2.10.2-1) ... Processing triggers for libc-bin (2.35-0ubuntu3.4) ... zyh@QAQ ~> sudo systemctl enable docker Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable docker zyh@QAQ ~> sudo systemctl start docker zyh@QAQ ~> sudo groupadd docker groupadd: group 'docker' already exists zyh@QAQ ~ [9]> docker run --rm hello-world docker: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Head "http://%2Fvar%2Frun%2Fdocker.sock/_ping": dial unix /var/run/docker.sock: connect: permission denied. See 'docker run --help'. zyh@QAQ ~ [126]>
解决 Unable to find image 'hello-world:latest' locally
的问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 Welcome to fish, the friendly interactive shell Type help for instructions on how to use fish zyh@QAQ ~> docker run --rm hello-world Unable to find image 'hello-world:latest' locally docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": context deadline exceeded (Client.Timeout exceeded while awaiting headers). See 'docker run --help'. zyh@QAQ ~ [125]> sudo vim /etc/docker/daemon.json [sudo] password for zyh: zyh@QAQ ~> sudo systemctl daemon-reload zyh@QAQ ~> sudo systemctl restart docker zyh@QAQ ~> docker run --rm hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world c1ec31eb5944: Pull complete Digest: sha256:91fb4b041da273d5a3273b6d587d62d518300a6ad268b28628f74997b93171b2 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ zyh@QAQ ~>
使用 daocloud
镜像加速:
1 2 3 4 5 6 { "registry-mirrors": [ "https://docker.m.daocloud.io" ] }
``
使用
查看 docker 版本
启动 docker 服务1 sudo systemctl start docker
检查 docker 服务状态1 sudo systemctl status docker
运行第一个容器
或者1 docker run --rm hello-world
拉取镜像
ps: nginx 是一个高性能的 HTTP 和反向代理 web 服务器
列出 docker 镜像
运行容器1 docker run -d -p 8080:80 nginx
会将 nginx
容器的端口 80 投射到宿主机的 8080 端口,可以通过 https://localhost:8080
访问 Nginx 服务。
列出所有容器
启动容器
停止容器
删除容器
删除本地镜像
查看容器日志
进入容器内部1 docker exec -it XX /bin/sh
docker compose 运行以下命令以下载 Docker Compose 的当前稳定版本: 将可执行权限应用于二进制文件: 创建软链: 测试是否安装成功:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 zyh@QAQ ~ [3]> set os (uname -s) set arch (uname -m) sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$os-$arch" -o /us r/local/bin/docker-compose % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0 100 23.5M 100 23.5M 0 0 55309 0 0:07:25 0:07:25 --:--:-- 47077 zyh@QAQ ~> sudo chmod +x /usr/local/bin/docker-compose [sudo] password for zyh: zyh@QAQ ~> sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose zyh@QAQ ~> docker-compose --version Docker Compose version v2.2.2
测试: 使用一个简单的 docker-compose.yml
文件来运行一个 Nginx 容器。测试步骤:
创建一个测试文件夹,并进入该文件夹:
1 2 mkdir dockercomposetestcd dockercomposetest
创建一个 docker-compose.yml
文件:
在文件中粘贴以下内容:
1 2 3 4 5 6 version: '3' services: web: image: nginx:alpine ports: - "8080:80"
这个文件会使用 nginx:alpine
镜像,并将本地的 8080
端口映射到容器的 80
端口。
运行 Docker Compose:
如果 Docker Compose 正常工作,看到下面的输出,表明 Nginx 容器启动成功:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 zyh@QAQ ~/dockercomposetest> sudo docker-compose up [+] Running 9/9 ⠿ web Pulled 30.8s ⠿ 43c4264eed91 Pull complete 2.4s ⠿ 5b19511a843d Pull complete 2.6s ⠿ 652d69a25e85 Pull complete 2.6s ⠿ 51676974aef5 Pull complete 2.7s ⠿ bb16f69e8876 Pull complete 2.7s ⠿ 6fb07faa0055 Pull complete 2.7s ⠿ c298c5a0cd21 Pull complete 4.8s ⠿ 0c02f601d0ee Pull complete 12.0s [+] Running 2/2 ⠿ Network dockercomposetest_default Created 0.2s ⠿ Container dockercomposetest-web-1 Created 0.2s Attaching to dockercomposetest-web-1 dockercomposetest-web-1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration dockercomposetest-web-1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ dockercomposetest-web-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh dockercomposetest-web-1 | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf dockercomposetest-web-1 | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf dockercomposetest-web-1 | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh dockercomposetest-web-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh dockercomposetest-web-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh dockercomposetest-web-1 | /docker-entrypoint.sh: Configuration complete; ready for start up dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: using the "epoll" event method dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: nginx/1.27.1 dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: built by gcc 13.2.1 20240309 (Alpine 13.2.1_git20240309) dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: OS: Linux 5.15.153.1-microsoft-standard-WSL2 dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: start worker processes dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: start worker process 30 dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: start worker process 31 dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: start worker process 32 dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: start worker process 33 dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: start worker process 34 dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: start worker process 35 dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: start worker process 36 dockercomposetest-web-1 | 2024/09/24 16:43:16 [notice] 1#1: start worker process 37 dockercomposetest-web-1 | 172.19.0.1 - - [24/Sep/2024:16:43:39 +0000] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0" "-"
打开浏览器并访问 http://localhost:8080
,看到 Nginx 的欢迎页面,表明 Docker Compose 运行成功。
停止容器,按 Ctrl+C
,然后运行以下命令来清理环境:
1 sudo docker-compose down
实战 这里以我的项目 project: 基于Springboot+Vue的亚健康管理系统 (gitee.com) 为例,将这个 web 项目部署到 docker 中运行。 通过Docker容器化的方式,不需要在本地(Windows或WSL)安装Node.js、MySQL、Java等开发依赖 ,这些依赖项完全可以由Docker来管理。Docker容器会为每个服务提供一个独立的运行环境,本地机器不需要直接安装这些服务。只需要安装Docker和Docker Compose工具,剩下的依赖都在Docker中管理。通过Git和Docker Compose,团队成员可以轻松启动、重建和管理开发环境,简化协作流程。
项目调研 关键信息 将一个包含 Vue、Spring Boot 和 MySQL 的项目部署到 Docker 中的过程涉及构建多个 Docker 容器来分别运行前端、后端和数据库。需要考虑以下关键信息:
Vue 项目的打包配置 :
Vue 项目是否已经通过 npm run build
或者 yarn build
进行生产环境打包?
是否有 Dockerfile 或现成的部署脚本?
Spring Boot 应用的构建方式 :
Spring Boot 项目是否使用 Maven 或 Gradle 进行构建?
是否已生成了 .jar
文件,或者是否有 Dockerfile 来构建 Spring Boot 容器?
MySQL 配置 :
MySQL 数据库的版本?
数据库的初始化脚本(如有的话)?
MySQL 的数据库配置,是否需要挂载外部数据卷保存数据?
应用的连接配置 :
Vue 前端如何连接到 Spring Boot 后端?(例如,API 地址在开发时是否通过代理,或者已经设置为生产环境的 URL)
Spring Boot 如何连接 MySQL?是否有外部配置(如 application.properties
或 application.yml
中的数据库连接设置)?
其他配置 :
是否有任何环境变量,配置文件或 secrets 需要传递给容器?
是否需要使用 Docker Compose 来管理多个容器(例如前端、后端、数据库)?
实际情况 :
vue项目还没打包,没有dockerfile
2.springboot采用maven构建,没有.jar文件,没有dockerfile 其中pom.xml为:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 <?xml version="1.0" encoding="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 3.2.5</version > <relativePath /> </parent > <groupId > com.example</groupId > <artifactId > demo</artifactId > <version > 0.0.1-SNAPSHOT</version > <name > demo</name > <description > Demo project for Spring Boot</description > <properties > <java.version > 17</java.version > </properties > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > <version > 3.0.3</version > </dependency > <dependency > <groupId > com.mysql</groupId > <artifactId > mysql-connector-j</artifactId > <scope > runtime</scope > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > </dependency > <dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter-test</artifactId > <version > 3.0.3</version > <scope > test</scope > </dependency > <dependency > <groupId > com.baomidou</groupId > <artifactId > mybatis-plus-spring-boot3-starter</artifactId > <version > 3.5.5</version > </dependency > <dependency > <groupId > cn.hutool</groupId > <artifactId > hutool-all</artifactId > <version > 5.8.25</version > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > <configuration > <excludes > <exclude > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > </exclude > </excludes > </configuration > </plugin > </plugins > </build > </project >
MySQL 的数据库配置需要挂载外部数据卷保存数据
vue前端和springboot后端通过跨域配置进行连接 ,vue.config.js内容为: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 module .exports = { devServer : { proxy : { '/api' : { target : 'http://localhost:9091' , changeOrigin : true , pathRewrite : { '^/api' : '' } } } } }
没有环境变量,配置文件或 secrets 需要传递给容器。需要使用 Docker Compose 来管理多个容器,即前端、后端、数据库
大致的部署流程
为 Vue 应用创建 Dockerfile :该 Dockerfile 用于将 Vue 应用打包为静态文件并部署到 Nginx 或其他 web 服务器中。
为 Spring Boot 应用创建 Dockerfile :这个 Dockerfile 将包含 Java 环境和 Spring Boot .jar
文件。
使用 Docker Compose 配置文件 :编写 docker-compose.yml
文件,用于启动 Vue 前端、Spring Boot 后端和 MySQL 数据库的三个服务。
方案 注意:下面有两种方案,我采取的是方案二
方案一:使用Docker Desktop的WSL集成 Docker Desktop,启用WSL 2 集成,这样Docker Desktop会将WSL与Windows的Docker环境整合在一起。
Docker Desktop在设置中启用 WSL 2 integration
,并选择你使用的WSL发行版(如Ubuntu)。
在Windows PowerShell或WSL中运行Docker: 在Windows的PowerShell中:1 cd D:\Desktop\project docker-compose up --build
在WSL中:1 cd /mnt/d/Desktop/project docker-compose up --build
优点:Windows还是WSL中可以使用相同的Docker环境。
方案二:将项目目录映射到WSL中的文件系统 继续使用WSL中的Docker,而不能直接在WSL中访问Windows文件系统:
挂载Windows的文件系统到WSL: 在WSL中,Windows的文件系统已经挂载在/mnt/
目录下。例如,C:\
盘的文件可以在WSL中通过/mnt/c/
访问。
在WSL中访问Windows上的项目: 你可以在WSL中通过/mnt/
目录访问Windows的文件。例如,如果项目在D:\Desktop\project
下:1 cd /mnt/d/Desktop/project
在WSL中运行Docker命令: 进入项目目录后,在WSL的fish shell中运行Docker命令:1 docker-compose up --build
优点:使用WSL中的Docker,但仍然能够访问Windows文件系统,避免了项目文件复制的麻烦。
方案三:将项目文件复制到WSL的文件系统中 项目文件完全在WSL的Linux文件系统中,将项目文件从Windows复制到WSL中,之后再通过WSL的Docker进行操作。
复制项目到WSL: 在PowerShell中运行以下命令,将项目复制到WSL的文件系统中(例如,复制到/home/your-user/projects
目录): 1 cp -r D:\Desktop\project\health_manage_springboot \\wsl$\Ubuntu\home\your-user\projects\
在WSL中进入项目目录:1 cd ~/projects/health_manage_springboot
运行Docker命令: 运行docker-compose up --build
命令:1 docker-compose up --build
优点:将项目文件复制到WSL的Linux文件系统中,确保在WSL环境下操作更加流畅,避免文件系统访问的问题。
操作 通过创建 Dockerfile、配置 Docker Compose 文件,将前端、后端和数据库拆解为不同的容器。
1. 打包 Vue 项目 在部署到 Docker 之前,Vue 项目需要打包成静态文件。
步骤:
确保你已经安装了 Node. js。
在项目根目录下,执行以下命令打包 Vue 项目:1 2 npm install npm run build
生成一个 dist
目录,里面是打包好的静态资源文件。
2. 创建 Vue 的 Dockerfile 在 Vue 项目的根目录中,创建一个 Dockerfile,用于构建一个运行 Vue 静态文件的 Nginx 容器。
Dockerfile (Vue) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 FROM node:16 AS build-stageWORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build FROM nginx:alpine AS production-stageCOPY nginx.conf /etc/nginx/nginx.conf COPY --from=build-stage /app/dist /usr/share/nginx/html EXPOSE 80 CMD ["nginx" , "-g" , "daemon off;" ]
Nginx 配置文件 (nginx. conf) 创建一个 nginx.conf
文件,指定如何处理前端资源:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 worker_processes auto;events { worker_connections 1024 ; } http { include /etc/nginx/mime.types; default_type application/octet-stream; sendfile on ; keepalive_timeout 65 ; server { listen 80 ; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri / /index.html; } location /api/ { proxy_pass http://backend:9091/; proxy_set_header Host $host ; proxy_set_header X-Real-IP $remote_addr ; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; proxy_set_header X-Forwarded-Proto $scheme ; } } }
3. 打包 Spring Boot 项目 为了将 Spring Boot 项目部署到 Docker 中,我们需要先生成一个可执行的 JAR 文件。
使用系统环境中的Maven:
在项目根目录下,使用 Maven 打包 Spring Boot 项目:
这将在 target
目录中生成一个 JAR 文件。
IDEA的Maven工具: 这里由于我开发时使用的 maven 是 idea 自带的,在终端运行时会报错,故使用 IDE 自带的 Maven 项目进行打包。
打开IntelliJ IDEA,进入Spring Boot项目。
点击右侧的“Maven”工具窗口(如果看不到,可以通过View -> Tool Windows -> Maven
来打开)。
展开项目中的Maven模块,找到Lifecycle
。
在Lifecycle
下,找到clean
和package
两个任务。
先双击clean
,然后再双击package
。
这样,IntelliJ IDEA会使用自带的Maven来清理项目并打包为JAR文件。打包完成后,生成的JAR文件会在target
目录下。
4. 创建 Spring Boot 的 Dockerfile 在 Spring Boot 项目的根目录中,创建一个 Dockerfile,用于构建 Spring Boot 容器。
Dockerfile (Spring Boot) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 FROM openjdk:17 -jdk-alpineWORKDIR /app COPY target/demo-0.0.1-SNAPSHOT.jar app.jar EXPOSE 9091 ENTRYPOINT ["java" , "-jar" , "app.jar" ]
5. MySQL 容器配置 为了将 MySQL 运行在 Docker 中,我们可以直接使用官方的 MySQL 镜像,并通过 Docker Compose 配置其数据库、用户和挂载外部数据卷来保存数据。 在 6 中可以完成这个需求。
6. 创建 Docker Compose 文件 Docker Compose 文件用于管理这三个服务(Vue 前端、Spring Boot 后端、MySQL 数据库)。
docker-compose. yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 version: '3' services: frontend: build: context: ./vue-project dockerfile: Dockerfile dns: - 8.8 .8 .8 - 8.8 .4 .4 ports: - "8080:80" depends_on: - backend backend: build: context: ./springboot-project dockerfile: Dockerfile ports: - "9091:9091" environment: SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/health?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC SPRING_DATASOURCE_USERNAME: root SPRING_DATASOURCE_PASSWORD: 123456 depends_on: - db db: image: mysql:8.0 environment: MYSQL_ROOT_PASSWORD: 123456 MYSQL_DATABASE: health volumes: - db_data:/var/lib/mysql - /mnt/d/Desktop/project/health_management.sql:/docker-entrypoint-initdb.d/health_management.sql ports: - "3306:3306" volumes: db_data:
7. 使用 Docker Compose 启动服务
在 WSL 中导航到项目的根目录1 cd /mnt/d/Desktop/project
运行 docker compose 命令1 docker-compose up --build
这个命令会:
构建 Vue 前端、Spring Boot 后端和 MySQL 的容器镜像。
使用 Nginx 托管 Vue 前端的静态文件。
将前端请求代理到后端 Spring Boot 应用。
启动 MySQL 并将数据保存在本地挂载卷中。
8. 检查服务是否正常运行
访问 http://localhost:8080
来查看 Vue 前端是否正常工作。
后端 Spring Boot 服务应该在 http://localhost:9091
正常运行。
通过 docker logs <container_name>
查看各个容器的日志,确保一切正常。
如果导入 sql 文件出现问题,检查SQL文件是否已经成功导入: 进入 mysql 容器:1 docker exec -it <container_id> /bin/bash
(用 docker ps -a
获取 container_id) 登录 mysql:
检查数据库中是否有你导入的表和数据:1 2 USE health; SHOW TABLES;
如果 SQL 文件没有挂载成功的话可以采取手动导入 SQL 文件: 注意这里的 sql 表格如果你是从 gitee 上直接 clone 的需要删除 CREATE DATABASE health;
这条语句,因为数据库 health
已经存在。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 zyh@QAQ /m/d/D/project (master)> docker exec -it 649ab335c63b /bin/bash bash-5.1# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 13 Server version: 8.0.39 MySQL Community Server - GPL Copyright (c) 2000, 2024, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> USE health; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> SHOW TABLES; +------------------+ | Tables_in_health | +------------------+ | body | | user | +------------------+ 2 rows in set (0.00 sec) mysql> SELECT * FROM user; +----+----------+----------+-------------+------+----------------------+--------+ | id | username | password | number | role | email | avatar | +----+----------+----------+-------------+------+----------------------+--------+ | 1 | gss | 123456 | 13343243242 | 1 | super@aliyun.com | | | 2 | zhangsan | 123456 | NULL | 1 | zhangsan@gmail.com | | | 3 | lisi | 123456 | NULL | 1 | lisi@gmail.com | | | 4 | wangwu | 123456 | NULL | 0 | wangwu@gmail.com | | | 5 | zhaoer | 123456 | NULL | 0 | zhaoer@gmail.com | | | 6 | songliu | 123456 | 4322432434 | 0 | songliu@gmail.com | | | 13 | Alice | 123456 | 234532111 | 0 | alice@example.com | | | 14 | Bob | 123456 | 13564736363 | 0 | bob@example.com | | | 15 | Charlie | 123456 | 25323626 | 0 | charlie@example.com | | | 16 | David1 | 123456 | 145256264 | 0 | david@example.com | | | 17 | Emma | 123456 | 152334445 | 0 | emma@example.com | | | 18 | Frank | 123456 | 1566543646 | 0 | frank@example.com | | | 19 | Grace | 123456 | 1435253535 | 0 | grace@example.com | | | 20 | Henry | 123456 | 15325363463 | 0 | henry@example.com | | | 21 | Isabella | 123456 | 15353435 | 0 | isabella@example.com | | | 22 | Jacob | 123456 | 114523424 | 0 | jacob@example.com | | | 31 | test | 123456 | NULL | 0 | | | +----+----------+----------+-------------+------+----------------------+--------+ 17 rows in set (0.00 sec) mysql>
上述信息表示一切功能正常。
补充 :
停止正在运行的容器:
停止并删除所有服务容器、网络和卷(不删除数据卷) 再次启动:1 docker-compose up --build
停止容器而不删除
再次启动:
查看当前运行的容器
包含历史: