Bootstrap FreeKB - Sails - Create a new Sails app on Docker using Docker Compose
Sails - Create a new Sails app on Docker using Docker Compose

Updated:   |  Sails articles

This assumes you have created a Sails image on Docker.

A Docker image contains the code used to create a Docker container. In this way, an image is like a template used to create a container. An image is kind of like a snapshot of a virtual machine.

 

And the docker images command should return something like this.

~]# docker images
REPOSITORY    TAG     IMAGE ID      CREATED             SIZE
nodejs/sails  latest  fb60d5e3e7c8  About a minute ago  973MB
node          latest  7528ad312b56  About a minute ago  944MB

 

The following docker-compose.yml file can be used to create a new Sails app named foo-app. Let's break this down.

AVOID TROUBLE

By default, in the container, the app will be located at /<app name>, such as /foo-app in this example. The second field in --volume will need to point to /<app name>.

  • The nodejs/sails:latest image is used
  • The name of the container will be foo-app.
  • The Docker server will be listening on port 8080 and the Sails service in the container will be listening on port 1337.
  • The volumes option is used to mount a directory on your Docker server (/usr/local/apps) to a directory in the container (/foo-app).
  • The working_dir option is used to set /foo-app as the present working directory in the container. This is needed because the sails new command will create the new Sails app in the present working directory. We want our Sails app to be created at /foo-app in the container.
  • The sails new command is used to create a new Sails app. When the Sails image, as long as the -g or --global flag was used, then the sails new command can be used from any directory, including the /foo-app directory. The echo 1 option is used to create a Sails web app. Or, echo 2 could be used to create an empty Sails app.
version: '3'
services:
  new:
    image: nodejs/sails:latest
    container_name: 'foo-app'
    ports:
      - '8080:1337'
    volumes:
      - /usr/local/apps/foo-app:/foo-app
    command: /bin/bash -c "echo 1 | sails new foo-app"

 

The docker-compose up command can then be used.

docker-compose up

 

The following should be displayed. Since "echo 1" was used, a Sails web app will be created.

 Choose a template for your new Sails app:
 1. Web App  ·  Extensible project with auth, login, & password recovery
 2. Empty    ·  An empty Sails app, yours to configure
 (type "?" for help, or <CTRL+C> to cancel)
? 1
 info: Installing dependencies...
Press CTRL+C to cancel.
(to skip this step in the future, use --fast)
 info: Created a new Sails app `foo-app`!

 

Since --volume mounted the /usr/local/apps/foo-app directory on the Docker server to /foo-app in the container, the /usr/local/apps/foo-app directory on the Docker server should now exist and contain the files that make up foo-app.

~]# ll /usr/local/apps/foo-app
drwxr-xr-x  9 root root 4096 Jul 31 05:29 foo-app

 

Be aware that the container will still exists, in an Exited state.

~]# docker container ls -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS                          PORTS     NAMES
db5ed1f766ee   nodejs/sails:latest   "docker-entrypoint.s…"   2 minutes ago   Exited (0) About a minute ago             foo-app

 

The docker rm command can be used to remove the exited container.

docker rm foo-app

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 8c053f in the box below so that we can be sure you are a human.