Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to build with Devcontainer #88

Merged
merged 9 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
FROM ubuntu:20.04

ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID

ENV DEBIAN_FRONTEND=noninteractive

ENV NODE_VERSION 16.13.0
ENV NVM_DIR /usr/local/nvm
ENV PATH $NVM_DIR/versions/node/$NODE_VERSION/bin:$PATH
RUN mkdir -p $NVM_DIR

# Install dependencies
RUN apt-get -y update --no-install-recommends \
&& apt-get -y install --no-install-recommends \
build-essential \
curl \
ca-certificates \
apt-utils \
dialog \
git \
openssh-server \
&& apt-get autoremove -y \
&& apt-get clean -y

# PHP
RUN apt-get -y update && apt-get -y upgrade \
&& apt-get -y install software-properties-common \
&& add-apt-repository ppa:ondrej/php \
&& apt-get -y update \
&& apt-get -y install php8.1-gd php8.1-xml php8.1-soap php8.1-mbstring php8.1-mysql php8.1-pgsql php8.1-dev php8.1-zip php8.1-curl php8.1-xdebug

# XDEBUG Setup
RUN echo "xdebug.mode = develop,debug" >> /etc/php/8.1/cli/conf.d/20-xdebug.ini \
&& echo "xdebug.start_with_request = yes" >> /etc/php/8.1/cli/conf.d/20-xdebug.ini \
&& echo "xdebug.client_host = localhost" >> /etc/php/8.1/cli/conf.d/20-xdebug.ini \
&& echo "xdebug.discover_client_host=1" >> /etc/php/8.1/cli/conf.d/20-xdebug.ini \
&& echo "xdebug.client_port = 9003" >> /etc/php/8.1/cli/conf.d/20-xdebug.ini \
&& echo "xdebug.log = /var/log/xdebug.log" >> /etc/php/8.1/cli/conf.d/20-xdebug.ini

RUN touch /var/log/xdebug.log

# Yarn
RUN curl https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y yarn

# Composer
RUN cd ~
RUN curl -sS https://getcomposer.org/installer -o composer-setup.php \
&& HASH=`curl -sS https://composer.github.io/installer.sig` \
&& php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
&& php composer-setup.php --install-dir=/usr/local/bin --filename=composer

# Nvm & Node
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
RUN /bin/bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION"
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

# Set User and Group
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME

# Set Xdebug logging file
RUN chown $USER_UID:$USER_GID /var/log/xdebug.log

ENV DEBIAN_FRONTEND=dialog

USER $USERNAME
116 changes: 116 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Dev Container


## Running Dev Container

If you need/prefer the option you can build and use this template within a Dev Container, which you can run locally or with GitHub Codespaces.

To build and run locally you must first have docker and docker compose installed for your OS.

Once you have those installed you can click on the prompt:

