-4

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

  • Check your firewall settings – Jenny D May 21 '18 at 12:48
  • first check container is in running state "docker ps " , if yes get shell of container using command "docker exec -it container_name bash" and check apache service is running or not. – Sunil Bhoi May 21 '18 at 12:57
0

[root@ansiblecontrolnode dockerbuild]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpimage           latest              0f104cab653d        2 minutes ago       580MB
centos              latest              e934aafc2206        6 weeks ago         199MB

[root@ansiblecontrolnode dockerbuild]# docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

[root@ansiblecontrolnode dockerbuild]# docker run -tid --name httpcontainer 0f104cab653d

bfe98153a77a9bd84e9a04aebaf19b0ef65dd4ddc33dce7b01ad5c8045291f37

[root@ansiblecontrolnode dockerbuild]# docker inspect httpcontainer | grep "IPAddress"

        "SecondaryIPAddresses": null,
        "IPAddress": "172.17.0.2",
                "IPAddress": "172.17.0.2",

[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

[root@ansiblecontrolnode dockerbuild]# curl http://172.17.0.2

Hello Apache server on CentOS Docker

Question - How this is working now without "-p" or "-P" ?

  • This doesn't really answer the question, you should edit this into your question instead of posting it as an answer. But seeing this I think I know now what the actual problem was. Please see my new answer. – Gerald Schneider May 22 '18 at 12:02
  • This was my 1st post. I am new to this editing tool. Will try to improve on editing. Thanks! – Dhirendra Rai May 22 '18 at 12:29
0

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.

  • Thank you Gerald....!! I really appreciate your help and for you time!! – Dhirendra Rai May 22 '18 at 12:14

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.