使用Elastic docker镜像进行端口转发

时间:2016-09-28 10:42:28

标签: elasticsearch docker

我正在尝试使用this docker image测试docker。事情应该是直截了当的。但事实并非如此。

我运行此命令来启动容器:

sudo docker run -d -p 9200:9200 -p 9300:9300 elasticsearch -Des.node.name="ElasticTestNode"

然后我尝试在我的主机上运行此命令:

# curl -XPUT "http://localhost:9200/movies/movie/3" -d'
{
    "title": "To Kill a Mockingbird",
    "director": "Robert Mulligan",
    "year": 1962,
    "genres": ["Crime", "Drama", "Mystery"]
}'

我期待看到某种成功的信息。相反,命令只是卡住了。没有输出,根本没有停止。我必须按Ctrl-X退出。

出于想法,我在docker中启动了一个bash shell并进行了测试:

sudo sudo docker exec -i -t some-docker-id /bin/bash
root@somehash:/usr/share/elasticsearch# curl -XPUT "http://localhost:9200/movies/movie/3" -d'
{
    "title": "To Kill a Mockingbird",
    "director": "Robert Mulligan",
    "year": 1962,
    "genres": ["Crime", "Drama", "Mystery"]
}'
{"_index":"movies","_type":"movie","_id":"3","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}root@somehash:/usr/share/elasticsearch#

这是成功的。我做错了什么?

更新:在我的主机上尝试了另一个命令:

$ curl -XPUT -v "http://localhost:9200/movies/movie/3" -d'
{
    "title": "To Kill a Mockingbird",
    "director": "Robert Mulligan",
    "year": 1962,
    "genres": ["Crime", "Drama", "Mystery"]
}'
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 9200 (#0)
> PUT /movies/movie/3 HTTP/1.1
> Host: localhost:9200
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Length: 139
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 139 out of 139 bytes

被困在这里......

# sudo docker logs docker-id
[2016-09-28 11:52:16,630][INFO ][node                     ] [ElasticTestNode] version[2.4.0], pid[1], build[ce9f0c7/2016-08-29T09:14:17Z]
[2016-09-28 11:52:16,631][INFO ][node                     ] [ElasticTestNode] initializing ...
[2016-09-28 11:52:17,202][INFO ][plugins                  ] [ElasticTestNode] modules [reindex, lang-expression, lang-groovy], plugins [], sites []
[2016-09-28 11:52:17,219][INFO ][env                      ] [ElasticTestNode] using [1] data paths, mounts [[/usr/share/elasticsearch/data (/dev/sda8)]], net usable_space [5.4gb], net total_space [19.5gb], spins? [possibly], types [ext4]
[2016-09-28 11:52:17,219][INFO ][env                      ] [ElasticTestNode] heap size [990.7mb], compressed ordinary object pointers [true]
[2016-09-28 11:52:18,816][INFO ][node                     ] [ElasticTestNode] initialized
[2016-09-28 11:52:18,816][INFO ][node                     ] [ElasticTestNode] starting ...
[2016-09-28 11:52:18,877][INFO ][transport                ] [ElasticTestNode] publish_address {172.17.0.22:9300}, bound_addresses {[::]:9300}
[2016-09-28 11:52:18,881][INFO ][discovery                ] [ElasticTestNode] elasticsearch/LCo5k0dARimsWFXjN1Yu0A
[2016-09-28 11:52:21,915][INFO ][cluster.service          ] [ElasticTestNode] new_master {ElasticTestNode}{LCo5k0dARimsWFXjN1Yu0A}{172.17.0.22}{172.17.0.22:9300}, reason: zen-disco-join(elected_as_master, [0] joins received)
[2016-09-28 11:52:21,924][INFO ][http                     ] [ElasticTestNode] publish_address {172.17.0.22:9200}, bound_addresses {[::]:9200}
[2016-09-28 11:52:21,925][INFO ][node                     ] [ElasticTestNode] started
[2016-09-28 11:52:21,960][INFO ][gateway                  ] [ElasticTestNode] recovered [0] indices into cluster_state

1 个答案:

答案 0 :(得分:0)

似乎docker的端口映射有时会失败。我多次遇到过这个问题。相同的测试脚本适用于启动,而不适用于另一个。

一致性是,如果一次启动时出现问题,每次重新启动docker镜像时都会失败。即使在我放弃容器并用图像开始新的容器之后它仍然会失败。这似乎是docker守护进程的一个问题。

我解决此问题的方法是停止所有容器并重新启动docker守护程序:

sudo docker stop $(docker ps -a -q)
sudo systemctl restart docker
sudo docker start $(docker ps -a -q)

它对我有用。希望有人会觉得它有用。