我已经创建了一个独立运行的postgres容器。
我希望能够在Makefile make psql
中创建命令,在其中我可以通过psql从主机连接到容器,并检查数据是否正确插入。
我正在努力编写makefile命令。到目前为止,我得到了:
Makefile
PG_CONTAINER=project_ch_pg_run_1
test_ip_1:
docker exec -it project_ch_pg_run_1 hostname -i
test_ip_2:
docker exec -it $(PG_CONTAINER) hostname -i
test_ip_3:
IP=$$(docker exec -it $(PG_CONTAINER) hostname -i); \
echo "Here's the IP of the container:$(IP)"
pslq:
IP=$$(docker exec -it project_ch_pg_run_1 hostname -i); \
psql postgres://ch_user:ch_pass@$(IP):5432/ch_dib
结果:
make test_ip_1
docker exec -it project_ch_pg_run_1 hostname -i
192.168.96.2
docker exec -it project_ch_pg_run_1 hostname -i
192.168.96.2
IP
变量中并执行替换无效。IP=$(docker exec -it project_ch_pg_run_1 hostname -i); \
echo "Here's the IP of the container:"
Here's the IP of the container:
IP
变量中并用于编写pg URI无效。IP=$(docker exec -it project_ch_pg_run_1 hostname -i); \
psql postgres://ch_user:ch_pass@:5432/ch_dib
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Makefile:15: recipe for target 'psql' failed
make: *** [psql] Error 2
我已经走了好几个小时,但是我似乎找不到链接这些命令的正确语法-真的很感谢您的帮助。
您必须牢记make变量和shell变量之间的区别。
这里:
test_ip_3:
IP=$$(docker exec -it $(PG_CONTAINER) hostname -i); \
echo "Here's the IP of the container:$(IP)"
您正确地使用docker
退出了$$(...)
程序的调用,因此该语法不被视为make变量。
但是您要设置shell变量IP
,然后在下一行中使用$(IP)
,它引用了从未设置过的make变量IP
。
您需要使用:
test_ip_3:
IP=$$(docker exec -it $(PG_CONTAINER) hostname -i); \
echo "Here's the IP of the container:$$IP"
打印shell变量IP
的值。