Docker 搭建 Gogs
准备工作
- 先准备配置目录和持久化目录,举个栗子:
mkdir -p /opt/module/gogs/{data,backup}
拉取官方Gogs镜像
bash
# 拉取 gogs:0.13
docker pull gogs/gogs:0.13
# 拉取最新版 gogs 镜像
docker pull gogs/gogs
启动一个临时容器【通过创建一个临时的测试容器,用来复制配置文件,来反推配置】
bash
docker run -d \
-p 10022:22 \
-p 13000:3000 \
-v /opt/module/gogs/data:/data \
--name=gogs \
gogs/gogs:0.13
参数说明:
docker run -d
: 启动一个Docker容器并将其设置为在后台(守护进程模式)运行。-p 10022:22
: 将主机的端口10022映射到容器内部的端口22,这允许你通过主机的10022端口访问容器内部的SSH服务。-p 13000:3000
: 将主机的端口10300映射到容器内部的端口3000,这允许你通过主机的10300端口访问容器内部的Gogs Web应用程序。-v /opt/module/gogs/data:/data
: 将主机上的/opt/module/gogs/data
目录挂载到容器内部的/data
目录,这样可以持久化地存储Gogs的数据,以防容器停止后数据丢失。--name=gogs
: 为容器指定一个名称,即"gogs",以便在后续操作中更容易引用该容器。gogs/gogs:0.13
: 指定要运行的Docker镜像的名称和标签。在这里,使用的是Gogs官方镜像的0.13版本。
复制gogs的配置文件到宿主机中
bash
# 将容器app.ini文件复制到宿主机
docker cp gogs:/data/gogs/conf/app.ini /opt/module/gogs/conf
# 将容器data文件夹下内容复制到宿主机
docker cp gogs:/data /opt/module/gogs/data
# 将容器中的log文件夹复制到宿主机【可以不执行】
docker cp gogs:/var/log/nginx /opt/module/nginx/logs
bash
docker cp gogs:/data /opt/module/gogs/data
移除临时容器
bash
docker stop gogs
docker rm gogs
运行镜像
bash
docker run -d \
-p 10022:22 \
-p 13000:3000 \
-v /opt/module/gogs/data:/data \
-v /opt/module/gogs/backup:/backup \
-e TZ="Asia/Shanghai"\
--name=gogs \
--restart=always \
gogs/gogs:0.13
参数说明:
docker run -d
: 启动一个Docker容器并将其设置为在后台(守护进程模式)运行。-p 10022:22
: 将主机的端口10022映射到容器内部的端口22,这允许你通过主机的10022端口访问容器内部的SSH服务。-p 13000:3000
: 将主机的端口10300映射到容器内部的端口3000,这允许你通过主机的10300端口访问容器内部的Gogs Web应用程序。-v /opt/module/gogs/data:/data
: 将主机上的/opt/module/gogs/data
目录挂载到容器内部的/data
目录,这样可以持久化地存储Gogs的数据,以防容器停止后数据丢失。-v /opt/module/gogs/backup:/backup
: 将主机上的/opt/module/gogs/backup
目录挂载到容器内部的/backup
目录,这样可以持久化地存储Gogs的数据,以防容器停止后数据丢失。-e TZ="Asia/Shanghai"
: 设置容器的时区为"Asia/Shanghai"。--name=gogs
: 为容器指定一个名称,即"gogs",以便在后续操作中更容易引用该容器。--restart=always
: 指定容器的重启策略为始终自动重启。这意味着如果容器意外停止,Docker将自动重新启动容器。gogs/gogs:0.13
: 指定要运行的Docker镜像的名称和标签。在这里,使用的是Gogs官方镜像的0.13版本。
查看启动日志
bash
#docker logs [服务id/服务名]
docker logs gogs
docker logs -f -t --tail 20 gogs
进入gogs容器内部
bash
docker ps # 查看正在运行的容器,并得到 容器id
# 进入tomcat容器内部
docker exec -it [容器id] /bin/bash # 中间那个是容器id 就是 CONTAINER_ID
docker exec -it [容器id] /bin/sh
# -it 表示进入
bash
docker exec -it gogs sh
docker exec -it gogs /bin/sh
bash
# 使用 命令别名 开启 ll命令
cat <<EOF >> /etc/profile
alias ll='ls -l --color=tty'
alias grep='grep --color=auto'
EOF
source /etc/profile
查看防火墙【开放端口】
bash
# 查询端口是否开放
firewall-cmd --query-port=10022/tcp
firewall-cmd --query-port=13000/tcp
# 开放80、443端口
firewall-cmd --permanent --add-port=10022/tcp
firewall-cmd --permanent --add-port=13000/tcp
# 移除端口
firewall-cmd --permanent --remove-port=10022/tcp
firewall-cmd --permanent --remove-port=13000/tcp
# 重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload
# 查看已经开发那些端口
firewall-cmd --list-ports
firewall-cmd --zone=public --list-ports
测试
bash
#浏览器中服务器
http://虚拟机IP:13000
http://虚拟机IP:10022
常用Git命令
bash
# 配置
git config --global user.name "Your Name Comes Here"
git config --global user.email you@yourdomain.example.com
# 查看
git --version
git status
# 初始仓库
git init
# 添加至暂存区
git add ./file (将需要提交的提交到暂存区)
# 提交本地仓库
git commit - m 'feat: 注释' (提交到本地)
git commit --amend
# 快捷提交
git commit -am 'feat: 注释'
# 直接回退
git log --oneline
git checkout filename
# 从暂存区回退
git reset HEAD filename
git checkout -- filename
# 从本地仓回退
git log --oneline
git reset --hard XXX
# 从本地仓重置回退
git reflog
git reset --hard XXX
# 查看分支
git branch 查看本地所有分支
git branch -a 查看所有的分支
git branch -r 查看远程所有分支
# 创建分支
git branch 分支名称
# 切换分支
git checkout 分支名称
# 创建并切换
git checkout -b 分支名称
# 合并分支
git merge 目标分支 -m "描述"
git rebase 目标分支
# 将本地的Test分支推送到远端的master分支
git push origin Test:master
# 删除分支
git branch -d 分支
# 删除远程分支
git push origin --delete 分支
# 查看tag
git tag
# 查看详情
git show tag名称
# 创建tag
git tag -a "v1.0" -m "描述"
git tag -a "v1.0" 标识码 -m "描述"
# 删除tag
git tag -d 标签名
# 查看远程仓库
git remote [-v]
# 删除关联
git remote remove origin
# 推送tag
git push origin --tags
git push tag名称
# 删除远程tag
git push origin --delete tag名称
# 获取远程tag
git fetch origin tag V1.2
# 拉取远程代码
git pull <远程主机名> <远程分支名>:<本地分支名>
# 一、开发分支(develop)上的代码达到上线的标准后,要合并到 master 分支
git checkout develop
git pull
git checkout master
git merge develop
git push -u origin master
# 二、当master代码改动了,需要更新开发分支(develop)上的代码
git checkout master
git pull
git checkout develop
git merge master
git push -u origin develop
删除容器
注意: 还有删除数gogs据库
1. 停止和删除Gogs容器
bash
docker stop gogs
docker rm gogs
2.查看数据卷的详细信息,包括挂载到容器的数据卷的名称:
bash
docker inspect gogs
这将提供Gogs容器的详细信息,包括挂载的数据卷的名称。在JSON输出中,寻找"Mounts"字段,其中包含有关挂载的信息。
3.一旦你找到了相关的数据卷名称,你可以使用以下命令删除它:
bash
#查看所有数据卷
docker volume ls
docker volume rm <volume_name>
请将<volume_name>
替换为实际的数据卷名称,这样就可以删除与之关联的数据卷。
实际操作
bash
[root@192 ~]# docker inspect gogs
[
{
"Id": "aa5966fa1e07c9830a93d146f4eebc0510db4f3d484f1aa74c57fd045a3efd78",
"Created": "2023-09-08T10:35:12.579405355Z",
"Path": "/app/gogs/docker/start.sh",
"Args": [
"/bin/s6-svscan",
"/app/gogs/docker/s6/"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 25338,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-09-08T10:35:12.847045044Z",
"FinishedAt": "0001-01-01T00:00:00Z",
"Health": {
"Status": "healthy",
"FailingStreak": 0,
"Log": [
{
"Start": "2023-09-08T18:40:43.607286021+08:00",
"End": "2023-09-08T18:40:43.684070251+08:00",
"ExitCode": 0,
"Output": ""
},
{
"Start": "2023-09-08T18:41:13.687167438+08:00",
"End": "2023-09-08T18:41:13.76545214+08:00",
"ExitCode": 0,
"Output": ""
},
{
"Start": "2023-09-08T18:41:43.769161045+08:00",
"End": "2023-09-08T18:41:43.851412268+08:00",
"ExitCode": 0,
"Output": ""
},
{
"Start": "2023-09-08T18:42:13.859782099+08:00",
"End": "2023-09-08T18:42:13.943519873+08:00",
"ExitCode": 0,
"Output": ""
},
{
"Start": "2023-09-08T18:42:43.951301454+08:00",
"End": "2023-09-08T18:42:44.023457417+08:00",
"ExitCode": 0,
"Output": ""
}
]
}
},
"Image": "sha256:4fa5cedc038517695cf354561e6eceec91d1803bc8b8d84744c796f2e47993b4",
"ResolvConfPath": "/var/lib/docker/containers/aa5966fa1e07c9830a93d146f4eebc0510db4f3d484f1aa74c57fd045a3efd78/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/aa5966fa1e07c9830a93d146f4eebc0510db4f3d484f1aa74c57fd045a3efd78/hostname",
"HostsPath": "/var/lib/docker/containers/aa5966fa1e07c9830a93d146f4eebc0510db4f3d484f1aa74c57fd045a3efd78/hosts",
"LogPath": "/var/lib/docker/containers/aa5966fa1e07c9830a93d146f4eebc0510db4f3d484f1aa74c57fd045a3efd78/aa5966fa1e07c9830a93d146f4eebc0510db4f3d484f1aa74c57fd045a3efd78-json.log",
"Name": "/gogs",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/opt/module/gogs/data:/data"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"22/tcp": [
{
"HostIp": "",
"HostPort": "10022"
}
],
"3000/tcp": [
{
"HostIp": "",
"HostPort": "13000"
}
]
},
"RestartPolicy": {
"Name": "always",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
41,
207
],
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/92c6c2732f4f260fe37eb1965fc8c0b8b6c012c2b6d643011f8e295161106e14-init/diff:/var/lib/docker/overlay2/e8e4330e6a3c559eb093d7ee19dd92099ed5991c02623e70b288df5682bb99ac/diff:/var/lib/docker/overlay2/fb16d9b7d34cde7249809fec0867f74a96602cbc5e1e41df6e2229c318f1297b/diff:/var/lib/docker/overlay2/e52f016f52d3310c2b79b3cd41283f2fcb87778266b7657175ea11b97138bcf7/diff:/var/lib/docker/overlay2/3f28c4f1444f9c71a36d18744e709da916a7d3aae75fb501d61f3fbf5d504cbd/diff:/var/lib/docker/overlay2/ed08f7907ca663d1c06ccd8f595f7d9e0f0d9e320f4679dc47caf042df8dcc54/diff:/var/lib/docker/overlay2/732dc76ce54ea2e95f6c47811827e647e9739f587f0a6f8cb19c4ae89a6ddc64/diff:/var/lib/docker/overlay2/1f980dc96ce58159f4254a109d3ecf1d55261e37d533dfcbe75bc2c642850fdd/diff",
"MergedDir": "/var/lib/docker/overlay2/92c6c2732f4f260fe37eb1965fc8c0b8b6c012c2b6d643011f8e295161106e14/merged",
"UpperDir": "/var/lib/docker/overlay2/92c6c2732f4f260fe37eb1965fc8c0b8b6c012c2b6d643011f8e295161106e14/diff",
"WorkDir": "/var/lib/docker/overlay2/92c6c2732f4f260fe37eb1965fc8c0b8b6c012c2b6d643011f8e295161106e14/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/opt/module/gogs/data",
"Destination": "/data",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "volume",
"Name": "4c4048a2ae7c1daed9004d6551878050cc8bfa318fcdc08d95ea73021aa17fcb",
"Source": "/var/lib/docker/volumes/4c4048a2ae7c1daed9004d6551878050cc8bfa318fcdc08d95ea73021aa17fcb/_data",
"Destination": "/backup",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "aa5966fa1e07",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"22/tcp": {},
"3000/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"TZ=Asia/Shanghai",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOGS_CUSTOM=/data/gogs"
],
"Cmd": [
"/bin/s6-svscan",
"/app/gogs/docker/s6/"
],
"Healthcheck": {
"Test": [
"CMD-SHELL",
"(curl -o /dev/null -sS http://localhost:3000/healthcheck) || exit 1"
]
},
"Image": "gogs/gogs:0.13",
"Volumes": {
"/backup": {},
"/data": {}
},
"WorkingDir": "/app/gogs",
"Entrypoint": [
"/app/gogs/docker/start.sh"
],
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "09962969d9df5d6ecc24bc036b7b0841236be252a03f478377b9d74043044e4f",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"22/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "10022"
},
{
"HostIp": "::",
"HostPort": "10022"
}
],
"3000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "13000"
},
{
"HostIp": "::",
"HostPort": "13000"
}
]
},
"SandboxKey": "/var/run/docker/netns/09962969d9df",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "e92cbb6dfd8e64c2dc986bdeb3b4b10871f1117eedfb2d961c819cffc95571e9",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.5",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:05",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "ddd0a110308f6f26f3a3b4e63faeefc70c565fc776148030a4498acaf49d8937",
"EndpointID": "e92cbb6dfd8e64c2dc986bdeb3b4b10871f1117eedfb2d961c819cffc95571e9",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.5",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:05",
"DriverOpts": null
}
}
}
}
]
根据Docker容器的详细信息,你的Gogs容器在挂载了两个卷(volumes):
-
/opt/module/gogs/data
目录挂载到容器的/data
目录,这个卷用于持久化存储Gogs的数据。 -
还有一个匿名卷,它被挂载到容器的
/backup
目录。这个卷通常由Gogs用于备份目的,可能包含一些历史数据。
要删除与Gogs相关的历史数据,你可以执行以下步骤:
-
停止和删除Gogs容器(如果尚未完成):
bashdocker stop gogs docker rm gogs
-
删除Gogs数据卷:
对于
/opt/module/gogs/data
目录的数据卷,你可以使用以下命令删除它:bashdocker volume rm gogs_data
如果你想删除备份目录的匿名卷,可以使用以下命令:
bashdocker volume rm 4c4048a2ae7c1daed9004d6551878050cc8bfa318fcdc08d95ea73021aa17fcb
请谨慎操作,确保你不再需要这些数据。
-
然后,重新创建Gogs容器(使用之前的命令):
bashdocker run -d \ -p 10022:22 \ -p 10300:3000 \ --name=gogs \ gogs/gogs:0.13
通过执行以上步骤,你将删除与Gogs相关的历史数据,并重新启动一个新的Gogs容器,以确保不再使用旧的数据。