![image](https://user-images.githubusercontent.com/25044744/184038681-593065e5-f4d5-4aa2-bbe4-3da157a2c39d.png)

Or by using the option in VSCode:

![image](https://user-images.githubusercontent.com/25044744/184038895-fa330419-29bb-4a01-a58e-c4ea70503910.png)
![image](https://user-images.githubusercontent.com/25044744/184038953-000377c6-627e-4c5a-b9fd-f46a49325b95.png)


## Configuring Dev Container

After you click to run the app within a dev container you need to make sure your application `.env` is setup to use the proper configuration.

This means updating the following:

1. Database Connection
2. Mailer

> The DB connection your `.env` should match the following:

```
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laranuxt
DB_USERNAME=root
DB_PASSWORD=password
```

> And if you are using Mailhog as a development mail server:

```
MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
```

These `.env` entires are important to update since the HOST must match what was configured in the dockerize'd environment. Otherwise your database and mail server will not get connected.


## Dev Container Extras

To use tools like Spatie Ray you need to configure the dev container to do so.

First copy the `ray.php` file and add it to the root of this apps directory.

```bash
cp .devcontainer/ray.php ray.php
```

Next within your `.env` add the following entires:

> Keep in mind the value `RAY_LOCAL_PATH` should be set to your machines full path to the app.
```
RAY_HOST=host.docker.internal
RAY_REMOTE_PATH=/workspace

# Set this to your working directory
RAY_LOCAL_PATH=/path/to/working/folder
```

## Additional configuration

If you need to change any of the ports the dev container needs to map to you can add the following to your `.env`

```
DOCKER_PORT_MYSQL
DOCKER_PORT_MAILHOG_1
DOCKER_PORT_MAILHOG_2
```

Then set the value equal to the port you want it to run at.

For example if the mysql port of 3306 will not work for your locally, maybe you have another instance running.

You may set it to the value you want:

> To run instead at port 13306

```
DOCKER_PORT_MYSQL=13306
```

## After Dev Container Build

Once the Dev Container successfully built you can run your typically `artisan`/`composer`/`yarn` commands.

We suggest after everything is built opening up 2 integrated terminals.

> To migrate and seed container database & To run API server
1. One to run `yarn seed` && `yarn api`

> To run Nuxt server
2. `yarn dev`


But before both if this is the first time building the app you would still need to:

```bash
composer install && yarn install
```
21 changes: 21 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "Laranuxt VSCode Container",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspace",
"remoteEnv": {
"LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}"
},
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
"extensions": [
"editorconfig.editorconfig",
],
"forwardPorts": [
8000,
3000,
9003
],
"remoteUser": "vscode"
}
43 changes: 43 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: "3"

x-environment: &default-environment
MYSQL_ROOT_PASSWORD: "password"
MYSQL_DATABSE: "laranuxt"
services:
mysql:
container_name: mysql_container_laranuxt
image: mysql
platform: linux/x86_64
restart: unless-stopped
ports:
- "${DOCKER_PORT_MYSQL:-3306}:3306"
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD:-password}'
MYSQL_DATABASE: '${DB_DATABASE:-laranuxt}'
volumes:
- mysql-data:/var/lib/mysql
mailhog:
image: mailhog/mailhog:latest
container_name: mailhog_container_punchlist
ports:
- "${DOCKER_PORT_MAILHOG_1:-1025}:1025"
- "${DOCKER_PORT_MAILHOG_2:-8025}:8025"
app:
container_name: app_container_laranuxt
build:
context: ..
dockerfile: .devcontainer/Dockerfile
environment:
<<: *default-environment
PORTS:
3000
8000
9003
volumes:
- ..:/workspace
user: vscode
command: sleep infinity
extra_hosts:
- "host.docker.internal:host-gateway"
volumes:
mysql-data:
46 changes: 46 additions & 0 deletions .devcontainer/ray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

return [
/**
* This settings controls whether data should be sent to Ray.
*
* By default, `ray()` will only transmit data in non-production environments.
*/
'enable' => env('RAY_ENABLED', true),

/**
* When enabled, all things logged to the application log
* will be sent to Ray as well.
*/
'send_log_calls_to_ray' => env('SEND_LOG_CALLS_TO_RAY', true),

/**
* When enabled, all things passed to `dump` or `dd`
* will be sent to Ray as well.
*/
'send_dumps_to_ray' => env('SEND_DUMPS_TO_RAY', true),

/**
* The host used to communicate with the Ray app.
* For usage in Docker on Mac or Windows, you can replace host with 'host.docker.internal'
* For usage in Homestead on Mac or Windows, you can replace host with '10.0.2.2'
*/
'host' => env('RAY_HOST', 'localhost'),

/**
* The port number used to communicate with the Ray app.
*/
'port' => env('RAY_PORT', 23517),

/**
* Absolute base path for your sites or projects in Homestead,
* Vagrant, Docker, or another remote development server.
*/
'remote_path' => env('RAY_REMOTE_PATH', null),

/**
* Absolute base path for your sites or projects on your local
* computer where your IDE or code editor is running on.
*/
'local_path' => env('RAY_LOCAL_PATH', null),
];