From 92bfae4b2be17919abb3e2101cdd23c656191b3f Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Mon, 2 Nov 2020 09:53:51 +0100
Subject: [PATCH] docker-compose: add a build.sh script which automates the
 setup

* Create the user in the image with the same UID than the host user.
  This makes it easier to share files between the host and the container.
* Install the PHP dependencies right away, so the user doesn't have to
  know anything about composer at the beginning.
---
 docker-compose/README.md          | 4 +++-
 docker-compose/build.sh           | 7 +++++++
 docker-compose/php-fpm/Dockerfile | 3 ++-
 docker-compose/{run => run.sh}    | 2 +-
 4 files changed, 13 insertions(+), 3 deletions(-)
 create mode 100755 docker-compose/build.sh
 rename docker-compose/{run => run.sh} (80%)

diff --git a/docker-compose/README.md b/docker-compose/README.md
index cbc07d2..1889217 100644
--- a/docker-compose/README.md
+++ b/docker-compose/README.md
@@ -6,6 +6,8 @@ the symfony app.
 ## Setup
 
 * `sudo apt install docker-compose` to install docker-compose on Debian/Ubuntu
+* `sudo ./build.sh` - to build the docker images and install the PHP dependencies
+* `sudo docker-compose up` - to run the server: http://127.0.0.1:8000
 
 ## Running the Server
 
@@ -17,7 +19,7 @@ the symfony app.
 
 ## Executing Symfony commands
 
-* `./run` to start a shell in the php-fpm container.
+* `./run.sh` to start a shell in the php-fpm container.
 * After this you can run symfony/composer commands like:
   * `./bin/console debug:config`
   * `composer install`
diff --git a/docker-compose/build.sh b/docker-compose/build.sh
new file mode 100755
index 0000000..91968e8
--- /dev/null
+++ b/docker-compose/build.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+# We need to creat the user inside the container with the same ID as the host user
+docker-compose build --build-arg "UID=${SUDO_UID:-$UID}"
+
+# Install dependencies
+docker-compose run --rm --entrypoint composer php-fpm install
\ No newline at end of file
diff --git a/docker-compose/php-fpm/Dockerfile b/docker-compose/php-fpm/Dockerfile
index b2e792c..1a35715 100644
--- a/docker-compose/php-fpm/Dockerfile
+++ b/docker-compose/php-fpm/Dockerfile
@@ -38,7 +38,8 @@ RUN wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
 
 STOPSIGNAL SIGQUIT
 
-RUN useradd -u 1000 -ms /bin/bash user
+ARG UID
+RUN useradd -u "$UID" -ms /bin/bash user
 RUN echo 'user ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
 USER user
 
diff --git a/docker-compose/run b/docker-compose/run.sh
similarity index 80%
rename from docker-compose/run
rename to docker-compose/run.sh
index 79363b1..adef6ef 100755
--- a/docker-compose/run
+++ b/docker-compose/run.sh
@@ -1,3 +1,3 @@
-#!/bin/sh
+#!/bin/bash
 
 docker-compose run --rm --entrypoint bash php-fpm
-- 
GitLab