4

I use nginx inside a docker container and I'm unable to connect to it properly.

The error inside the browser is:

The connection was reset

if I try to curl inside the container I get:

/usr/share/nginx/html # curl localhost
curl: (7) Failed to connect to localhost port 80: Connection refused

My Dockerfile:

FROM nginx:alpine

COPY ./default.conf /etc/nginx/conf.d/default.conf

RUN chmod -R a+w /var/cache/nginx/ \
        && touch /var/run/nginx.pid \
        && chmod a+w /var/run/nginx.pid

WORKDIR /usr/share/nginx/html

COPY . .

EXPOSE 80

My command:

docker run -d -p 80:80 <mycontainer>

My default.conf:

server {
  listen [::]:80;
  sendfile on;
  default_type application/octet-stream;

  gzip on;
  gzip_http_version 1.1;
  gzip_disable      "MSIE [1-6]\.";
  gzip_min_length   256;
  gzip_vary         on;
  gzip_proxied      expired no-cache no-store private auth;
  gzip_types        text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  gzip_comp_level   9;

  root /usr/share/nginx/html;

  location / {
    try_files $uri$args $uri$args/ /pointeuse/index.html;
  }
}

This is my container inspect:

 [
    {
        "Id": "sha256:b431c5a40e599a69a31f482d52ba3a971953bece09d7de1a83d3ff78bd08e79b",
        "RepoTags": [
            "test:latest"
        ],
        "RepoDigests": [],
        "Parent": "sha256:52a9dfb246091e9a6fedc6e7bdc7c701dd39638bd3889ae098bb97d519889c2f",
        "Comment": "",
        "Created": "2020-05-23T12:27:30.123757764Z",
        "Container": "70cfa14de670aaf2c599385b1afbf5d5f4a155ce2e655031b1adcaa3654e5442",
        "ContainerConfig": {
            "Hostname": "70cfa14de670",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.17.10",
                "NJS_VERSION=0.3.9",
                "PKG_RELEASE=1"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "EXPOSE 80"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:52a9dfb246091e9a6fedc6e7bdc7c701dd39638bd3889ae098bb97d519889c2f",
            "Volumes": null,
            "WorkingDir": "/usr/share/nginx/html",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "DockerVersion": "19.03.9",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.17.10",
                "NJS_VERSION=0.3.9",
                "PKG_RELEASE=1"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:52a9dfb246091e9a6fedc6e7bdc7c701dd39638bd3889ae098bb97d519889c2f",
            "Volumes": null,
            "WorkingDir": "/usr/share/nginx/html",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 31180356,
        "VirtualSize": 31180356,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/462dd80fe58a733771808738b12b51fc08995c7a7f7b58c240404067e742fb13/diff:/var/lib/docker/overlay2/d91f7871375c57d25380a3f72afbe453cc3044ae6648037d7810c771ff34b873/diff:/var/lib/docker/overlay2/1b7177dbb104d4ad8da12fe2c0bf11e62d7c5a4e731572bf2677c3c0e576087f/diff:/var/lib/docker/overlay2/7f555e278efd118a189669ebf977d14e3ee0a9b1ac2440191bbcf77ceb693378/diff",
                "MergedDir": "/var/lib/docker/overlay2/72bac5710e111f45c9ef6cd0cddfae079c91bb007d9fa3fe79eda283f2bbf318/merged",
                "UpperDir": "/var/lib/docker/overlay2/72bac5710e111f45c9ef6cd0cddfae079c91bb007d9fa3fe79eda283f2bbf318/diff",
                "WorkDir": "/var/lib/docker/overlay2/72bac5710e111f45c9ef6cd0cddfae079c91bb007d9fa3fe79eda283f2bbf318/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:3e207b409db364b595ba862cdc12be96dcdad8e36c59a03b7b3b61c946a5741a",
                "sha256:3810cc0c140fd3afda75475ae08152cd1a6643962a18cc51a2de1c061f01ed7b",
                "sha256:9bb57ece8be2135a6f230901f9a66353f9d0f9134b36f85570b93bd812c9938b",
                "sha256:b3fc9f22b63668bb906ccefb28113ec4b7aab6cbdd569c3cbccfbcd9f49a6ca1",
                "sha256:9f2edf038c0c3a17430487323ef682150a2c7a4aade35f30b42b76f84b88ebdc"
            ]
        },
        "Metadata": {
            "LastTagTime": "2020-05-23T14:27:30.168712086+02:00"
        }
    }
]
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Olivier D'Ancona
  • 779
  • 2
  • 14
  • 30

2 Answers2

1

You are unable to reach your nginx container because you try to connect to it with ipv4 and in your configuration file you use an ipv6 syntax.

Change to listen 80.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Olivier D'Ancona
  • 779
  • 2
  • 14
  • 30
-1

Your dockerfile doesn't start the nginx service. You need to run nginx -g daemon off; Use this Dockerfile instead:

FROM nginx:alpine

COPY ./default.conf /etc/nginx/conf.d/default.conf

RUN chmod -R a+w /var/cache/nginx/ \
        && touch /var/run/nginx.pid \
        && chmod a+w /var/run/nginx.pid

WORKDIR /usr/share/nginx/html

COPY . .

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
Raj Srujan Jalem
  • 613
  • 5
  • 17
  • Hello, indeed the nginx daemon is running in my container because this line is already specified into the nginx:alpine image as you can see on the official nginx:alpine image on github. https://github.com/nginxinc/docker-nginx/blob/594ce7a8bc26c85af88495ac94d5cd0096b306f7/mainline/alpine/Dockerfile Furthermore, you can find this command into the inspect logs... In the other hand, I noticed in the same time that it was overkill to EXPOSE the port 80 twice. I removed the EXPOSE line and the behavior didn't changed as well. Thank you for your reply. – Olivier D'Ancona May 23 '20 at 21:27