Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add docker compose #2049

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fair-ducks-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": patch
---

create docker-compose.yml for postgres/mysql database options
26 changes: 16 additions & 10 deletions cli/src/installers/dbContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@ export const dbContainerInstaller: Installer = ({
databaseProvider,
projectName,
}) => {
const scriptSrc = path.join(
PKG_ROOT,
`template/extras/start-database/${databaseProvider}.sh`
);
const scriptText = fs.readFileSync(scriptSrc, "utf-8");
const scriptDest = path.join(projectDir, "start-database.sh");
// for configuration with postgresql and mysql when project is created with '.' project name
const [projectNameParsed] =
projectName === "." ? parseNameAndPath(projectDir) : [projectName];

// Sanitize the project name for Docker container usage
const sanitizedProjectName = sanitizeName(projectNameParsed);

fs.writeFileSync(
scriptDest,
scriptText.replaceAll("project1", sanitizedProjectName)
const dockerFolder = path.join(
PKG_ROOT,
"template/extras/docker/",
databaseProvider
);
fs.chmodSync(scriptDest, "755");

const files = fs.readdirSync(dockerFolder);
for (const file of files) {
const scriptSrc = path.join(dockerFolder, file);
const scriptDest = path.join(projectDir, file);
const scriptText = fs.readFileSync(scriptSrc, "utf-8");
fs.writeFileSync(
scriptDest,
scriptText.replaceAll("project1", sanitizedProjectName)
);
fs.chmodSync(scriptDest, "755");
}
};
10 changes: 10 additions & 0 deletions cli/template/extras/docker/mysql/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
db:
image: mysql:latest
container_name: ${DB_CONTAINER_NAME?}
restart: unless-stopped
ports:
- "${DB_PORT?}:3306"
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD?}
MYSQL_DATABASE: ${DB_NAME?}
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ if [ "$DB_PASSWORD" == "password" ]; then
sed -i -e "s#:password@#:$DB_PASSWORD@#" .env
fi

docker run -d \
--name $DB_CONTAINER_NAME \
-e MYSQL_ROOT_PASSWORD="$DB_PASSWORD" \
-e MYSQL_DATABASE="$DB_NAME" \
-p "$DB_PORT":3306 \
docker.io/mysql && echo "Database container '$DB_CONTAINER_NAME' was successfully created"
export DB_PASSWORD
export DB_PORT
export DB_NAME
export DB_CONTAINER_NAME

docker compose up -d \
&& echo "Database container '$DB_CONTAINER_NAME' was successfully created"
11 changes: 11 additions & 0 deletions cli/template/extras/docker/postgres/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
db:
image: postgres:latest
container_name: ${DB_CONTAINER_NAME?}
restart: unless-stopped
ports:
- "${DB_PORT?}:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ${DB_PASSWORD?}
POSTGRES_DB: ${DB_NAME?}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ if [ "$DB_PASSWORD" = "password" ]; then
sed -i -e "s#:password@#:$DB_PASSWORD@#" .env
fi

docker run -d \
--name $DB_CONTAINER_NAME \
-e POSTGRES_USER="postgres" \
-e POSTGRES_PASSWORD="$DB_PASSWORD" \
-e POSTGRES_DB="$DB_NAME" \
-p "$DB_PORT":5432 \
docker.io/postgres && echo "Database container '$DB_CONTAINER_NAME' was successfully created"
export DB_PASSWORD
export DB_PORT
export DB_NAME
export DB_CONTAINER_NAME

docker compose up -d \
&& echo "Database container '$DB_CONTAINER_NAME' was successfully created"
1 change: 1 addition & 0 deletions www/src/components/docs/folderStructureDiagramApp.astro
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"prettier.config.js": ["tailwind"],
"README.md": [],
"start-database.sh (mysql or postgres only)": ["drizzle"],
"docker-compose.yml (mysql or postgres only)": ["drizzle"],
"tailwind.config.ts": ["tailwind"],
"tsconfig.json": [],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"prettier.config.js": ["tailwind"],
"README.md": [],
"start-database.sh (mysql or postgres only)": ["drizzle"],
"docker-compose.yml (mysql or postgres only)": ["drizzle"],
"tailwind.config.ts": ["tailwind"],
"tsconfig.json": [],
};
Expand Down
7 changes: 7 additions & 0 deletions www/src/pages/en/folder-structure-app.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ The `prettier.config.mjs` file is used to configure Prettier to include the pret

The `start-database.sh` file is used to start the database. Please see the comments inside the file for information on how to start the database with your operating system.

</div>
<div data-components="drizzle">

### `docker-compose.yml (mysql or postgres only)`

The `docker-compose.yml` file is run after `start-database.sh` loads the necessary environment variables. Please see the contents inside for the environment variables that are required. If they are set you can directly run `docker compose up -d`.

</div>
<div>

Expand Down
7 changes: 7 additions & 0 deletions www/src/pages/en/folder-structure-pages.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ The `prettier.config.mjs` file is used to configure Prettier to include the pret

The `start-database.sh` file is used to start the database. Please see the comments inside the file for information on how to start the database with your operating system.

</div>
<div data-components="drizzle">

### `docker-compose.yml (mysql or postgres only)`

The `docker-compose.yml` file is run after `start-database.sh` loads the necessary environment variables. Please see the contents inside for the environment variables that are required. If they are set you can directly run `docker compose up -d`.

</div>
<div>

Expand Down
Loading