Docker file content –
[root@ansiblecontrolnode dockerbuild]# cat Dockerfile
FROM centos:latest MAINTAINER dhirendra120285.rai@gmail.com RUN yum update -y && yum install httpd net-tools -y CMD ["apachectl","-D","FOREGROUND"] EXPOSE 80
Ansible playbook – To create a new docker image –
[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 ...
Checking syntax
[root@ansiblecontrolnode dockerbuild]# ansible-playbook build_docker_image.yml --syntax-check playbook: build_docker_image.yml
Building Docker image using ansible with http installed –
[root@ansiblecontrolnode dockerbuild]# ansible-playbook build_docker_image.yml
Post build – checking images
[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
Spinning-up 1st container from 622e72211b67 image –
[root@ansiblecontrolnode dockerbuild]# docker run -it --name httpimage 622e72211b67 bash
Container run status (with port 80 exposed) –
[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
Getting container IP address –
[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
Please suggest what is wrong here?
Thanks,
Dhirendra
Answer
Notice the difference between your first output of 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
And your second output of the same command:
[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
On you first attempt you were starting the container with this command:
docker run -it --name httpimage 622e72211b67 bash
On the second you were using this command:
docker run -tid --name httpcontainer 0f104cab653d
The result: On the first run you were starting bash instead of apache. The container was running, but apache wasn’t running, so you got Connection refused
when you tried to access it with curl.
Attribution
Source : Link , Question Author : Dhirendra Rai , Answer Author : Gerald Schneider