我没有复杂的设置。2个容器,一个用于我的应用程序,一个用于我的API。
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8f07c240bee2 nginx "nginx -g 'daemon off" 13 hours ago Up 13 hours 443/tcp, 0.0.0.0:7010->80/tcp simtp_front_1
ae4296bd51d8 simtp_engine "php-fpm" 13 hours ago Up 13 hours 9000/tcp simtp_engine_1
4ae15fcba793 nginx "nginx -g 'daemon off" 6 days ago Up 40 hours 443/tcp, 0.0.0.0:7011->80/tcp simtpapi_front_1
cf04c9567201 simtpapi_engine "php-fpm" 6 days ago Up 40 hours 9000/tcp simtpapi_engine_1
3da782cca9b0 mysql:5.6 "docker-entrypoint.sh" 6 days ago Up 40 hours 0.0.0.0:3308->3306/tcp simtpapi_db_1
我想在我的应用程序中使用 Guzzle 来调用我的 API,因此我将两个容器链接在一起,但是当我测试 curl 时,它返回:
curl: (7) Failed to connect to simtpapi port 7011: Connection refused
我可以毫无问题地 ping 容器:
root@ae4296bd51d8:/var/www/simtp# ping simtpapi
PING simtpapi (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3: icmp_seq=0 ttl=64 time=0.064 ms
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.054 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.056 ms
^C--- simtpapi ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.054/0.058/0.064/0.000 ms
这是我的应用程序中的 docker-compose 文件:
front:
image: nginx
ports:
- "7010:80"
links:
- "engine:engine"
volumes:
- ".:/var/www/simtp:ro"
- "./docker/front/default.conf:/etc/nginx/conf.d/default.conf:ro"
engine:
build: ./docker/engine/
external_links:
- "simtpapi_engine_1:simtpapi"
volumes:
- ".:/var/www/simtp:rw"
- "./docker/engine/php.ini:/usr/local/etc/php/conf.d/custom.ini:ro"
working_dir: "/var/www/simtp"
从我的 API 来看:
front:
image: nginx
ports:
- "7011:80"
links:
- "engine:engine"
volumes:
- ".:/var/www/simtp-api:ro"
- "./docker/front/default.conf:/etc/nginx/conf.d/default.conf:ro"
db:
image: mysql:5.6
ports:
- "3308:3306"
environment:
- "MYSQL_ROOT_PASSWORD=password"
engine:
build: ./docker/engine/
volumes:
- ".:/var/www/simtp-api:rw"
- "./docker/engine/php.ini:/usr/local/etc/php/conf.d/custom.ini:ro"
links:
- "db:db"
working_dir: "/var/www/simtp-api"
Curl 版本
curl 7.38.0 (x86_64-pc-linux-gnu) libcurl/7.38.0 OpenSSL/1.0.1t zlib/1.2.8 libidn/1.29 libssh2/1.4.3 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API SPNEGO NTLM NTLM_WB SSL libz TLS-SRP
Docker、Docker-machine 和 docker-compose
PS C:\working_directory\simtp> docker -v
Docker version 1.12.3, build 6b644ec, experimental
PS C:\working_directory\simtp> docker-machine -v
docker-machine.exe version 0.8.2, build e18a919
PS C:\working_directory\simtp> docker-compose -v
docker-compose version 1.8.1, build 004ddae
我实际上使用的是适用于 Windows 的 Docker(Windows 10,启用了开发人员模式和 Hyper-V 容器)。
答案1
你的 curl 命令是什么?你的 Compose 文件中有这个命令:
ports:
- "7011:80"
您的 curl 命令显示端口 7011 拒绝连接,这是因为您使用错误的端口进行 curl 操作吗?请确保您使用端口 80,因为使用上述指令,您已在主机上将端口 7011 映射到 80。
另外,请记住,您在两个文件中都做了相同的操作,因此两个应用程序可能都尝试侦听端口 80。为了简单起见,我将分别将端口更改为:
ports:
- "7010:7010"
ports:
- "7011:7010"