简体   繁体   English

试图将Sails js项目进行泊坞化

[英]Trying to dockerize a sails js project

I am trying to create a project for sails js using Docker and Docker Compose. 我正在尝试使用Docker和Docker Compose为sails js创建一个项目。 What I want is to generate the sails code inside Docker and generate the structure in the current directory. 我想要的是在Docker内部生成sails代码,并在当前目录中生成结构。

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. 当我尝试生成它时,由于该目录不为空,因为我有一个Dockerfiledocker-compose.yml ,因此出现错误,指出该目录不为空。

This is the Dockerfile : 这是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. 也许此过程是错误的,我应该将其安装为依赖项,然后与docker-compose一个容器一起运行以生成代码,但是我想尝试在非空目录中生成代码也会遇到相同的问题。

Here is an idea that uses two separate images. 这是使用两个单独图像的想法。 One image is your Sails utility image and the other will be the actual application. 一个图像是您的Sails实用程序图像,另一个是实际的应用程序。

Utility Image 实用图片

First, the Sails utility image. 首先,是Sails实用程序映像。 This is pretty straightforward. 这很简单。 It is just an image based on Node.js with Sails installed. 它只是基于安装了Sails的Node.js的映像。 Create the following Dockerfile in a new directory: 在新目录中创建以下Dockerfile

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. 我们将使用它来执行sails命令。

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: 使用以下命令创建新的sails项目:

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. 这将使用实用程序映像在当前目录中创建Sails项目。 This is the root of our new Sails project. 这是我们新的Sails项目的根源。

Let's create the Dockerfile and .dockerignore file for this Sails project. 让我们为此Sails项目创建Dockerfile.dockerignore文件。 For this, we'll just rely on the -onbuild variant of the node image to handle most of the heavy lifting. 为此,我们仅依靠node映像的-onbuild变体来处理大多数繁重的工作。 Create the following Dockerfile : 创建以下Dockerfile

FROM node:8.9.3-onbuild
EXPOSE 1337

And also the accompanying .dockerignore file: 以及随附的.dockerignore文件:

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. Node.js映像的-onbuild变体将为我们执行npm install ,并将npm start作为默认命令。 The default Sails package.json sets the start script to run the application using node app.js . 默认的Sails package.jsonstart脚本设置为使用node app.js运行应用程序。 See https://github.com/nodejs/docker-node/blob/master/8/onbuild/Dockerfile for more information on what it does. 有关其功能的更多信息,请参见https://github.com/nodejs/docker-node/blob/master/8/onbuild/Dockerfile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM