Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Docker 如何获取容器ID&;IP从那时起是ansible yaml吗?_Docker_Ansible - Fatal编程技术网

Docker 如何获取容器ID&;IP从那时起是ansible yaml吗?

Docker 如何获取容器ID&;IP从那时起是ansible yaml吗?,docker,ansible,Docker,Ansible,在使用ansible下载和启动服务时,是否有更好的方法获取IP和容器ID 我不知道id也不知道IP在启动前。。。所以我从寄存器变量中得到这些信息 目前,我基于该方法: - name: start docker service service: name: docker1 state: started - name: load and start the container we wanna use docker_container:

在使用ansible下载和启动服务时,是否有更好的方法获取IP和容器ID

我不知道id也不知道IP在启动前。。。所以我从寄存器变量中得到这些信息

目前,我基于该方法:

  - name: start docker service 
    service: 
      name: docker1
      state: started
  - name: load and start the container we wanna use 
    docker_container:
      name: test
      image: rastasheep/ubuntu-sshd
      state: started
      ports:
       - "49154:22"
  - name : get container ID
    shell: docker ps | grep -i ubuntu-sshd | awk '{print $1}'
    register : container_ID
  - name : get container IP
    shell: docker inspect --format '{''{ .NetworkSettings.IPAddress }''}' container_ID
    register : container_IP
  - name: Wait maximum of 300 seconds for ports to be available 
    wait_for:
      host: 0.0.0.0
      port: 49154
      state: started
  - name: force docker to accept sshkey
    shell: sshpass -p root scp /root/.ssh/id_rsa.pub root@container_IP:/tmp/
这真的是一个好方法吗? 如果不是,我该怎么做?(获取IP&ID以在执行此步骤后将模块运行到容器)


谢谢。

您传递给
docker\u容器的
名称:
块可用于所有目的对容器进行寻址;您永远不需要十六进制容器ID


容器内部IP地址基本上是无用的,我将删除查找它的块。由于您在主机上发布了端口49154,因此可以通过
{{ansible\u hostname}}:49154:49154

访问该服务,因为问题中的代码是有效的,所以我投票将此问题作为离题题来结束。应该发布到per ok谢谢,只要我用变量{{ansible_hostname}}:49154调用服务,我就做得很好了。。。