和ChatGPT关于Swing music的一场对话(上篇)

文章讲述了非程序员老苏如何利用Docker和ChatGPT将SwingMusic服务端和客户端转化为Docker镜像的过程,包括修改Dockerfile,设置阿里源,安装依赖,以及处理客户端构建和前后端集成。最终实现了SwingMusic的Docker化部署。
摘要由CSDN通过智能技术生成

在这里插入图片描述

什么是 Swing Music ?

Swing Music 是一款漂亮的自托管音乐播放器,适用于您的本地音频文件。就像一个更酷的 Spotify …但带上你自己的音乐。

第一次在 reddit 上看到 Swing Music,就被其 UI 吸引了

在这里插入图片描述

但源码站点的releases 中只有 windowslinux 的版本

在这里插入图片描述

老苏不是程序员,但作为一个爱折腾的人,岂能轻易放弃,何不借助最近火的一塌糊涂的 ChatGPT,让其协助完成 Docker 版呢?于是就有了下面👇的一场对话

与 ChatGPT 的对话

以下只记录了对话的要点,不是事无巨细的完整记录。整个对话断断续续进行了 2天,其中包含了老苏查资料、编译、测试的时间

服务端

首先当然是看官方的安装手册了

在这里插入图片描述

说实话没看懂,虽然之前折腾过 python 的项目,但是 poetry 是什么鬼?

于是老苏向 ChatGPT 提了第一个问题


  • Q:如何将下面这段命令改编为 Dockerfile
git clone https://github.com/geoffrey45/swingmusic.git

cd swingmusic

# install dependencies using poetry
poetry install

# start the app
poetry run python manage.py

ChatGPT 很快就做出了响应,看起来是不是头头是道的?不仅给了代码,还有详细的解释


  • A:将上述命令编写为 Dockerfile 的步骤如下:
  1. 创建一个名为 Dockerfile 的文件,将以下内容复制到文件中:
FROM python:3.8-slim-buster

# 更换国内源
RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN sed -i s@/security.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN  apt-get clean

# 安装 Git 和 Poetry
RUN apt-get update && \
    apt-get install -y git && \
    curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python

# 下载代码
RUN git clone https://github.com/geoffrey45/swingmusic.git /app

# 安装依赖
WORKDIR /app
RUN poetry install --no-dev

# 启动应用
CMD ["poetry", "run", "python", "manage.py"]

Dockerfile 基于 Python 3.8 官方镜像构建,安装了 GitPoetry,从 GitHub 上下载了 geoffrey45/swingmusic 项目的代码,安装了项目依赖,并在容器启动时启动了应用程序。

  1. 在命令行中进入包含 Dockerfile 文件的目录,并执行以下命令构建 Docker 镜像:

    docker build -t my-swingmusic-app .

    其中,my-swingmusic-app 是您为该 Docker 镜像指定的名称。

  2. 构建完成后,可以使用以下命令启动 Docker 容器:

    docker run --name my-swingmusic-container my-swingmusic-app

    其中,my-swingmusic-container 是您为该 Docker 容器指定的名称。

希望这些步骤能够帮助您将上述命令编写为 Dockerfile,并成功构建和运行 Docker 容器。


接来下老苏让 ChatGPT 改了阿里源、 python 的版本,同时根据编译的情况做了修正,最后得到了可以编译过的服务端的 Dockerfile 文件

FROM python:3.11-slim-buster  
  
