0

I am trying to create a project for sails js using Docker and Docker Compose. What I want is to generate the sails code inside Docker and generate the structure in the current directory.

When I try to generate it, as the directory is not empty because I have a Dockerfile and a docker-compose.yml, I'm getting an error saying that the directory is not empty.

This is the Dockerfile:

FROM node:8.9.3

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY . /usr/src/app

RUN npm install -g sails
RUN sails new .
RUN sails lift

With this configuration I get the error of current directory not empty.

Thinking about it I tried generating the code and the copying it.

...

RUN npm install -g sails
RUN sails new .

# I do the COPY after generating the code
COPY . /usr/src/app

RUN sails lift

This seems to work because I don't get any error in my local environment I don't have the generated code.

Maybe this process is wrong and I should install it as a dependency and then run with docker-compose a container to generate the code, but I think I will have the same problem about trying to generate the code in a non empty directory.

5
  • 1
    I'm confused as to what you are trying to accomplish. The sails new command would be creating the source code that you are already copying from the build context. I would think that you do one or the other (not both). If the project was already created locally with sails new, then why run it at docker build time? Commented Dec 15, 2017 at 18:11
  • Maybe it's not correct to generate the sails code inside Docker. Probably I should do it in local and copy them in the Docker image. But the problem I have if that I don't want to use the local node, I want to install it using Docker but I have the problem that I can't generate the code in the current directory because it's not empty. Commented Dec 15, 2017 at 18:30
  • in your 2nd iteration you need to add this WORKDIR /usr/src/app after the COPY Commented Dec 15, 2017 at 19:41
  • I think I understand what you are trying to do... Maybe you can confirm it (and then I can create a proper answer for a suggestion on how to approach). You essentially want to avoid installing Sails (and Node) on the host and instead use a Docker container to generate the Sails code (sails init) and then build an image using that code? Commented Dec 15, 2017 at 19:44
  • @AndyShinn Yeah, that's what I want to do. Commented Dec 16, 2017 at 8:08

1 Answer 1

4

Here is an idea that uses two separate images. One image is your Sails utility image and the other will be the actual application.

Utility Image

First, the Sails utility image. This is pretty straightforward. It is just an image based on Node.js with Sails installed. Create the following Dockerfile in a new directory:

FROM node:8.9.3
RUN npm install -g sails
WORKDIR /usr/src/app  
ENTRYPOINT ["sails"]

Then build it:

docker build -t sails .

This could even be a public image. There isn't anything specific to your project here. We are just going to use it to do sails commands.

Application

Now, create another new folder and navigate into it. It will need to be empty. Create the new sails project with a command like:

docker run -it --rm -v $PWD:/usr/src/app sails new .

This will create the Sails project in the current directory using the utility image. This is the root of our new Sails project.

Let's create the Dockerfile and .dockerignore file for this Sails project. For this, we'll just rely on the -onbuild variant of the node image to handle most of the heavy lifting. Create the following Dockerfile:

FROM node:8.9.3-onbuild
EXPOSE 1337

And also the accompanying .dockerignore file:

node_modules

Now we can build the application:

docker build -t myproject

After the application is build we can launch it using:

docker run -p 1337:1337 myproject

The -onbuild variant of the Node.js image will do the npm install for us and has npm start as the default command. The default Sails package.json sets the start script to run the application using node app.js. See https://github.com/nodejs/docker-node/blob/master/8/onbuild/Dockerfile for more information on what it does.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for explaining it, that's great!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.