Run postgresql using docker compose on devcontainer

This will make easier to make sure the service is running when the
development container is started.

Before after the first time the container was created, if it was
restarted you needed to run the boot.sh script manually to start the
service.
This commit is contained in:
Rafael Mendonça França 2023-08-04 23:57:36 +00:00
parent 5c69954d6b
commit 451dec7968
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
5 changed files with 67 additions and 38 deletions

@ -1,18 +1,18 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.191.1/containers/ruby/.devcontainer/base.Dockerfile
# [Choice] Ruby version: 3, 3.0, 2, 2.7, 2.6
ARG VARIANT="3.0"
FROM mcr.microsoft.com/devcontainers/ruby:0-${VARIANT}
ARG VARIANT="3"
FROM mcr.microsoft.com/devcontainers/ruby:${VARIANT}
# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
ARG NODE_VERSION="none"
ARG NODE_VERSION="lts/*"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
# [Optional] Uncomment this section to install additional OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends \
mariadb-server libmariadb-dev \
postgresql postgresql-client postgresql-contrib libpq-dev \
postgresql-client postgresql-contrib libpq-dev \
redis-server memcached \
ffmpeg mupdf mupdf-tools libvips poppler-utils

@ -3,16 +3,14 @@ yarn install
sudo chown -R vscode:vscode /usr/local/bundle
sudo service postgresql start
sudo service mariadb start
sudo service redis-server start
sudo service memcached start
# Create PostgreSQL users and databases
sudo su postgres -c "createuser --superuser vscode"
sudo su postgres -c "createdb -O vscode -E UTF8 -T template0 activerecord_unittest"
sudo su postgres -c "createdb -O vscode -E UTF8 -T template0 activerecord_unittest2"
# Create MySQL database and databases
cd activerecord
MYSQL_CODESPACES=1 bundle exec rake db:mysql:build
# Create PostgreSQL databases
bundle exec rake db:postgresql:rebuild
# Create MySQL databases
MYSQL_CODESPACES=1 bundle exec rake db:mysql:rebuild

@ -1,24 +1,30 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.191.1/containers/ruby
// For format details, see https://aka.ms/devcontainer.json.
{
"name": "Ruby",
"build": {
"dockerfile": "Dockerfile",
"context": "..",
"args": {
// Update 'VARIANT' to pick a Ruby version: 3, 3.0, 2, 2.7, 2.6
"VARIANT": "3",
// Options
"NODE_VERSION": "lts/*"
}
},
"name": "Rails project development",
"dockerComposeFile": "docker-compose.yml",
"service": "rails",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
"ghcr.io/devcontainers/features/github-cli:1": {
"version": "latest"
}
},
"containerEnv": {
"PGHOST": "postgres",
"PGUSER": "postgres",
"PGPASSWORD": "postgres"
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// This can be used to network with other containers or the host.
// "forwardPorts": [3000, 5432],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": ".devcontainer/boot.sh",
// Configure tool-specific properties.
"customizations": {
"vscode": {
@ -29,16 +35,6 @@
}
},
"containerEnv": {
"MYSQL_SOCK": "/run/mysqld/mysqld.sock"
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": ".devcontainer/boot.sh",
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}

@ -0,0 +1,35 @@
version: '3'
services:
rails:
build:
context: ..
dockerfile: .devcontainer/Dockerfile
volumes:
- ../..:/workspaces:cached
# Overrides default command so things don't shut down after the process ends.
command: sleep infinity
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
network_mode: service:postgres
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)
postgres:
image: postgres:latest
restart: unless-stopped
volumes:
- rails-postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_DB: postgres
POSTGRES_PASSWORD: postgres
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)
volumes:
rails-postgres-data:

@ -253,7 +253,7 @@ namespace :db do
end
desc "Drop the MySQL test databases"
task :drop do
task drop: ["db:mysql:build_user"] do
%x( mysql #{mysql2_connection_arguments["arunit"]} -e "drop database IF EXISTS #{mysql2_config["arunit"]["database"]}" )
%x( mysql #{mysql2_connection_arguments["arunit2"]} -e "drop database IF EXISTS #{mysql2_config["arunit2"]["database"]}" )