Nanobox Craftcms Dry Run

Craft CMS on Nanobox: Part 2 – DRY-RUN a.k.a Local Staging

Published , updated , author Oto Hlincik

What is DRY-RUN?

In the previous article we focused on getting a local development environment for Craft 3 project setup on Nanobox. Hopefully, everything worked seamlessly and now we have a fully functioning Craft 3 website running locally. Before we proceed with deploying the website live, it is a very good idea to test that everything will work properly once the code is pushed to the live server.

While your Craft 3 project runs in a Nanobox local development environment that resembles as closely as possible the production environment (i.e. components/containers), inevitably there comes a time when your production environment needs some additional setup. Nanobox makes this possible through the deploy.config section of the boxfile.yml.

So, before you actually deploy your project live, you'd want to stage the application locally by deploying it to the Nanobox dry-run.

Deploying Craft 3 project with dry-run

We can add a local DNS entry to make it easier to preview our dry-run website in a browser.

nanobox dns add dry-run stage.my-craft3.test

Then, simply perform the dry-run deploy.

nanobox deploy dry-run

This will perform a full micro-platform build and deploy on your local machine and simulate the production deploy (i.e. deploy hooks run, web and worker components are created and started, cron jobs are schedule and run, etc.)

After the dry-run deploy completes, Nanobox automatically starts the php-server and your website is now accessible in a browser through the DNS entry we setup earlier: http://stage.my-craft3.test

Immediately, you'll see that your website is not working because the dry-run database component is empty. You will need to copy your local development database data into the dry-run database component. Thankfully, this can be done quite effectively.

Copy your local db data into the dry-run db

First, open a new terminal window/tab and have Nanobox list the environment info for the local environment.

nanobox info local

That will produce an output that looks something like the below. Note that the local environment will display (dev) after the name of your app.

-----------------------------------------
my-craft3 (dev)              Status: up
-----------------------------------------

Mount Path: /Users/macuser/Sites/Nanobox/my-craft3
Env IP: 172.21.0.13

data.db
  IP      : 172.21.0.14
  User(s) :
    root - JeMyf3Ji1J
    nanobox - 27ICipDug9

data.storage
  IP      : 172.21.0.15
  User(s) :
    gonano - kC8sKUzPQK

Environment Variables
  DATA_STORAGE_USER = gonano
  DATA_STORAGE_USERS = gonano
  APP_NAME = dev
  DATA_DB_ROOT_PASS = JeMyf3Ji1J
  DATA_DB_USER = nanobox
  DATA_STORAGE_GONANO_PASS = kC8sKUzPQK
  DATA_STORAGE_HOST = 172.21.0.15
  DATA_STORAGE_PASS = kC8sKUzPQK
  DATA_DB_HOST = 172.21.0.14
  DATA_DB_NANOBOX_PASS = 27ICipDug9
  DATA_DB_PASS = 27ICipDug9
  DATA_DB_USERS = root nanobox

DNS Aliases
  my-craft3.test

Then, you'll need to do the same for the dry-run environment.

nanobox info dry-run

The output would look something like below and the dry-run environment will display (sim) after the name of your app.

-----------------------------------------
my-craft3 (sim)              Status: up
-----------------------------------------

Mount Path: /Users/macuser/Sites/Nanobox/my-craft3
Env IP: 172.21.0.16

data.db
  IP      : 172.21.0.19
  User(s) :
    root - tuadrU5yeU
    nanobox - WK1muxAAJH

data.storage
  IP      : 172.21.0.20
  User(s) :
    gonano - H7PLTM5hjz

hoarder (Storage)
  IP      : 172.21.0.21

logvac (Logger)
  IP      : 172.21.0.17

mist (Message Bus)
  IP      : 172.21.0.18

portal (Router)
  IP      : 172.21.0.16

web.craft
  IP      : 172.21.0.22

Environment Variables
  DATA_DB_HOST = 172.21.0.19
  DATA_STORAGE_GONANO_PASS = H7PLTM5hjz
  DATA_STORAGE_HOST = 172.21.0.20
  DATA_STORAGE_PASS = H7PLTM5hjz
  DATA_STORAGE_USER = gonano
  HOARDER_HOST = 172.21.0.21
  DATA_DB_PASS = WK1muxAAJH
  MIST_HOST = 172.21.0.18
  PORTAL_HOST = 172.21.0.16
  APP_NAME = sim
  DATA_DB_NANOBOX_PASS = WK1muxAAJH
  DATA_DB_USER = nanobox
  DATA_DB_ROOT_PASS = tuadrU5yeU
  DATA_DB_USERS = root nanobox
  DATA_STORAGE_USERS = gonano
  LOGVAC_HOST = 172.21.0.17

DNS Aliases
  stage.my-craft3.test

Now, with the information for both environments handy, you need to console into the dry-run database component and run 2 commands to export the data from the local database and import it to the dry-run database.

# console into the dry-run database component
nanobox console dry-run data.db

# export the data from the local database
# Note: substitute the DATA_DB_HOST and DATA_DB_PASS with local environment info values
mysqldump -h DATA_DB_HOST -u nanobox --password=DATA_DB_PASS --force --no-create-db gonano | gzip > /tmp/db_dump.sql.gz

# import the data into the dry-run database
# Note: substitute the DATA_DB_PASS with dry-run environment info value
gunzip /tmp/db_dump.sql.gz && mysql -v -u nanobox --password=DATA_DB_PASS gonano < /tmp/db_dump.sql && rm /tmp/db_dump.sql

# exit the dry-run database component
exit

Important: You need to have both the local and dry-run environments running, otherwise the command to export the local database data will fail. You can check the status of your Nanobox apps by running the following command in your terminal.

# check the status of your Nanobox apps
nanobox status

Status: Running

App                   : Status  : Path
-----------------------------------------------------------------------------
my-craft3 (local)     : up      : /Users/macuser/Sites/Nanobox/my-craft3
my-craft3 (dry-run)   : up      : /Users/macuser/Sites/Nanobox/my-craft3

Now, when you go back to your browser and refresh the http://stage.my-craft3.test page, you should see your local staging version of your website running. However, if you setup any local asset volumes, these will be missing in your dry-run website. Similarly to the database data, you will need to copy the asset files to the dry-run storage component.

Copy your local asset volumes into the dry-run storage component

Assuming that your local asset volumes reside inside the web/assets directory within your craft root directory and using the dry-run info output by the nanobox info dry-run command, we can copy all the assets using the rsync utility.

# Note: substitute the DATA_STORAGE_HOST with the dry-run environment info
rsync -rtvO --super web/assets gonano@DATA_STORAGE_HOST:/data/var/db/unfs/web

# copy and paste the value of DATA_STORAGE_GONANO_PASS from the dry-run environment when 'rsync' asks you to input a password

Now, all your assets are available in your dry-run website.

When you no longer need the dry-run environment

After you successfully tested the dry-run deployment and your local staging website is running, you can remove the dry-run environment from your computer by running the following command in the root directory of your Craft 3 application.

nanobox destroy dry-run

In the next article, we will deploy our website live on Digital Ocean droplet through Nanobox.