build(docker): optimize Dockerfile and setup vscode devcontainer with all required dependencies

- fix: set mediaBaseURL as baseURL if not defined
- update dev documentation seting up a dev environment
This commit is contained in:
Yassine Doghri 2021-05-24 10:08:15 +00:00
parent a162d6d08f
commit 88c52db53c
No known key found for this signature in database
GPG Key ID: 3E7F89498B960C9F
7 changed files with 122 additions and 63 deletions

View File

@ -2,10 +2,13 @@
// https://github.com/microsoft/vscode-dev-containers/tree/v0.117.1/containers/docker-existing-dockerfile // https://github.com/microsoft/vscode-dev-containers/tree/v0.117.1/containers/docker-existing-dockerfile
{ {
"name": "Castopod Host dev", "name": "Castopod Host dev",
"dockerComposeFile": ["../docker-compose.yml"], "dockerComposeFile": ["../docker-compose.yml", "./docker-compose.yml"],
"service": "app", "service": "app",
"workspaceFolder": "/castopod-host", "workspaceFolder": "/castopod-host",
"postCreateCommand": "cron && php spark serve --host 0.0.0.0", "postCreateCommand": "composer install && npm install && npm run build:dev",
"postStartCommand": "crontab ./crontab && cron && php spark serve --host 0.0.0.0",
"postAttachCommand": "crontab ./crontab && service cron reload",
"shutdownAction": "stopCompose",
"settings": { "settings": {
"terminal.integrated.defaultProfile.linux": "/bin/bash", "terminal.integrated.defaultProfile.linux": "/bin/bash",
"editor.formatOnSave": true, "editor.formatOnSave": true,

View File

@ -0,0 +1,10 @@
version: "3"
services:
app:
volumes:
# Mounts the project folder to '/workspace'. While this file is in .devcontainer,
# mounts are relative to the first file in the list, which is a level up.
- .:/castopod-host:cached
# Overrides default command so things don't shut down after the process ends.
command: /bin/sh -c "while sleep 1000; do :; done"

View File

@ -1,10 +1,9 @@
#################################################### ####################################################
# Castopod Host development Docker file # Castopod Host development Docker file
#################################################### ####################################################
# NOT optimized for production # ⚠️ NOT optimized for production
# should be used only for development purposes # should be used only for development purposes
#################################################### #---------------------------------------------------
FROM php:8.0-fpm FROM php:8.0-fpm
LABEL maintainer="Yassine Doghri <yassine@doghri.fr>" LABEL maintainer="Yassine Doghri <yassine@doghri.fr>"
@ -13,50 +12,51 @@ COPY . /castopod-host
WORKDIR /castopod-host WORKDIR /castopod-host
# Install composer # Install composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
# Install latest npm # Install server requirements
RUN apt-get update && \ RUN apt-get update \
apt-get install -y --no-install-recommends gnupg && \ # gnupg to sign commits with gpg
curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - && \ && apt-get install --yes --no-install-recommends gnupg \
apt-get update && \ # npm through the nodejs package
apt-get install -y --no-install-recommends nodejs && \ && curl -fsSL https://deb.nodesource.com/setup_14.x | bash - \
npm install --global npm && apt-get update \
&& apt-get install --yes --no-install-recommends nodejs \
# Install git + vim # update npm
RUN apt-get update && \ && npm install --global npm@7 \
apt-get upgrade -y && \ && apt-get update \
apt-get install -y git vim && apt-get install --yes --no-install-recommends \
git \
### Install CodeIgniter's server requirements openssh-client \
#-- https://github.com/codeigniter4/appstarter#server-requirements vim \
# cron for scheduled tasks
# Install intl extension using https://github.com/mlocati/docker-php-extension-installer cron \
RUN apt-get update && apt-get install -y \ # unzip used by composer
unzip \
# required libraries to install php extensions using
# https://github.com/mlocati/docker-php-extension-installer (included in php's docker image)
libicu-dev \ libicu-dev \
libpng-dev \ libpng-dev \
libjpeg-dev \ libjpeg-dev \
zlib1g-dev \ zlib1g-dev \
&& docker-php-ext-install intl libzip-dev \
# intl for Internationalization
RUN docker-php-ext-configure gd --with-jpeg \ && docker-php-ext-install intl \
&& docker-php-ext-install gd && docker-php-ext-install zip \
# gd for image processing
RUN pecl install -o -f redis \ && docker-php-ext-configure gd --with-jpeg \
&& rm -rf /tmp/pear \ && docker-php-ext-install gd \
&& docker-php-ext-enable redis # redis extension for cache
&& pecl install -o -f redis \
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli && rm -rf /tmp/pear \
&& docker-php-ext-enable redis \
RUN echo "file_uploads = On\n" \ # mysqli for database access
&& docker-php-ext-install mysqli \
&& docker-php-ext-enable mysqli \
# configure php
&& echo "file_uploads = On\n" \
"memory_limit = 512M\n" \ "memory_limit = 512M\n" \
"upload_max_filesize = 500M\n" \ "upload_max_filesize = 500M\n" \
"post_max_size = 512M\n" \ "post_max_size = 512M\n" \
"max_execution_time = 300\n" \ "max_execution_time = 300\n" \
> /usr/local/etc/php/conf.d/uploads.ini > /usr/local/etc/php/conf.d/uploads.ini \
# install cron
RUN apt-get update && \
apt-get install -y cron
RUN crontab /castopod-host/crontab

View File

@ -163,7 +163,7 @@ class InstallController extends Controller
self::writeEnv([ self::writeEnv([
'app.baseURL' => $baseUrl, 'app.baseURL' => $baseUrl,
'app.mediaBaseURL' => 'app.mediaBaseURL' =>
$mediaBaseUrl === null ? $baseUrl : $mediaBaseUrl, $mediaBaseUrl === '' ? $baseUrl : $mediaBaseUrl,
'app.adminGateway' => $this->request->getPost('admin_gateway'), 'app.adminGateway' => $this->request->getPost('admin_gateway'),
'app.authGateway' => $this->request->getPost('auth_gateway'), 'app.authGateway' => $this->request->getPost('auth_gateway'),
]); ]);

View File

@ -117,10 +117,12 @@ if (! function_exists('media_base_url')) {
} }
$uri = trim($uri, '/'); $uri = trim($uri, '/');
return rtrim(config('App')->mediaBaseURL, '/') . $appConfig = config('App');
$mediaBaseUrl = $appConfig->mediaBaseURL === '' ? $appConfig->baseURL : $appConfig->mediaBaseURL;
return rtrim($mediaBaseUrl, '/') .
'/' . '/' .
config('App') $appConfig->mediaRoot .
->mediaRoot .
'/' . '/' .
$uri; $uri;
} }

View File

@ -8,9 +8,8 @@ services:
build: build:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
container_name: castopod_host container_name: castopod-host
command: > command: /bin/sh -c "crontab ./crontab && cron && service cron reload && php spark serve --host 0.0.0.0"
sh -c "cron && php spark serve --host 0.0.0.0"
ports: ports:
- 8080:8080 - 8080:8080
volumes: volumes:
@ -23,7 +22,7 @@ services:
redis: redis:
image: redis:alpine image: redis:alpine
container_name: castopod_host_redis container_name: castopod-host_redis
ports: ports:
- 6379:6379 - 6379:6379
volumes: volumes:
@ -33,7 +32,7 @@ services:
mariadb: mariadb:
image: mariadb:latest image: mariadb:latest
container_name: castopod_host_mariadb container_name: castopod-host_mariadb
ports: ports:
- 3306:3306 - 3306:3306
volumes: volumes:
@ -48,7 +47,7 @@ services:
phpmyadmin: phpmyadmin:
image: phpmyadmin/phpmyadmin:latest image: phpmyadmin/phpmyadmin:latest
container_name: castopod_host_phpmyadmin container_name: castopod-host_phpmyadmin
environment: environment:
PMA_HOST: mariadb PMA_HOST: mariadb
PMA_PORT: 3306 PMA_PORT: 3306

View File

@ -12,6 +12,8 @@
- [Going Further](#going-further) - [Going Further](#going-further)
- [Useful docker / docker-compose commands](#useful-docker--docker-compose-commands) - [Useful docker / docker-compose commands](#useful-docker--docker-compose-commands)
- [Known issues](#known-issues) - [Known issues](#known-issues)
- [Allocation failed - JavaScript heap out of memory](#allocation-failed---javascript-heap-out-of-memory)
- [Files created inside container are attributed to root locally (Linux)](#files-created-inside-container-are-attributed-to-root-locally-linux)
## Introduction ## Introduction
@ -74,7 +76,7 @@ to help you kickstart your contribution.
3. (for docker desktop) Add the repository you've cloned to docker desktop's 3. (for docker desktop) Add the repository you've cloned to docker desktop's
`Settings` > `Resources` > `File Sharing` `Settings` > `Resources` > `File Sharing`
### (recommended) Develop inside the app Container with VSCode ## (recommended) Develop inside the app Container with VSCode
If you're working in VSCode, you can take advantage of the `.devcontainer/` If you're working in VSCode, you can take advantage of the `.devcontainer/`
folder. It defines a development environment (dev container) with preinstalled folder. It defines a development environment (dev container) with preinstalled
@ -110,7 +112,7 @@ required services will be loaded automagically!
For more info, see For more info, see
[VSCode Remote Containers](https://code.visualstudio.com/docs/remote/containers) [VSCode Remote Containers](https://code.visualstudio.com/docs/remote/containers)
### (not-recommended) Develop outside the app container ## (not-recommended) Develop outside the app container
You do not wish to use the VSCode devcontainer? No problem! You do not wish to use the VSCode devcontainer? No problem!
@ -134,13 +136,13 @@ You do not wish to use the VSCode devcontainer? No problem!
> The `docker-compose up -d` command will boot 4 containers in the > The `docker-compose up -d` command will boot 4 containers in the
> background: > background:
> >
> - `castopod_host_app`: a php based container with CodeIgniter4 requirements > - `castopod-host_app`: a php based container with CodeIgniter4 requirements
> installed > installed
> - `castopod_host_redis`: a [redis](https://redis.io/) database to handle > - `castopod-host_redis`: a [redis](https://redis.io/) database to handle
> queries and pages caching > queries and pages caching
> - `castopod_host_mariadb`: a [mariadb](https://mariadb.org/) server for > - `castopod-host_mariadb`: a [mariadb](https://mariadb.org/) server for
> persistent data > persistent data
> - `castopod_host_phpmyadmin`: a phpmyadmin server to visualize the mariadb > - `castopod-host_phpmyadmin`: a phpmyadmin server to visualize the mariadb
> database. > database.
2. Run any command by prefixing them with `docker-compose run --rm app`: 2. Run any command by prefixing them with `docker-compose run --rm app`:
@ -292,7 +294,7 @@ To see your changes, go to:
docker-compose logs --tail 50 --follow --timestamps app docker-compose logs --tail 50 --follow --timestamps app
# interact with redis server using included redis-cli command # interact with redis server using included redis-cli command
docker exec -it castopod_host_redis redis-cli docker exec -it castopod-host_redis redis-cli
# monitor the redis container # monitor the redis container
docker-compose logs --tail 50 --follow --timestamps redis docker-compose logs --tail 50 --follow --timestamps redis
@ -319,7 +321,50 @@ more insights.
## Known issues ## Known issues
- `Allocation failed - JavaScript heap out of memory` when running `npm install` ### Allocation failed - JavaScript heap out of memory
👉 By default, docker might not have access to enough RAM. Allocate more This happens when running `npm install`.
memory and run `npm install` again.
👉 By default, docker might not have access to enough RAM. Allocate more memory
and run `npm install` again.
### Files created inside container are attributed to root locally (Linux)
You may use Linux user namespaces to fix this:
> **Note:**
>
> Replace "username" with your local username
1. Go to `/etc/docker/daemon.json` and add:
```json
{
"userns-remap": "username"
}
```
2. Configure the subordinate uid/guid:
```bash
username:1000:1
username:100000:65536
```
```bash
username:1000:1
username:100000:65536
```
3. Restart docker:
```bash
sudo systemctl restart docker
```
4. That's it! Now, the root user in the container will be mapped to the user on
your local machine, no more permission problems! 🎉
You can check
[this great article](https://www.jujens.eu/posts/en/2017/Jul/02/docker-userns-remap/)
to know more about how it works.