运行您的第一个容器
使用预构建的 Docker 镜像部署一个简单应用,并通过命令行与其交互。
运行您的第一个容器 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Docker & Kubernetes for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Docker & Kubernetes for Developers 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Getting Started with Containers
Time to run real containers. You'll launch apps from images, interact with them, and learn the commands that manage their lifecycle.
Launching Containers with 'docker run'
docker run is your launch button: it checks for the image (pulling if needed), creates a container from it, and starts it — all in one command.
Your First Container: Hello World
Run the simplest container there is: hello-world. It prints a message and exits. Try it below.
docker run hello-worldWhat Just Happened?
Behind that hello-world run, Docker pulled the image, created a container, ran its command, printed the greeting, then stopped. A clean first cycle.
Interacting with Containers
To work inside a container, combine the -it flags: -i keeps STDIN open and -t gives you a terminal, so you can type commands inside it.
Get a Shell in Alpine Linux
Spin up minimal Alpine Linux and drop into its shell to run real Linux commands. Type exit to leave the container.
docker run -it alpine shBackgrounding with '-d'
To keep a container running without locking your terminal, use detached mode with the -d flag. Docker prints the container ID and hands control back.
Connecting with Port Mapping
Containers are isolated, so reach a service inside one with port mapping. The -p flag maps a host port to a container port: host:container.
Launching Nginx in Detached Mode
Combine detached mode and port mapping to run Nginx in the background, mapping your host's 8080 to the container's port 80. See the command below.
docker run -d -p 8080:80 nginxStopping & Removing Containers
Clean up when you're done: docker stop halts a running container and docker rm removes a stopped one. Grab the ID from docker ps first.
# First, find your Nginx container ID:
docker ps
# Then, stop it:
docker stop <your_nginx_container_id>
# Finally, remove it:
docker rm <your_nginx_container_id>Quick Check: Interactive Mode
You want to run a container and immediately get a shell inside it to execute commands. Which option would you use with docker run?
Lesson Recap: Running Containers
Recap: docker run launches containers; -it gives a shell, -d runs detached, -p maps ports — plus stop and rm to clean up afterward.
常见问题解答
「运行您的第一个容器」课时是免费的吗?
是的 — 「运行您的第一个容器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。
「运行您的第一个容器」这节课中我会学到什么?
使用预构建的 Docker 镜像部署一个简单应用,并通过命令行与其交互。 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Docker & Kubernetes for Developers 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「运行您的第一个容器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?
能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 容器化简介
- 安装 Docker 与基本命令
- 运行您的第一个容器
- 管理 Docker 镜像与容器