FROM node:alpine as builder
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build
FROM nginx
ENV PORT 3000
EXPOSE $PORT
COPY nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/build /usr/share/nginx/html
# docker-compose file for development
version: '3'
services:
app:
build:
context: .
ports:
- '3000:3000'
volumes:
- ./:/app
command: npm run start:dev
env_file:
- .env
Dockerfile.dev
# Development docker file for Node.js
FROM node:10-alpine
COPY package.json .
RUN npm install
RUN mkdir /app
WORKDIR /app
COPY ./ /app
RUN adduser -D user
USER user
# Docker and XDebug configuration
## PHP Dockerfile
Below is the PHP Dockerfile with the XDebug configuration.
The `host.docker.internal` pseudo domain is pointing back to the docker host. Do not use directly the ip address because the host ip address is changing and it wont work in all the cases.
The default xdebug port `9000` is changed to `9001` to avoid conflicts with `php-fpm`.
```
# Install xdebug
RUN pecl install xdebug \
&& docker-php-ext-enable xdebug \
&& echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_autostart=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_connect_back=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_log=/tmp/xdebug.log" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
\!h && echo "xdebug.remote_port=9001" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
\!h && echo "xdebug.remote_host=host.docker.internal">> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.profiler_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.profiler_output_dir=/tmp/snapshots" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
\!h && echo "xdebug.idekey=VSCODE" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.profiler_enable_trigger=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
```
## docker-composer.yml
Because we need our application to connect back to the docker host we do
not forward the `9001` port. Otherwise the container will connect back to itself.
The `${PROJECT_PATH}` should be set in the `.env` file inside your docker-compose project.
```
php:
environment:
- ENVIROMENT=dev
- XDEBUG_CONFIG="idekey=VSCODE"
build: "./containers/php"
hostname: "phpxdebug"
container_name: "phpxdebug"
volumes:
\!h - "${PROJECT_PATH}:/var/www/html"
ports:
- "9000:9000"
```
## VS Code config
Below is the __VS Code__. The default __XDebug__ port should be replaced with the
`9001` so that we avoid that `php-fpm` and `xdebug` are having confilicts on the same port.
```
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
\!h "port": 9001,
"log": true,
\!h "pathMappings": {
\!h "/var/www/html": "${workspaceRoot}"
\!h }
}
```
### Debug
In case that something is not working you check the `/tmp/xdebug.log` path on the container. Be sure that xdebug has write permissions on the file.