LoginSignup
3
3

More than 3 years have passed since last update.

VSCode Remote-Containers で Laravel7 開発環境 ( CentOS8+PHP7.4+MongoDB4.4 ) を爆速で準備する

Last updated at Posted at 2020-08-11

はじめに

Docker で開発環境コンテナをサクッと準備。実際の開発は VSCode + Remote-Containers ( 拡張機能 ) を使ってコンテナ内で行うための手順をまとめています。記事内容に間違いなどありましたら、コメント欄にてご指摘お願いいたします。
まだ色々と制約はありますが、とりあえずコンテナを立ち上げるところまでは上手くいったので公開します。

前提条件

  • Docker ( Windows/MacOS の方は Docker Desktop ) が使用できること
  • VSCode が使用できること

ファイル構成

以下の構成でディレクトリとファイルを作成していきます。
とりあえず動かしてみたい方は GitHub から Clone してください。

Project/
  ├─ .devcontainer/
  │    ├─ devcontainer.json
  │    └─ Dockerfile
  └─ mongodb-org.repo

手順.1 : VSCode に Remote-Containers ( 拡張機能 ) をインストール

VSCode に Remote-Containers という拡張機能をインストールしてください。

手順.2 : devcontainer.json 作成

.devcontainer/devcontainer.json
{
    "name": "test",
    "context": "..",
    "dockerFile": "Dockerfile",
    "settings": {
        "terminal.integrated.shell.linux": null,
        "workbench.startupEditor": "newUntitledFile",
        "editor.minimap.enabled": true,
        "vsicons.dontShowNewVersionMessage": true,
        "explorer.confirmDelete": false,
        "files.autoSave": "afterDelay"
    },
    "appPort": [ 8000, 27017 ],
    "remoteUser": "vscode",
    "workspaceFolder": "/var/www/html",
    "extensions": [
        "alefragnani.bookmarks",
        "mikestead.dotenv",
        "mhutchie.git-graph",
        "eamodio.gitlens",
        "onecentlin.laravel-blade",
        "austenc.laravel-blade-spacer",
        "onecentlin.laravel5-snippets",
        "cjhowe7.laravel-blade",
        "felixfbecker.php-pack",
        "vscode-icons-team.vscode-icons"
    ]
}

手順.3 : Dockerfile 作成

.devcontainer/Dockerfile
FROM centos:8

LABEL maintainer="slangsoft"
LABEL version="1.0"
LABEL description="Laravel sample project for trying out the VS Code Remote - Containers extension."

ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID
ARG PATH_LARAVEL=/var/www/html

# Basic settings
RUN set -x \
    && dnf -y upgrade \
    && dnf -y install sudo dnf-utils wget git \
    && groupadd --gid $USER_GID $USERNAME \
    && useradd --shell /bin/bash --uid $USER_UID --gid $USER_GID --create-home $USERNAME \
    && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
    && chmod 0440 /etc/sudoers.d/$USERNAME

# Install nginx
RUN  dnf -y install nginx

# Install php
RUN set -x \
    && dnf module reset php \
    && dnf -y install epel-release glibc-locale-source glibc-langpack-en \
    && dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm \
    && dnf -y module install php:remi-7.4

# Install Laravel
EXPOSE 8000
RUN set -x \
    && dnf -y install php-mcrypt php-pdo php-bcmath php-tokenizer php-xml php-mysqlnd php-gd php-intl php-zip php-opcache php-pecl-xdebug \
    && wget https://getcomposer.org/installer -O composer-installer.php \
    && php composer-installer.php --filename=composer --install-dir=/usr/local/bin \
    && composer self-update \
    && composer create-project laravel/laravel $PATH_LARAVEL --prefer-dist \
    && groupadd laravel \
    && gpasswd -a $USERNAME laravel \
    && gpasswd -a nginx laravel \
    && find $PATH_LARAVEL -type d -exec chmod 775 \{\} \; \
    && find $PATH_LARAVEL -type f -exec chmod 664 \{\} \; \
    && chown -R :$USERNAME $PATH_LARAVEL \
    && find $PATH_LARAVEL -type d -exec chmod g+s \{\} \; \
    && setfacl -R -d -m g::rwx $PATH_LARAVEL

# Install MongoDB
EXPOSE 27017
COPY ./mongodb-org.repo /etc/yum.repos.d/
RUN dnf -y install mongodb-org

# Set locale
RUN localedef -f UTF-8 -i ja_JP ja_JP.UTF-8
ENV LANG="ja_JP.UTF-8"
ENV LANGUAGE="ja_JP:ja"
ENV LC_ALL="ja_JP.UTF-8"

# Working directory setting
WORKDIR $PATH_LARAVEL

手順.4 : mongodb-org.repo 作成

mongodb-org.repo
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

手順.5 : コンテナ起動

コマンドパレットを開いて [remote-containers: Open Folder in Container...] を実行するとコンテナのビルドが始まります。無事にビルドが完了すると、コンテナ内で VSCode が起動します。

参考リンク

改訂履歴

  • 2020/08/11
    初版公開 ( ほとんど説明も無い叩き台記事 )
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3