[DOCKER] Remote Container access to localhost returns ERR_EMPTY_RESPONSE

When I used php-apache image in VSCode's Remote Container, the access to localhost returned ERR_EMPTY_RESPONSE. I also reviewed the settings such as the port (ERR_EMPTY_RESPONSE appears when accessing the application on docker with localhost), but I was worried without any problems.

environment

Cause

The Apache service was not started. When I checked the start of the service with the following command in the container, I found that apache2 was not started.

$ service --status-all
 [ - ]  apache-htcacheclean
 [ - ]  apache2
 [ ? ]  hwclock.sh
 [ - ]  procps

When I did docker run from WSL, it started automatically, so I thought it was also started in Remote Container.

Solution

It is necessary to explicitly start the Apache service after starting the container with devcontainer.json.

devcontainer.json


{
	"build": {
		"dockerfile": "Dockerfile"
	},
	"mounts": [
		"source=${localWorkspaceFolder}/public,target=/var/www/html,type=bind,consistency=cached"
	],
	"forwardPorts": [80],
+	"postStartCommand": "service apache2 start"
}

The above is a configuration using Dockerfile, but in the case of Docker Compose, there is a required service property, so you can use it. By the way, the Dockerfile looks like this:

FROM php:7.4.7-apache

EXPOSE 80

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
      git \
      vim \
      unzip \
      libonig-dev \
      libzip-dev \
      libfreetype6-dev \
      libjpeg62-turbo-dev \
      libpng-dev \
      libwebp-dev && \
    docker-php-ext-configure gd --with-jpeg --with-webp --with-freetype && \
    docker-php-ext-install -j$(nproc) \
      mbstring \
      zip \
      gd \
      exif && \
    # reduce image size
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

COPY --from=composer /usr/bin/composer /usr/bin/composer

COPY --from=node /usr/local/bin/ /usr/local/bin/
COPY --from=node /usr/local/lib/node_modules /usr/local/lib/node_modules

RUN a2enmod rewrite headers

reference

Recommended Posts

Remote Container access to localhost returns ERR_EMPTY_RESPONSE
I wanted to develop PHP with vscode remote container
Ssh connect from container to git with VSCode Remote Container
[Docker] How to access the host from inside the container. http://host.docker.internal: