Skip to content

Create Dockerfile #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions DOCKERFILEABC/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM ruby:3.4-slim
WORKDIR /usr/src/app
RUN apt-get update && apt-get install -y build-essential
RUN gem install jekyll bundler
COPY . .
RUN jekyll build
EXPOSE 4000
CMD ["jekyll", "serve", "--host=0.0.0.0"]
# 新的实现, 但是测试有点小问题
59 changes: 59 additions & 0 deletions DOCKERFILEABC/Dockerfile1
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
2025.4.21 周一 --- 2025.4.22 周二 --- 2025.4.23 周三上午10:14完成

本文目的: 编写一个基于Ruby的Dockerfile 共五个部分 通过Docker一键启动Jekyll开发环境
---------------------------------------------------------------------------------------------------------------------------------------------------------
一、 编写Dockerfile的注意事项:
* 最常用的五个参数: 1. FROM
2. WORKDIR
3. COPY
4. RUN
5. CMD
* 文件的命名必须是Dockerfile, 不能是其他, D大写
* 除了FROM, 其他都是非必须的
* EXPOSE 指定当前镜像暴露出来的端口
VOLUME 指定映射文件的, 一般是映射到匿名卷
docker run 中的 -p 和 -v 是分别指定映射到外部的端口和目录
ENV 指定当前容器的环境变量
ARG 构建参数, 运行时无效, 可以构建时临时修改变量
LABEL 指定元数据, 便于找到 Docker
USER 设置运行用户
---------------------------------------------------------------------------------------------------------------------------------------------------------
二、 上一位同学代码的意思:(共8行代码, 3行注释)
FROM ruby:3.2 意思是指定基础镜像ruby的版本为 3.2
WORKDIR /app 意思是设置工作目录为 /app
RUN apk add --no-cache bash build-base git 执行
COPY . . 意思是将当前构建上下文 (本地当前目录) 的所有文件复制到镜像的工作目录 (/app) 中
RUN bundle install --path=~/vendor/bundler 执行
EXPOSE 4000: 意思是指定的容器暴露的端口是4000
CMD ["bash"]: 意思是指定容器跑起来的命令是bash
---------------------------------------------------------------------------------------------------------------------------------------------------------
三、 但是代码并不完整(AI搜索)
1. # RUN bundle install --path=~/vendor/bundler 被注释, 使项目的 Gem 依赖不能安装
2. CMD bash/bin 有问题 bash/bin 不是常见的可执行程序路径格式, 可能使容器不能正常工作
3. # RUN apk add --no-cache bash build-base git 被注释, 包管理器是apk, 基础镜像ruby:3.2是基于Debian系统
---------------------------------------------------------------------------------------------------------------------------------------------------------
四、 2025.4.16 周三下午接-----------2025.4.21 周一正式开搞
新的Dockerfile实现(AI搜索, 不是自己写的)
FROM ruby:3.2 # 用官方 Ruby 镜像(基于Debian)
WORKDIR /app # 设置工作目录
RUN apt-get update -qq && \
apt-get install -y nodejs npm && \
npm install -g yarn && \
rm -rf /var/lib/apt/lists/* # 安装 Debian 系统依赖(Node.js 和 Yarn)
RUN gem install bundler jekyll # 安装 Jekyll 和 Bundler
COPY Gemfile* ./ # 先复制 Gemfile 以利用 Docker 缓存
RUN bundle install
COPY . . # 复制项目文件
EXPOSE 4000 # 暴露端口
CMD ["bundle", "exec", "jekyll", "serve", "--host", "0.0.0.0", "--port", "4000"] # 启动 Jekyll 服务
---------------------------------------------------------------------------------------------------------------------------------------------------------
五、 2025.4.21 周一
其他的小东西:(借鉴知乎的文章以及其他)
Docker是一个用 Go 语言实现的开源项目 快速部署
- Dockerfile 是一个文本文件, 用于构建Docker镜像 Dockerfile 可以提高部署效率 Dockerfile的内容在编译之后生成的可执行程序是image
- 构建镜像 docker build -t jekyll-begin .
- 运行容器 docker run -p 4000:4000 jekyll-begin
- FROM #制作基准镜像 FROM 镜像
- WORKDIR #类似于Linux中的cd命令 但是和cd不一样的地方在于, 若输入的目录在不存在, 则WORKDIR会自动创建这个目录, 再进入该目录
- RUN #运行
- CI/CD #指持续集成和持续交付/部署 在软件开发过程中自动化构建、测试和部署的流程
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
source "https://gems.ruby-china.com"
gemspec
gem "jekyll"
gem "bundler"
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3'
services:
web:
image: nginx:latest
ports:
- "4000:80"
volumes:
- ./:/usr/share/nginx/html
12 changes: 12 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>

<head>
<title>{{ page.title }}</title>
</head>

<body>
<h1>欢迎来到我的网站!</h1>
<p>这是 HTML 版本的首页。</p>
</body>

</html>