Alistair
Alistair

Reputation: 641

Swagger express gives error inside docker container

I have an app that uses the swagger-express-mw library and I start my app the following way:

SwaggerExpress.create({ appRoot: __dirname }, (err, swaggerExpress) => {  
// initialize application, connect to db etc...
}

Everything works fine on my local OSX machine. But when I use boot2docker to build an image from my app and run it, I get the following error:

/usr/src/app/node_modules/swagger-node-runner/index.js:154
config.swagger = _.defaults(readEnvConfig(),
             ^

TypeError: Cannot assign to read only property 'swagger' of [object Object]

My dockerfile looks like this (nothing special):

FROM node:latest

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

COPY package.json /usr/src/app/

RUN npm set progress=false
RUN npm install

COPY . /usr/src/app

EXPOSE 5000

CMD ["npm", "run", "dev"]

Has anyone else encountered similar situations where the local environment worked but the app threw an error inside the docker container?

Thanks!

Upvotes: 0

Views: 843

Answers (1)

CtheGood
CtheGood

Reputation: 1019

Your issue doesn't appear to be something wrong with your docker container or machine setup. The error is not a docker error, that is a JavaScript error.

The docker container appears to be running your JavaScript module in Strict Mode, in which you cannot assign to read-only object properties (https://msdn.microsoft.com/library/br230269%28v=vs.94%29.aspx). On your OSX host, from the limited information we have, it looks like it is not running in strict mode.

There are several ways to specify the "strictness" of your scripts. I've seen that you can start Node.js with a flag --use_strict, but I'm not sure how dependable that is. It could be that NPM installed a different version of your dependent modules, and in the different version they specify different rules for strict mode. There are several other ways to define the "strictness" of your function, module, or app as a whole.

You can test for strict mode by using suggestions here: Is there any way to check if strict mode is enforced?.

So, in summary: your issue is not inheritantly a docker issue, but it is an issue with the fact that your javascript environments are running in different strict modes. How you fix that will depend on where strict is being defined.

Hope that helps!

Upvotes: 1

Related Questions