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

Add docker support #936

Merged
merged 6 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
63 changes: 63 additions & 0 deletions docs/getting-started-docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
id: docker
title: Docker
---

[Docker](https://www.docker.com/) is a tool that enables you to create, deploy, and manage lightweight, stand-alone packages that contain everything needed to run an application. It can help us to avoid conflicting dependencies & unwanted behavior when running Docusaurus.

## Run the local webserver in docker

You need to ensure you have installed [docker](https://www.docker.com/get-started).

To run the local webserver you only need to do a few step:

1. Enter the folder where you have install docusaurus, and then run `docker build -t docusaurus-doc .`

After the build phase finished, you can run `docker images` to check the docker image list.

> We have already added a `Dockerfile` in your project when you install docusaurus, So you can build it directly.

2. Run docker start command: `docker run --rm -p 3000:3000 docusaurus-doc`

It will run a container with the image `docusaurus-doc`.And you can run `docker ps` to see the container info.

## Use docker-compose

We can use docker-compose to configure our application, run it with a single command.

> Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Using Compose is basically a three-step process:

1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.

2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.

3. Run docker-compose up and Compose starts and runs your entire app.

We have already added a basic `docker-compose.yml` in your project:
``` yml
version: "3"

services:
docusaurus:
build: .
ports:
- 3000:3000
- 35729:35729
volumes:
- ./docs:/app/docs
- ./website/blog:/app/website/blog
- ./website/core:/app/website/core
- ./website/i18n:/app/website/i18n
- ./website/pages:/app/website/pages
- ./website/static:/app/website/static
- ./website/sidebars.json:/app/website/sidebars.json
- ./website/siteConfig.js:/app/website/siteConfig.js
working_dir: /app/website

```

To run a local webserver with `docker-compose` you only need to run `docker-compose up`.

If you want to build static HTML pages and publish, you can run `docker-compose run docusaurus bash -c 'yarn publish-gh-pages'`
10 changes: 10 additions & 0 deletions examples/basics/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:8.11.4

WORKDIR /app/website

EXPOSE 3000 35729
COPY ./docs /app/docs
COPY ./website /app/website
RUN yarn install

CMD ["yarn", "start"]
18 changes: 18 additions & 0 deletions examples/basics/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: "3"

services:
docusaurus:
build: .
ports:
- 3000:3000
- 35729:35729
volumes:
- ./docs:/app/docs
- ./website/blog:/app/website/blog
- ./website/core:/app/website/core
- ./website/i18n:/app/website/i18n
- ./website/pages:/app/website/pages
- ./website/static:/app/website/static
- ./website/sidebars.json:/app/website/sidebars.json
- ./website/siteConfig.js:/app/website/siteConfig.js
working_dir: /app/website
2 changes: 2 additions & 0 deletions examples/basics/dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*/node_modules
*.log
68 changes: 40 additions & 28 deletions lib/copy-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,9 @@ if (feature === 'translations') {
// copy docs examples
if (fs.existsSync(`${CWD}/../docs-examples-from-docusaurus`)) {
console.log(
`${chalk.yellow(
'Example docs already exist!'
)} Rename or remove ${chalk.yellow(
`${outerFolder}/docs-examples-from-docusaurus`
)} to regenerate example docs.\n`
`- ${chalk.green(
'docs-examples-from-docusaurus'
)} already exists in ${chalk.blue(outerFolder)}.`
);
} else {
fs.copySync(
Expand All @@ -141,11 +139,9 @@ if (feature === 'translations') {
// copy blog examples
if (fs.existsSync(`${CWD}/blog-examples-from-docusaurus`)) {
console.log(
`${chalk.yellow(
'Example blog posts already exist!'
)} Rename or remove ${chalk.yellow(
`${outerFolder}/website/blog-examples-from-docusaurus`
)} to regenerate example blog posts.\n`
`- ${chalk.green(
'blog-examples-from-docusaurus'
)} already exists in ${chalk.blue(`${outerFolder}/website`)}.`
);
} else {
fs.copySync(
Expand All @@ -155,20 +151,33 @@ if (feature === 'translations') {
exampleSiteCreated = true;
blogCreated = true;
}

const copyFileToProjectFolder = (fileNameFrom, fileNameTo) => {
const copiedFileName = fileNameTo || fileNameFrom;
const src = path.join(folder, fileNameFrom);
const dest = path.join(CWD, '..', copiedFileName);
if (fs.existsSync(dest)) {
console.log(
`- ${chalk.green(copiedFileName)} already exists in ${chalk.blue(
outerFolder
)}.`
);
} else {
fs.copySync(src, dest);
}
};

// copy .gitignore file
let gitignoreName = '.gitignore';
if (fs.existsSync(`${CWD}/../.gitignore`)) {
gitignoreName = '.gitignore-example-from-docusaurus';
console.log(
`${chalk.yellow('.gitignore already exists')} in ${chalk.yellow(
CWD
)}. Creating an example gitignore file for you to copy from if desired.\n`
);
}
fs.copySync(
path.join(folder, 'gitignore'),
path.join(CWD, `/../${gitignoreName}`)
);
copyFileToProjectFolder('gitignore', '.gitignore');

// copy Dockerfile file
copyFileToProjectFolder('Dockerfile');

// copy docker-compose.yml file
copyFileToProjectFolder('docker-compose.yml');

// copy .dockerignore file
copyFileToProjectFolder('dockerignore', '.dockerignore');

// copy other files
const files = glob.sync(`${folder}/**/*`);
Expand All @@ -179,6 +188,9 @@ if (feature === 'translations') {
const containingFolder = path.basename(path.dirname(file));
if (
path.basename(file) === 'gitignore' ||
path.basename(file) === 'Dockerfile' ||
path.basename(file) === 'docker-compose.yml' ||
path.basename(file) === 'dockerignore' ||
containingFolder === 'blog-examples-from-docusaurus' ||
containingFolder === 'docs-examples-from-docusaurus'
) {
Expand All @@ -193,11 +205,11 @@ if (feature === 'translations') {
exampleSiteCreated = true;
} catch (e) {
console.log(
`${chalk.yellow(
`${path.basename(filePath)} already exists`
)} in ${chalk.yellow(
`website${filePath.split(path.basename(filePath))[0]}`
)}. Rename or remove the file to regenerate an example version.\n`
`- ${chalk.green(
`${path.basename(filePath)}`
)} already exists in ${chalk.blue(
`${outerFolder}/website${filePath.split(path.basename(filePath))[0]}`
)}.`
);
}
});
Expand Down
3 changes: 2 additions & 1 deletion website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"installation",
"site-preparation",
"site-creation",
"publishing"
"publishing",
"docker"
],
"Guides": [
"adding-blog",
Expand Down