使用 Docker 容器化
学习将 Clojure 应用打包到 Docker 容器中,以便在不同环境中一致地部署。
使用 Docker 容器化 是 CoddyKit 上的免费 Clojure Functional Programming & JVM Backend Development 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Clojure Functional Programming & JVM Backend Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Clojure Functional Programming & JVM Backend Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is Docker?
Docker helps you package your applications and all their dependencies into standardized units called containers. Think of it as a lightweight virtual machine, but much more efficient!
For Clojure developers, Docker ensures your application runs exactly the same way on your machine, a teammate's machine, or in the cloud. No more "it works on my machine" problems!
Images vs. Containers
At Docker's core are two main concepts:
- Images: These are read-only templates, like a blueprint for your application. They contain your code, libraries, and runtime.
- Containers: These are runnable instances of an image. You can start, stop, move, or delete a container without affecting the image.
A Dockerfile is a script that tells Docker how to build an image.
Starting Your Dockerfile
Every Dockerfile starts with a FROM instruction. This specifies the base image your application will build upon. For Clojure, an OpenJDK (Java Development Kit) image is a great choice.
The WORKDIR instruction sets the default directory for subsequent commands in the image.
FROM openjdk:17-jdk-slim
WORKDIR /appProject Dependencies (deps.edn)
Our Clojure web app will need a library to handle HTTP requests. We'll use Ring's Jetty adapter. This dependency is declared in your deps.edn file, which Clojure uses for dependency management.
We define a map where keys are library names and values specify their version. This tells Clojure how to find and download the necessary code.
{:deps {ring/ring-jetty-adapter {:mvn/version "1.9.5"}}}Our Simple Web App
Now, let's write a minimal Clojure web server. This file, src/server.clj, will be the entry point for our application. It starts a Jetty server that listens on port 3000 and responds with "Hello from Docker!".
Try running it locally first (after setting up deps.edn and running clojure -P):
(ns server
(:require [ring.adapter.jetty :as jetty])
(:gen-class))
(defn handler [request]
{:status 200
:headers {"Content-Type" "text/plain"}
:body "Hello from Clojure in Docker!"})
(defn -main []
(println "Starting server on port 3000...")
(jetty/run-jetty handler {:port 3000 :join? false}))Copying Code & Installing Deps
Next, we copy our deps.edn and source code into the container. It's good practice to copy deps.edn first and run clojure -P. If deps.edn doesn't change, Docker can reuse this layer, making builds faster.
Then, we copy the rest of our project files.
FROM openjdk:17-jdk-slim
WORKDIR /app
COPY deps.edn .
RUN clojure -P
COPY src srcFinalizing the Dockerfile
For a web app, we need to tell Docker which port our application listens on. The EXPOSE instruction documents this. Then, CMD specifies the command to execute when the container starts, launching our Clojure app.
Our Clojure app runs the -main function in the server namespace.
FROM openjdk:17-jdk-slim
WORKDIR /app
COPY deps.edn .
RUN clojure -P
COPY src src
EXPOSE 3000
CMD ["clojure", "-M", "-m", "server"]Building Your Image
With the Dockerfile and our Clojure project ready, we can now build the Docker image. Navigate to your project's root directory (where Dockerfile, deps.edn, and src are located) and run this command:
-ttags your image with a name and optional version..tells Docker to look for the Dockerfile in the current directory.
docker build -t my-clojure-web-app .Running Your Container
Once the image is built, you can run it as a container. We'll use the -p flag to map a port on your host machine (e.g., 8080) to the port inside the container (3000, as exposed by our app).
After running, open your browser to http://localhost:8080 to see your Clojure app!
docker run -p 8080:3000 my-clojure-web-appWhy Containerize?
Containerizing your Clojure applications offers many benefits:
- Consistency: Your app runs the same everywhere.
- Isolation: Containers isolate your app from other apps and the host system.
- Portability: Easily move your app between different environments.
- Scalability: Quickly spin up multiple instances of your app.
This makes deployment and management much simpler for Clojure services.
Dockerfile Commands Check
Which of the following Dockerfile instructions are correctly described?
Recap: Docker for Clojure
You've learned how Docker helps package Clojure applications for consistent deployment. We covered:
- The core concepts of Images and Containers.
- Writing a Dockerfile with key instructions like
FROM,WORKDIR,COPY,RUN,EXPOSE, andCMD. - Building a Docker image and running it as a container.
This knowledge is crucial for deploying robust Clojure microservices!
常见问题解答
「使用 Docker 容器化」课时是免费的吗?
是的 — 「使用 Docker 容器化」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Clojure Functional Programming & JVM Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Clojure Functional Programming & JVM Backend Development 课程共包含 4 节课。
「使用 Docker 容器化」这节课中我会学到什么?
学习将 Clojure 应用打包到 Docker 容器中,以便在不同环境中一致地部署。 你通过在浏览器中直接运行的动手代码来练习 Clojure Functional Programming & JVM Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Clojure Functional Programming & JVM Backend Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Clojure Functional Programming & JVM Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用 Docker 容器化」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Clojure Functional Programming & JVM Backend Development 课中编写并运行代码吗?
能。每节 Clojure Functional Programming & JVM Backend Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 设计 Clojure 微服务
- 使用 Docker 容器化
- 部署到云平台
- 服务发现与 API 网关