Docker Community Forums

Share and learn in the Docker community.

Cannot connect to port 8091 using curl from within entrypoint script

docker

(Abhijit Sarkar) #1

Running docker-machine version 0.5.0, Docker version 1.9.0 on OS X 10.11.1.

I’ve a Couchbase image of my own (not the official one). From inside the entrypoint script, I’m running some curl commands to configure the Couchbase server and to load sample data. Problem is, curl fails with error message Failed to connect to localhost port 8091: Connection refused.
I’ve tried 127.0.0.1, 0.0.0.0, localhost, all without any success. netstat shows that port 8091 on localhost is listening. If I later log on to the server using docker exec and run the same curl commands, those work! What am I missing?

Error:

couchbase4 |   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
couchbase4 |                                  Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to localhost port 8091: Connection refused

netstat output:

root@cd4d3eb00666:/opt/couchbase/var/lib# netstat -lntu                              
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:21100           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:21101           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:9998          0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:4369            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:8091            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:8092            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:41125           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:11209           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:11210           0.0.0.0:*               LISTEN     
tcp6       0      0 :::11209                :::*                    LISTEN     
tcp6       0      0 :::11210                :::*                    LISTEN

(Abhijit Sarkar) #2

It turns out that if I do the curls after restarting the server, those work. Go figure! That said, note that the REST API for installing sample buckets is undocumented as far as I know. Arun Gupta’s blog is the only place where I saw any mention of a REST call for installing sample buckets. There’s a python script available but that requires installing python-httplib2.

That said, Arun Gupta’s curl statement may be improved upon as follows:

if [ -n "$SAMPLE_BUCKETS" ]; then
	IFS=',' read -ra BUCKETS <<< "$SAMPLE_BUCKETS"

	for bucket in "${BUCKETS[@]}"; do
		printf "\n[INFO] Installing %s.\n" "$bucket"
		curl -sSL -w "%{http_code} %{url_effective}\\n" -u $CB_USERNAME:$CB_PASSWORD --data-ascii '["'"$bucket"'"]' $ENDPOINT/sampleBuckets/install
	done
fi

where SAMPLE_BUCKETS can be a comma-separated environment variable, possible values being combinations of gamesim-sample, beer-sample and travel-sample. The --data-ascii option keeps curl from choking on the dynamically created JSON.

Now if only there was an easy way to start the server in the foreground. :slight_smile: