docker runを使って、Next.jsやGatsbyなんかをgenerateしてみるヘ(^o^)ノ

ローカル環境にnodeのruntimeがないので、Dockerを使っていろいろgenerateしてみる

Nodeのdocker imageでgenerate

基本的にはDockerのcommandを上書きしてあげると良いです

$ docker run -it -w '/app' -v $PWD:/app node:15.5.1-alpine3.12 [command]

僕の環境だとこんな感じです

$ cd dev   # ~/devにコードやらなんやらがある
$ docker run -it -w '/app' -v $PWD:/app node:15.5.1-alpine3.12 npx create-next-app my-app
$ ls -al
my-app/

Gatsbyだと

$ cd dev
$ docker run -it -w '/app' -v $PWD:/app node:15.5.1-alpine3.12 npm init gatsby
Command failed with ENOENT: git clone https://github.com/gatsbyjs/gatsby-starter-minimal.git /app/my-gatsby-site --recursive --depth=1 --quiet
spawn git ENOENT
npm notice 
npm notice New minor version of npm available! 7.3.0 -> 7.4.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v7.4.0
npm notice Run npm install -g npm@7.4.0 to update!
npm notice 
npm ERR! code 1
npm ERR! path /app
npm ERR! command failed
npm ERR! command sh -c create-gatsby

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-01-12T11_21_15_546Z-debug.log

templateをgit cloneしてくるところで怒られました

コンテナで確認してみます

$ docker run -it -w '/app' -v $PWD:/app node:15.5.1-alpine3.12 /bin/sh
/app # git -v
/bin/sh: git: not found

gitがインストールされてませんね

こういう場合は自分でイメージを作成する必要がありますかね

# ~/docker-images/node-dev/15.5.1-alpine3.12/Dockerfile
FROM node:15.5.1-alpine3.12 
 
WORKDIR /app 
 
RUN apk update --no-cache && \
    apk upgrade --no-cache && \
    apk --no-cache add git

buildして、もう一回試す

$ docker build -t node-dev:15.5.1-alpine3.12 ~/docker-images/node-dev/15.5.1-alpine3.12/
$ docker run -it -v $PWD:/app node-dev:15.5.1-alpine3.12 npm init gatsby
What would you like to call your site?                                                                                                                                                                                                                                                                                        
✔ · My Gatsby Site                                                                                                                                                                                                                                                                                                            
What would you like to name the folder where your site will be created?                                                                                                                                                                                                                                                       
✔ app/ my-gatsby-site                                                                                                                                                                                                                                                                                                         
✔ Will you be using a CMS?                                                                                                                                                                                                                                                                                                    
· Netlify CMS                                                                                                                                                                                                                                                                                                                 
✔ Would you like to install a styling system?                                                                                                                                                                                                                                                                                 
· Emotion                                                                                                                                                                                                                                                                                                                     
✔ Would you like to install additional features with other plugins?                                                                                                                                                                                                                                                           
· Add Markdown and MDX support                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                              
Thanks! Here's what we'll now do:                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                              
    🛠  Create a new Gatsby site in the folder my-gatsby-site                                                                                                                                                                                                                                                                  
    📚 Install and configure the plugin for Netlify CMS                                                                                                                                                                                                                                                                       
    🎨 Get you set up to use Emotion for styling your site                                                                                                                                                                                                                                                                    
    🔌 Install gatsby-plugin-mdx                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                              
✔ Shall we do this? (Y/n) · Yes                                                                                                                                                                                                                                                                                               
✔ Created site from template                                                                                                                                                                                                                                                                                                  
✔ Installed Gatsby                                                                                                                                                                                                                                                                                                            
✔ Installed plugins                                                                                                                                                                                                                                                                                                           
✔ Created site in my-gatsby-site                                                                                                                                                                                                                                                                                              
🔌 Setting-up plugins...                                                                                                                                                                                                                                                                                                      
╔════════════════════════════════════════════════════════════════════════╗                                                                                                                                                                                                                                                    
║                                                                        ║                                                                                                                                                                                                                                                    
║   Gatsby collects anonymous usage analytics                            ║                                                                                                                                                                                                                                                    
║   to help improve Gatsby for all users.                                ║                                                                                                                                                                                                                                                    
║                                                                        ║                                                                                                                                                                                                                                                    
║   If you'd like to opt-out, you can use `gatsby telemetry --disable`   ║                                                                                                                                                                                                                                                    
║   To learn more, checkout https://gatsby.dev/telemetry                 ║                                                                                                                                                                                                                                                    
║                                                                        ║                                                                                                                                                                                                                                                    
╚════════════════════════════════════════════════════════════════════════╝                                                                                                                                                                                                                                                    
info Adding gatsby-plugin-netlify-cms                                                                                                                                                                                                                                                                                         
info Adding gatsby-plugin-emotion                                                                                                                                                                                                                                                                                             
info Adding gatsby-plugin-mdx                                                                                                                                                                                                                                                                                                 
info Adding gatsby-source-filesystem                                                                                                                                                                                                                                                                                          
info Installed gatsby-plugin-netlify-cms in gatsby-config.js                                                                                                                                                                                                                                                                  
success Adding gatsby-plugin-netlify-cms to gatsby-config - 0.157s                                                                                                                                                                                                                                                            
info Installed gatsby-plugin-emotion in gatsby-config.js                                                                                                                                                                                                                                                                      
success Adding gatsby-plugin-emotion to gatsby-config - 0.153s                                                                                                                                                                                                                                                                
info Installed gatsby-plugin-mdx in gatsby-config.js                                                                                                                                                                                                                                                                          
success Adding gatsby-plugin-mdx to gatsby-config - 0.161s                                                                                                                                                                                                                                                                    
info Installed pages in gatsby-config.js                                                                                                                                                                                                                                                                                      
success Adding gatsby-source-filesystem (pages) to gatsby-config - 1.213s                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                              
*** Please tell me who you are.                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                              
Run                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                              
  git config --global user.email "you@example.com"                                                                                                                                                                                                                                                                            
  git config --global user.name "Your Name"                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                              
to set your account's default identity.                                                                                                                                                                                                                                                                                       
Omit --global to set the identity only in this repository.

Initial git commit failed - removing git support

🎉  Your new Gatsby site My Gatsby Site has been successfully created
at /app/my-gatsby-site.
Start by going to the directory with

  cd my-gatsby-site

Start the local development server with

  npm run develop

See all commands at

  https://www.gatsbyjs.com/docs/gatsby-cli/

できたっぽい

公式のdockerイメージには、ENTRYPOINTCMDが設定されています

CMDを上書きする場合は、docker run コマンドのあとにつらつら追加していけばOKです

あと、ENTRYPOINTも上書きしたい場合もあるかもしれません

そんなときはENTRYPOINTを指定できます

npxコマンドで上書き

$ docker run --entrypoint npx -it node:15.5.1-alpine3.12 -v
7.3.0

no more runtime on localhost ヘ(^o^)ノ