Docker搭建内网 composer satis 代码仓库

拉取 composer/satis 镜像

1
docker pull composer/satis

说明: 下文说明存放数据的结构是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
D:\workspace\dockerlamp\docker>nodetree -L 3
.
├── docker-compose.yml
├── docker_data
│   ├── composer # 存放satis 容器运行时使用到的配置文件的文件夹
│   │   ├── auth.json
│   │   ├── cache
│   │   ├── composer.json
│   │   └── config.json
│   ├── composer_public # 编译包输出的文件夹
│   │   ├── dist # 编译后会自动生成的文件夹
│   │   ├── include # 编译后会自动生成的文件夹
│   │   ├── index.html # 编译后会自动生成的文件
│   │   ├── p2 # 编译后会自动生成的文件夹
│   │   └── packages.json# 编译后会自动生成的文件
├── README.md
├── satis.sh

auth.json

1
2
3
{

}

composer.json

1
2
3
{

}

config.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"config": {
"secure-http": false,
"optimize-autoloader": false,
"preferred-install": "dist",
"sort-packages": true,
"platform": {
}
},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://mirrors.aliyun.com/composer/"
}
}
}

构建模块扩展

主要修改 config.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name": "composer_satis_test",
"homepage": "http://local.satis.com",
"repositories": [
{
"type": "git",
"name": "zhanghuizong/composer_satis_test",
"url": "https://gitee.com/zhanghuizong/composer_satis_test.git"
}
],
"archive": {
"directory": "dist",
"format": "zip",
"skip-dev": false,
"prefix-url": "http://local.satis.com"
}
}

摘抄自参考博文

关键词 描述
repositories 指定去哪获取包
require 指定获取哪些包,如果想获取所有包,使用require-all: true
archive.directory dist 文件存放的路径(取代 output-dir)
archive.format 可选,默认:zip, 支持两种压缩格式:ziptar。build时采用的压缩格式
archive.skip-dev 可选,默认情况下为 false,启用时(true)satis 不会为分支机构创建下载
archive.prefix-url 可选,下载位置,主页(来自satis.json),默认情况下是目录

composer 官网说明:https://docs.phpcomposer.com/articles/handling-private-packages-with-satis.html
satis GitHub代码:https://github.com/composer/satis

shell 脚本

satis.sh

1
2
3
4
5
6
#!/usr/bin/env bash

docker run --rm --init -it -v "$(pwd)"/docker_data/composer/config.json:/satis.json:ro \
-v "$(pwd)"/docker_data/composer_public:/build \
-v "$(pwd)"/docker_data/composer:/composer \
composer/satis build /satis.json /build "$@"
1
chmod +x satis.sh

增加快捷访问

编辑文件: vi ~/.bashrc

1
alias satis='docker run --rm --init -it -v "$(pwd)"/docker_data/composer/config.json:/satis.json:ro -v "$(pwd)"/docker_data/composer_public:/build -v "$(pwd)"/docker_data/composer:/composer composer/satis build /satis.json /build'

用法

1
2
3
4
5
# 构建所有代码仓库模块
sh satis.sh

# 安装指定模块
sh satis.sh zhanghuizong/composer_satis_test

注意
指定模块安装,repositories 节点中必须配置 name 字段,同时与 require 节点配置对应上

nginx 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 80;
server_name local.satis.com;
root "/data/httpd/docker-config/composer_satis/test/public/";
location / {
index index.php index.html;
}

location ~ \.php(.*)$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}

可以通过 http://ip 访问到私有仓库界面了,类似访问 https://packagist.org/

或者nginx容器

nginx 80 端口衍射为 10090

1
docker run --name some-nginx -p 10090:80 -v /some/composer_public:/usr/share/nginx/html:ro -d nginx

就可以通过 http://ip:10090 访问到私有仓库界面了,类似访问 https://packagist.org/,只是私有的稍微简陋些,但已足够使用了

项目中使用

在自己项目中的composer.json 中添加或修改类似如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"require":
{
"test/package1": "*"
}
"config":
{
"secure-http": false
}
"repositories":
{
"packagist":
{
"type": "composer",
"url": "http://127.0.0.1:10090"
}
}
}

require 中,可以改为 "test/package1": "dev-master",避免出现类似以下错误:

1
Could not find a version of package xxx/yyy matching your minimum-stability (stable).Require it with an explicit version constrai

后记:

  1. 如果 vendor文件夹在版本管理中不是忽略的,在项目中使用之前,建议是先打一个tag,然后推送 tagconfig.json中填写的git仓库地址中,进行一次 satis 编译。因为我发现安装 dev-master版本的库,在已安装的这个库下面会有一个 .git 文件夹,这样这个库就是 git的一个子模块了,在部署或者其它人无法同步下来,只能重新删除,再次安装这个私有库。

https://github.com/composer/satis的社区工具https://github.com/ludofleury/satisfy

satis.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"name": "localhost\/respository",
"description": "company private composer repository",
"homepage": "http:\/\/192.168.0.201:10090",
"output-dir": "public",
"output-html": true,
"require-all": false,
"require-dependencies": false,
"require-dev-dependencies": false,
"require-dependency-filter": false,
"repositories": [
],
"archive": {
"directory": "dist",
"format": "zip",
"skip-dev": true,
"whitelist": [],
"blacklist": [],
"prefix-url": "http:\/\/192.168.0.201:10090",
"checksum": true
},
"minimum-stability": "dev",
"providers": false,
"pretty-print": true
}

在进入 http://ip/admin中,点击 Edit configuration,其中 config 填写的值是:

1
2
3
4
"config": {
"secure-http": false,
"preferred-install": "dist",
}

References