# 改阿里源  
RUN sed -i "s@http://deb.debian.org@http://mirrors.aliyun.com@g" /etc/apt/sources.list  
RUN cat /etc/apt/sources.list  
RUN rm -Rf /var/lib/apt/lists/*
  
# 安装 Git 和 Poetry  
RUN apt-get update && \  
    apt-get install -y git curl
  
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python -  
ENV PATH /root/.local/bin:$PATH  
  
# 下载代码  
RUN git clone https://ghproxy.com/github.com/geoffrey45/swingmusic.git /app  
  
# 安装依赖  
WORKDIR /app  
#RUN pip install pyinstaller && \  
RUN pip install --upgrade pip && \  
    poetry install

EXPOSE 1970
# 启动应用  
CMD ["poetry", "run", "python", "manage.py"]

客户端

如法炮制,让 ChatGPT 将客户端的安装过程转为 Dockerfile 文件

在这里插入图片描述


  • Q:如何将下面这段命令改编为 Dockerfile
git clone https://github.com/geoffrey45/swing-client.git

cd swing-client

# Install dependencies
yarn install

# Start the development server
yarn dev

# Build the project
yarn build

ChatGPT 同样很快就有了回应


  • A:以下是将这段命令编写成 Dockerfile 的示例:
FROM node:latest

RUN apt-get update && apt-get install -y git

RUN git clone https://github.com/geoffrey45/swing-client.git /app

WORKDIR /app

RUN yarn install

RUN yarn build

EXPOSE 3000

CMD ["yarn", "start"]

在该 Dockerfile 中,我们使用了 Node.js 的官方镜像作为基础镜像,并安装了 Git 。接着,我们克隆了 Swing 客户端的代码并将其复制到 Docker 容器中的 /app目录下。然后,我们在 /app 目录中运行了 yarn installyarn build 命令来安装依赖和构建项目。最后,我们暴露了容器的 3000 端口并在容器启动时运行了 yarn start 命令。


接下来,老苏要求改为多阶段,采用阿里的源等多次反复之后,得到了下面的 Dockerfile

# 第一阶段:构建应用
FROM node:latest as builder

RUN apt-get update && apt-get install -y git

# 设置淘宝镜像源
RUN npm config set registry https://registry.npm.taobao.org/

RUN git clone https://github.com/geoffrey45/swing-client.git /app

WORKDIR /app

RUN yarn install

RUN yarn build

# 第二阶段:运行应用
FROM node:latest

# 设置淘宝镜像源
RUN npm config set registry https://registry.npm.taobao.org/

COPY --from=builder /app /app

WORKDIR /app

EXPOSE 3000

CMD ["yarn", "start"]

构建镜像

如果你不想自己构建,可以跳过,直接阅读下一章节

在这个项目中,老苏还是有贡献的 😂 ,对 ChatGPT 给的代码进行了融合、编译和调试,最终的 Dockerfile 是下面这样的

#前端构建    
FROM node:latest as builder    
    
# 改阿里源      
RUN sed -i "s@http://deb.debian.org@http://mirrors.aliyun.com@g" /etc/apt/sources.list    
RUN cat /etc/apt/sources.list      
RUN rm -Rf /var/lib/apt/lists/*    
    
# 安装 Git    
RUN apt-get update && \    
    apt-get install -y git    
    
# 设置yarn镜像为国内镜像    
RUN yarn config set registry https://registry.npm.taobao.org --global && \    
    yarn config set disturl https://npm.taobao.org/dist --global    
    
# 下载客户端代码    
RUN git clone https://github.com/geoffrey45/swing-client.git /app    
    
# 删除 yarn.lock,否则改源没有用    
RUN rm -Rf /app/yarn.lock    
    
# 安装依赖     
WORKDIR /app    
RUN yarn install && \    
    yarn build    
    
# 后端构建    
FROM python:3.11-slim-buster     
MAINTAINER laosu<wbsu2003@gmail.com> 
      
# 改阿里源  
RUN sed -i "s@http://deb.debian.org@http://mirrors.aliyun.com@g" /etc/apt/sources.list      
RUN cat /etc/apt/sources.list      
RUN rm -Rf /var/lib/apt/lists/*    
      
# 安装 Git 和 Poetry      
RUN apt-get update && \      
    apt-get install -y git curl    
      
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python -      
ENV PATH /root/.local/bin:$PATH      
      
# 下载服务端代码      
RUN git clone https://github.com/geoffrey45/swingmusic.git /app  
  
# 复制前端    
COPY --from=builder /app/dist/. /app/client/.    
  
# 安装依赖      
WORKDIR /app      
#RUN pip install pyinstaller && \      
RUN pip install --upgrade pip && \      
    poetry install    
    
EXPOSE 1970    
ENV XDG_CONFIG_HOME=/data  
  
# 替换 localhost 为 0.0.0.0,否则容器外无法访问  
RUN sed -i "s@localhost@0.0.0.0@g" /app/app/settings.py  
  
# 启动应用      
CMD ["poetry", "run", "python", "manage.py"]

构建镜像和容器运行的基本命令如下👇

# 新建目录
mkdir swingmusic

# 进入目录
cd swingmusic

# 创建 Dockerfile 文件
touch Dockerfile

# 构建镜像  
docker build -t wbsu2003/swingmusic:v1 .  

# 运行容器  
docker run -d \
   --name swingmusic \
   -p 1970:1970 \
   -v $(pwd)/data:/data \
   -v $(pwd)/music:/music \
   wbsu2003/swingmusic:v1

下篇进入 Swing Music 的安装、使用环节,咱们周三不见不散。

参考文档

swing-opensource/swingmusic: Swing Music is a beautiful, self-hosted music player for your local audio files. Like a cooler Spotify … but bring your own music.
地址:https://github.com/swing-opensource/swingmusic

swing-opensource/swingmusic-client: Browser-based client for the swing music player: (https://github.com/swing-opensource/swingmusic). Powered by VueJS, Typescript, Pinia and Axios
地址:https://github.com/swing-opensource/swingmusic-client

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨浦老苏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值