Centos 7 docker 安装gogs详解

安装前提提交

系统版本为3.10.0以上才能安装

[root@localhost conf]# uname -a
Linux localhost.localdomain 3.10.0-693.11.1.el7.x86_64 #1 SMP Mon Dec 4 23:52:40 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost conf]# 

安装mysql

docker pull mysql/mysql-server:latest  
docker run -d --restart always  -e MYSQL_ROOT_PASSWORD=123456 --name mysql -v $PWD/mysql/my.cnf:/etc/mysql/my.cnf -v $PWD/mysql/data:/var/lib/mysql -p 3306:3306 mysql/mysql-server:latest 
docker exec -it ce33e66bd8a5 /bin/sh
mysql -uroot -p -h localhost 

修改 mysql密码及授权登录

修改本地root用户密码,并开放root远程访问权限

set password for root@localhost = password('123456');
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
FLUSH PRIVILEGES;

本地化数据

Docker mysql 把数据存储在本地目录,很简单,只需要映射本地目录到容器即可

1、加上-v参数

$ docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql -v /data/mysql/data:/var/lib/mysql -p 3306:3306 mysql 

还可以指定配置文件

docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql -v /data/mysql/my.cnf:/etc/mysql/my.cnf -v /data/mysql/data:/var/lib/mysql -p 3306:3306 mysql 

这样,即可修改配置文件,还能把数据存在本地目录,一举两得,-v 参数可以多次使用,每次映射一个目录,通过这种方式,很容易进行配置。

安装gogs镜像

[root@localhost ~]#  docker pull gogs/gogs
Using default tag: latest
Trying to pull repository docker.io/gogs/gogs ... 
latest: Pulling from docker.io/gogs/gogs
b1f00a6a160c: Pull complete 
219d6f70b7c7: Pull complete 
d7347dd7b842: Pull complete 
91781f374319: Pull complete 
2af6e1a042d8: Pull complete 
c8aa11c846cd: Pull complete 
63ac2f7bab89: Pull complete 
75e74f5ba116: Pull complete 
8c5f51d39741: Pull complete 
5e9f52d4c77e: Pull complete 
4f319b1c39f5: Pull complete 
Digest: sha256:f770a8d3f5f38bd7905ec030f3e66da800c21ce6f9c91c5c48199e6ac31f3010
[root@localhost ~]# 

运行gogs镜像

docker run -d --name=mygogs -p 2222:22 -p 9090:3000 -v /srv/gogs:/data gogs/gogs 

常见问题

监听日志,提示没有权限
mkdir: can't create directory '/data/git/': Permission denied
此时,可能是SELINUX阻止的原因

[root@localhost data]# docker logs --tail 0 -f 712c350adb28cc17b73f0a87548ab97e02f45d0b9bb56c6f6b495de258f01222

Saving key "/data/ssh/ssh_host_dsa_key" failed: No such file or directory
Saving key "/data/ssh/ssh_host_ecdsa_key" failed: No such file or directory
Saving key "/data/ssh/ssh_host_ed25519_key" failed: No such file or directory
chown: /data/ssh/*: No such file or directory
chmod: /data/ssh: No such file or directory
chmod: /data/ssh/*: No such file or directory
Could not load host key: /data/ssh/ssh_host_rsa_key
Could not load host key: /data/ssh/ssh_host_dsa_key
Could not load host key: /data/ssh/ssh_host_ecdsa_key
Could not load host key: /data/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.
mkdir: can't create directory '/data/git/': Permission denied
chmod: /data/git/.ssh: No such file or directory
./run: ./setup: line 11: can't create /data/git/.ssh/environment: nonexistent directory
chmod: /data/git/.ssh/environment: No such file or directory
chown: /data: Permission denied
chown: /data: Permission denied
chown: /data/git/: No such file or directory
chmod: /data: Permission denied
chmod: /data/gogs: No such file or directory
chmod: /data/git/: No such file or directory
2017/12/31 01:19:08 [ WARN] Custom config '/data/gogs/conf/app.ini' not found, ignore this if you're running first time
2017/12/31 01:19:08 [FATAL] [...g/setting/setting.go:501 NewContext()] Fail to create '/data/git/.ssh': mkdir /data/git: permission denied
Saving key "/data/ssh/ssh_host_rsa_key" failed: No such file or directory
^C
[root@localhost data]# 
关闭SELINUX即可解决

(1)临时关闭:setenforce 0
(2)永久关闭: vim /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
再次监听日志运行成功
[root@localhost data]# docker logs --tail 0 -f 712c350adb28cc17b73f0a87548ab97e02f45d0b9bb56c6f6b495de258f01222
usermod: no changes
Dec 31 01:32:48 syslogd started: BusyBox v1.25.1
2017/12/31 01:32:48 [ WARN] Custom config '/data/gogs/conf/app.ini' not found, ignore this if you're running first time
2017/12/31 01:32:48 [TRACE] Custom path: /data/gogs
2017/12/31 01:32:48 [TRACE] Log path: /app/gogs/log
2017/12/31 01:32:48 [TRACE] Build Time: 2017-11-22 08:19:49 UTC
2017/12/31 01:32:48 [TRACE] Build Git Hash: 
2017/12/31 01:32:48 [TRACE] Log Mode: Console (Trace)
2017/12/31 01:32:48 [ INFO] Gogs 0.11.34.1122
2017/12/31 01:32:48 [ INFO] Cache Service Enabled
2017/12/31 01:32:48 [ INFO] Session Service Enabled
2017/12/31 01:32:48 [ INFO] SQLite3 Supported
2017/12/31 01:32:48 [ INFO] Run Mode: Development
Dec 31 01:32:48 sshd[32]: Server listening on :: port 22.
Dec 31 01:32:48 sshd[32]: Server listening on 0.0.0.0 port 22.
2017/12/31 01:32:48 [ INFO] Listen: http://0.0.0.0:3000

可选安装(可自行添加git用户,并分配权限)

yum install zlib-devel perl-CPAN perl-devel curl-devel gettext expat-devel gettext-devel openssl-devel perl-Time-HiRes git -y

进入安装界面

Centos 7 docker 安装gogs详解_第1张图片
image.png

Centos 7 docker 安装gogs详解_第2张图片
image.png

注意新建好数据库,且设置好编码和排序集

Centos 7 docker 安装gogs详解_第3张图片
image.png

提示数据库设置不正确:dial tcp 192.168.0.18:3306: getsockopt: no route to host

设置好防火墙即可

[root@localhost ~]# vim /etc/firewalld/zones/public.xml


  Public
  For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
  
  
  
  
  
  
  
  
  
  
  
  


[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# 

输入安装地址

Centos 7 docker 安装gogs详解_第4张图片
image.png

添加非对称密钥公钥

Administrator@USER-20170324BD MINGW64 ~/Desktop
$ cat ~/.ssh/id_rsa.pub | clip

Administrator@USER-20170324BD MINGW64 ~/Desktop
$
Centos 7 docker 安装gogs详解_第5张图片
image.png

如遇配置不一致

修改/srv/gogs/gogs/conf其中的配置即可
[root@localhost conf]# pwd
/srv/gogs/gogs/conf
[root@localhost conf]#

[root@localhost conf]# vim app.ini 
[root@localhost conf]# 
Centos 7 docker 安装gogs详解_第6张图片
image.png

至此,gogs安装完毕!

你可能感兴趣的:(Centos 7 docker 安装gogs详解)