首页 > web前端 > js教程 > Windows 上的 Docker:带您进入容器仙境

Windows 上的 Docker:带您进入容器仙境

WBOY
发布: 2024-09-01 21:03:07
原创
605 人浏览过

这一切都始于一个简单的好奇心。我发现了这个用 Next.js 制作的很酷的开源项目——我很高兴能够探索它。但是,隐藏在项目文件中......神秘的 docker-compose.yml 文件。

Docker on Windows: Led Into Container Wonderland

经过快速的谷歌搜索后,我了解到这不仅仅是一些随机的文件用于装饰。不,不,这是必要的!为了运行这个项目,我需要 Docker,这个神奇的工具似乎每个人都知道,但从未用简单的术语解释过。

所以,我的旅程开始了——在 Windows 上安装 Docker,一路上克服了一些挑战,最终了解到 Docker 就像将您的应用程序打包到一个运输集装箱中,准备好在任何平台上顺利航行。 ?但首先,我必须弄清楚如何实际安装这个东西……事情是这样的。 ?

任务 1:安装 wsl

那么,我的 Docker 之旅的第一站是什么呢? WSL — 又名 Linux 的 Windows 子系统。如果您不熟悉 WSL,请将其视为秘密之门,它可以让您在 Windows 计算机中运行完整的 Linux 环境。

我很快了解到 Docker 在 Linux 上运行得非常好,因此安装 WSL 是我在 Windows 上顺利启动和运行 Docker 的门票。我选择的工具?强大的PowerShell! ?️ 通过一个简单的命令,我召唤了 WSL:

wsl --install
登录后复制

如果一切顺利,Windows 将发挥其魔力并安装默认的 Linux 发行版,即 Ubuntu! ?

现在,事情变得有趣了。 Ubuntu 第一次启动时,会要求输入用户名和密码。突然灵机一动(或懒惰),我决定让事情变得简单:用户名和密码都是“ubuntu”。

过了一会儿,我发现自己进入了 Ubuntu 命令行。但由于我们才刚刚开始,下一步就是优雅地退出。为此,我输入:

exit
登录后复制

…就这样,Linux 命令行窗口关闭了。

现在,如果您喜欢冒险并想尝试 Ubuntu 以外的东西,请不要担心 - WSL 为您提供选择!您可以使用以下命令列出所有可用的发行版:

wsl -l -o
登录后复制

并通过运行切换到另一个:

wsl --install -d <Distribution Name>
登录后复制

但现在,我们将坚持使用 Ubuntu(我的意思是,我已经承诺了用户名和密码,对吧??)。

在继续之前,让我们确保使用正确的 WSL 版本。您可以检查您安装的版本:

wsl -l -v
登录后复制

如果您有 WSL 2,那就太棒了!它更快、更强大,总体来说是 Docker 的更好选择。让我们通过运行将其设为默认值:

wsl --set-default-version 2
登录后复制

就这样,安装 Docker 的第一个要求就完成了!我们距离容器化的辉煌又近了一步?.

任务2:下载docker

WSL 设置并准备就绪后,是时候解决下一个重大任务了:安装 Docker 本身。 ?但在开始下载之前,我必须确保我值得信赖的设备能够应对挑战。毕竟,Docker 不仅仅可以在任何旧机器上运行,它有一些您需要满足的要求。

1️⃣检查系统要求

首先,我访问了 Docker 官方网站来获取安装程序。但在点击下载按钮之前,我仔细检查了我的设备是否满足 Docker 的系统要求。

Docker on Windows: Led Into Container Wonderland

这些要求包括足够的内存、磁盘空间,而且重要的是 Windows 版本为 1900 或更高版本。您可以通过运行 dxdiag 命令来检查您的 Windows 版本,该命令将向您显示有关系统的所有有趣的详细信息。

Docker on Windows: Led Into Container Wonderland

2️⃣虚拟化——必须的!

接下来,我确保我的计算机上启用了虚拟化。 Docker依靠虚拟化来创建容器,因此这一步至关重要。您可以通过打开任务管理器并在“性能”选项卡下查找虚拟化状态来检查它是否已启用。

Docker on Windows: Led Into Container Wonderland

如果已启用,那么就可以开始了!如果没有……那么,是时候访问您的 BIOS 设置并将其打开,检查一下

3️⃣Enable Windows Features

Before installing Docker, there are a couple of important Windows features that need to be activated: Windows Subsystem for Linux and Virtual Machine Platform. These are essential for Docker to run smoothly.

Docker on Windows: Led Into Container Wonderland

Here’s how to activate them:

  • Open the run system and type 'windows featuere'
  • Scroll through the list and check the boxes for:
    • Windows Subsystem for Linux
    • Virtual Machine Platform
  • Click OK and let Windows do its thing. You’ll probably need to restart your computer to apply these changes.

Once these features are enabled, you’re ready to proceed with the Docker installation. ?

4️⃣Download and Install Docker

Now that my system was fully prepped, I went ahead and downloaded the Docker installer from the official website. The installation process was smooth—just a few clicks, and Docker was up and running on my PC. ?

5️⃣Fire Up Docker

With Docker successfully installed, I launched Docker Desktop, and just like that, my system was ready to start spinning up containers like a pro. ?

And there we have it—mission two complete! Docker is now installed, next I’ll walk you through setting up your first Docker container and running your Next.js project inside it.

Final Mission : Start Docker

With Docker installed and ready to roll, it was time for the final mission: testing the installation. I was about to take my first dive into containerized waters, and luckily, Docker provided a handy little lifeboat—a sample project called docker/welcome-to-docker. ?️

Step 1: Launch Docker Desktop
First things first, I launched Docker Desktop from the Start menu. You’ll notice Docker starts running in the background, quietly preparing to do its container magic.

Step 2: Accessing the CLI
Now, it was time to get my hands dirty with some command-line interface (CLI) action. Since Docker works best with Linux distributions, I needed to make sure I was operating in the right environment. In my case, that meant switching to Ubuntu (remember we already exit just now☺️).

To switch to Ubuntu, I opened my CLI and typed:

ubuntu
登录后复制

This moved me into my Ubuntu environment, where Docker commands are at home. ??

Step 3: Running the Docker Welcome Project
With the environment set, it was time to spin up my first Docker container using Docker’s welcome project. Here’s the command I used:

docker run -d -p 80:80 docker/welcome-to-docker
登录后复制

(Note: The -d flag runs the container in detached mode, meaning it runs in the background, and the -p 80:80 part maps the container’s port 80 to my machine’s port 80. Translation: the container is now accessible via my web browser.)

Docker on Windows: Led Into Container Wonderland
After running this command, Docker fired up the welcome container in the background, and I could visit http://localhost in my browser to see the "Welcome to Docker" message. Success! ?

Step 4: Stopping the Container
Once I’d taken in all the glory of my first running container, it was time to shut it down. To do this, I needed the container’s ID. I found it by running:

docker ps -a
登录后复制

This listed all running containers, and from there, I grabbed the container ID. With the ID in hand, I issued the command:

docker stop [container_id]
登录后复制

And just like that, my container was stopped, quietly resting until needed again. ?

So there you have it—Docker is now installed on Windows. If you found this blog post helpful, feel free to share it with others who might benefit from it. And hey, why not hit that follow button for more nerdy goodness on JavaScript, React, and all things web development?

Let's stay in touch on Instagram, Twitter, and GitHub—where the real magic happens.

Thanks for sticking around! ?
Docker on Windows: Led Into Container Wonderland

Reference

  • Install linux in windows

  • Docker in windows by docker doc

以上是Windows 上的 Docker:带您进入容器仙境的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板