Docker 文件内容 -
[root@ansiblecontrolnode dockerbuild]# cat Dockerfile
FROM centos:latest
MAINTAINER [email protected]
RUN yum update -y && yum install httpd net-tools -y
CMD ["apachectl","-D","FOREGROUND"]
EXPOSE 80
Ansible playbook - 创建新的 docker 镜像 -
[root@ansiblecontrolnode dockerbuild]# cat build_docker_image.yml
---
- name: Build a docker image
hosts: localhost
gather_facts: no
tasks:
- name: Build a CENTOS (latest) docker image with fully updated and Webservice installed
docker_image:
path: /root/ANSIBLE/ANSIBLEDOCKER/dockerbuild/
name: docker-image-created-by-ansible
tag: ansibleexample
...
检查语法
[root@ansiblecontrolnode dockerbuild]# ansible-playbook
build_docker_image.yml --syntax-check
playbook: build_docker_image.yml
使用安装了 http 的 ansible 构建 Docker 镜像 -
[root@ansiblecontrolnode dockerbuild]# ansible-playbook build_docker_image.yml
构建后 - 检查图像
[root@ansiblecontrolnode dockerbuild]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-image-created-by-ansible ansibleexample 622e72211b67 35 minutes ago 449MB
centos latest e934aafc2206 6 weeks ago 199MB
从 622e72211b67 图像中旋转第一个容器 -
[root@ansiblecontrolnode dockerbuild]# docker run -it --name httpimage 622e72211b67 bash
容器运行状态(暴露80端口)-
[root@ansiblecontrolnode dockerbuild]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be452b97dda0 622e72211b67 "bash" 7 minutes ago Up 7 minutes 80/tcp httpimage
获取容器 IP 地址 -
[root@ansiblecontrolnode dockerbuild]# docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' httpimage
172.17.0.2
[root@ansiblecontrolnode dockerbuild]# ping -c2 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.107 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.058 ms
--- 172.17.0.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.058/0.082/0.107/0.026 ms
[root@ansiblecontrolnode dockerbuild]# telnet 172.17.0.2 80
Trying 172.17.0.2...
telnet: connect to address 172.17.0.2: Connection refused
[root@ansiblecontrolnode dockerbuild]# curl http://172.17.0.2:80
curl: (7) Failed connect to 172.17.0.2:80; Connection refused
请指出这里出了什么问题?
谢谢,Dhirendra
答案1
请注意第一个输出之间的区别docker ps
:
[root@ansiblecontrolnode dockerbuild]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be452b97dda0 622e72211b67 "bash" 7 minutes ago Up 7 minutes 80/tcp httpimage
同一命令的第二个输出为:
[root@ansiblecontrolnode dockerbuild]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bfe98153a77a 0f104cab653d "apachectl -D FOREGR…" 41 seconds ago Up 40 seconds 80/tcp httpcontainer
首次尝试时,您使用以下命令启动容器:
docker run -it --name httpimage 622e72211b67 bash
第二次你使用这个命令:
docker run -tid --name httpcontainer 0f104cab653d
结果:第一次运行时,您启动的是 bash,而不是 apache。容器正在运行,但 apache 未运行,因此Connection refused
当您尝试使用 curl 访问它时会出错。