我正在尝试使用以下模块使用ansible运行docker映像
- name: run the image in a docker container
docker_container:
name: deploy1
image: redhat-jboss
state: started
exposed_ports:
- 8089/tcp"
,但是容器暴露在多个端口中。
$ docker ps -a
CONTAINER ID- 18f8hddd237 IMAGE 18f8hddd237
COMMAND "/docker-entrypoint.…"
CREATED 11 seconds ago
STATUS Up 10 seconds NAMES redhat-jboss
PORTS 22/tcp, 3528-3529/tcp, 4447/tcp, 4712-4713/tcp, 5432/tcp, 5445/tcp, 5455/tcp, 7500/tcp, 7600/tcp, 8009/tcp, 8080/tcp, 8089-8090/tcp, 8443/tcp, 8787/tcp, 9990/tcp, 9999/tcp, 23364/tcp, 45700/tcp, 57600/tcp deploy1
让我知道如何解决此问题。
答案 0 :(得分:0)
使用ports
代替exposed_ports
。
在您的情况下,应该是这样的:
name: run the image in a docker container
docker_container:
name: deploy1
image: redhat-jboss
state: started
ports:
- '0.0.0.0:8089:8089'
说明。可用的docker_container模块中有两个不同的参数:
exposed_ports
是“其他容器端口的列表,它通知Docker该容器在运行时监听指定的网络端口。如果该端口已经在Dockerfile中使用EXPOSE公开,则无需再次公开”。因此,这只是用您添加的内容扩展了Dockerfile的EXPOSE端口。published_ports
(别名为ports
):“要从容器发布到主机的端口列表。”这些端口实际上将连接到主机的端口。有关如何打开端口的更多信息,您可以在模块的official documentation中阅读。
答案 1 :(得分:0)
Are exposed ports accessible to the host system by default? No.
Are exposed ports accessible to linked containers? Yes.
Are exposed ports accessible to other containers? No.
What is an exposed port?
An exposed port is basically your docker image saying: "These ports are available for outside communication. But you have to publish them first."
Note: outside means outside its own network. If you link containers together, they can communicate through the exposed ports.
Where are exposed ports defined?
The exposed ports you are seeing are defined in the Dockerfile
of the image you are running ( or in one of the parents ). These are defined with the EXPOSE <portnumber>
syntax.
You can also define additional exposed ports when you start a container, which you're doing in the expose_ports
section of your docker-compose.yml
file.
How do you make exposed ports accessible
To access an exposed port, you have to publish it, then it will be available on the host. To publish an exposed port, you specify these ports in the ports
part of your docker-compose.yml
file, or with the -p
argument when starting the container ( e.g. -p 22
to expose it to a random port, -p 22:2222
to expose it to port 2222
on the host, etc ).
Further reading on EXPOSE vs publish.