diff --git a/docker-compose.yml b/docker-compose.yml index 1a9e0c7c8..5333ed6d9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,6 @@ -version: "3.9" services: db: + restart: on-failure:3 build: context: "." dockerfile: docker/Dockerfile.db @@ -20,10 +20,10 @@ services: uwsgi: - restart: always + restart: on-failure:3 build: context: "." - dockerfile: docker/Dockerfile + dockerfile: ${CLIENT_DOCKERFILE} depends_on: db: condition: service_healthy @@ -47,7 +47,7 @@ services: - db redis: - restart: always + restart: on-failure:3 image: redis:latest healthcheck: test: ["CMD", "redis-cli","ping"] diff --git a/docker/Dockerfile b/docker/Dockerfile index 0fb89166a..15c7fb626 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,24 +1,22 @@ FROM ruby:3.0.0 -# docker build -f docker/Dockerfile -t qpixel_uwsgi . - ENV RUBYOPT="-KU -E utf-8:utf-8" RUN apt-get update && \ apt-get install -y gcc && \ apt-get install -y make && \ apt-get install -y \ - default-libmysqlclient-dev \ - autoconf \ - bison \ - build-essential \ - libssl-dev \ - libyaml-dev \ - libreadline-dev \ - zlib1g-dev \ - libncurses5-dev \ - libffi-dev \ - libgdbm-dev && \ - apt-get install -y default-mysql-server + default-libmysqlclient-dev \ + autoconf \ + bison \ + build-essential \ + libssl-dev \ + libyaml-dev \ + libreadline-dev \ + zlib1g-dev \ + libncurses5-dev \ + libffi-dev \ + libgdbm-dev && \ + apt-get install -y default-mysql-server # Install nodejs and imagemagick WORKDIR /opt diff --git a/docker/Dockerfile.arm b/docker/Dockerfile.arm new file mode 100644 index 000000000..06b954c0b --- /dev/null +++ b/docker/Dockerfile.arm @@ -0,0 +1,50 @@ +FROM --platform=linux/arm64/v8 ruby:3.0.0 + +ENV RUBYOPT="-KU -E utf-8:utf-8" +RUN apt-get update && \ + apt-get install -y gcc && \ + apt-get install -y make && \ + apt-get install -y \ + default-libmysqlclient-dev \ + autoconf \ + bison \ + build-essential \ + ruby-dev \ + libssl-dev \ + libyaml-dev \ + libreadline-dev \ + zlib1g-dev \ + liblzma-dev \ + libncurses5-dev \ + libffi-dev \ + libgdbm-dev && \ + patch && \ + apt-get install -y default-mysql-server + +RUN dpkg --add-architecture amd64 +RUN apt update +RUN apt install -y libc6:amd64 + +# Install nodejs and imagemagick +WORKDIR /opt +RUN wget https://nodejs.org/dist/v12.18.3/node-v12.18.3-linux-x64.tar.xz && \ + tar xf node-v12.18.3-linux-x64.tar.xz && \ + wget https://imagemagick.org/archive/binaries/magick && \ + chmod +x magick && \ + mv magick /usr/local/bin/magick + +ENV NODEJS_HOME=/opt/node-v12.18.3-linux-x64/bin +ENV PATH=$NODEJS_HOME:$PATH + +# Add core code to container +WORKDIR /code +COPY . /code + +RUN gem install bundler -v 2.4.22 +RUN bundle install +RUN gem install nokogiri --platform=ruby + +EXPOSE 80 443 3000 +ENTRYPOINT ["/bin/bash"] +CMD ["/code/docker/entrypoint.sh"] + diff --git a/docker/Dockerfile.dev b/docker/Dockerfile.dev new file mode 100644 index 000000000..af533f570 --- /dev/null +++ b/docker/Dockerfile.dev @@ -0,0 +1,43 @@ +FROM ruby:3.0.0 + +ENV RUBYOPT="-KU -E utf-8:utf-8" +RUN apt-get update && \ + apt-get install -y gcc && \ + apt-get install -y make && \ + apt-get install -y \ + default-libmysqlclient-dev \ + autoconf \ + bison \ + build-essential \ + libssl-dev \ + libyaml-dev \ + libreadline-dev \ + zlib1g-dev \ + libncurses5-dev \ + libffi-dev \ + libgdbm-dev && \ + apt-get install -y default-mysql-server + +# Install nodejs and imagemagick +WORKDIR /opt +RUN wget https://nodejs.org/dist/v12.18.3/node-v12.18.3-linux-x64.tar.xz && \ + tar xf node-v12.18.3-linux-x64.tar.xz && \ + wget https://imagemagick.org/archive/binaries/magick && \ + chmod +x magick && \ + mv magick /usr/local/bin/magick + +ENV NODEJS_HOME=/opt/node-v12.18.3-linux-x64/bin +ENV PATH=$NODEJS_HOME:$PATH + +# Add core code to container +WORKDIR /code +COPY . /code +RUN gem install bundler -v 2.4.22 && \ + bundle install + +EXPOSE 80 443 3000 + +RUN /code/docker/entrypoint.sh dev + +# ensures continued running of the container +CMD sleep 7d diff --git a/docker/README.md b/docker/README.md index edfec7c60..7a2f81886 100644 --- a/docker/README.md +++ b/docker/README.md @@ -32,7 +32,14 @@ chmod +x docker/local-setup.sh docker/local-setup.sh ``` -Editing the `./.env` file will modify the corresponding variables used in the docker-compose.yml file but **NOT** the environment variables in the container. Editing the `./docker/env` file will change environment variables only in the running container. +Editing the `./.env` file will modify the corresponding variables used in the docker-compose.yml file but **NOT** the environment variables in the container. +Editing the `./docker/env` file will change environment variables only in the running container. + +### Custom build config + +Our Docker setup supports custom build configurations for the uwsgi contianer via the `CLIENT_DOCKERFILE` environment variable (see [compose-env](/docker/compose-env)). The default is `docker/Dockerfile`, which points to a preconfigured [production-like setup](/docker/Dockerfile). For developers who need more control over their setup, we also provide a [configuration](/docker/Dockerfile.dev) that is tailored for local development. + +To use a custom build config, change the `CLIENT_DOCKERFILE` variable in the .env file that is automatically created by [local-setup.sh](/docker/local-setup.sh) in the project root. ## 2. Database File Ensure `config/database.yml` has the username and password as defined in [docker/env](docker/env) file. The `config/database.yml` should already be gitignored. @@ -61,17 +68,13 @@ NOTE: If you get an error like "Cannot connect to the Docker daemon at ...", you ## 4. Start Containers -Then start your containers! +Then start your containers: ```bash -docker compose up # append -d if you want to detach the processes, although it can be useful to see output into the terminal -Creating qpixel_redis_1 ... done -Creating qpixel_db_1 ... done -Creating qpixel_uwsgi_1 ... done +docker compose up # append -d (--detach) if you want, although it can be useful to see output in the terminal ``` -The uwsgi container has a sleep command for 15 seconds to give the database a chance to start, -so don't expect to see output right away. After about 20 seconds, check to make sure the server is running (and verify port 3000, note that you can change this mapping in the `.env` file) +After about 20 seconds, check to make sure the server is running (and verify port 3000, note that you can change this mapping in the `.env` file) ``` qpixel_uwsgi_1 | => Booting Puma @@ -94,6 +97,22 @@ and see the interface. You can then click "Sign in" to login with what you defined for `$COMMUNITY_ADMIN_EMAIL` and `$COMMUNITY_ADMIN_PASSWORD`. Importantly, your password must be 6 characters or longer, otherwise the user won't be created. +### Custom build configs + +If you are using a custom build config that doesn't automatically start Rails or our [config for local development](/docker/Dockerfile.dev), you will also have to manually start the server. + +After the containers have started, connect to the uwsgi container (if you are using Docker Desktop, you can connect directly from the application): + +```bash +docker exec -it qpixel-uwsgi-1 bash +``` + +And run the following command to start Rails (starting the server is intentionally disabled to allow for live debugging): + +```bash +rails server -b 0.0.0.0 +``` + ## 5. Login Once you are logged in, you should see your icon in the top right: diff --git a/docker/compose-env b/docker/compose-env index 162e3169b..140d36f60 100644 --- a/docker/compose-env +++ b/docker/compose-env @@ -3,3 +3,4 @@ COMMUNITY_NAME=Dev Community RAILS_ENV=development CONFIRMABLE_ALLOWED_ACCESS_DAYS=2 ENV_FILE_LOCATION=./docker/env +CLIENT_DOCKERFILE=docker/Dockerfile diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh old mode 100644 new mode 100755 index efbf2b14e..b6fbc730e --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -15,5 +15,7 @@ fi # If this isn't done again, there is a 500 error on the first page about posts rails db:seed -# defaults to port 3000 -rails server -b 0.0.0.0 +# we don't start the server immediately in dev mode +if [[ "$1" != 'dev' ]]; then + rails server -b 0.0.0.0 +fi