0

I am trying to containerized a sample react.js app inside docker.

My docker file is as follow and I am in the working directory of the project

FROM node:10-alpine
RUN mkdir -p /var/tmp/thermodocker && chown -R root:root /var/tmp/thermodocker
WORKDIR /var/tmp/thermodocker
COPY * ./
USER root
RUN [ "npm", "install","--python","python2" ]
EXPOSE 3000
CMD [ "npm", "run" ]

I can successfully create image

[root@******]# docker build -t thermo-api .
...
Successfully built c6697819c3e1
Successfully tagged thermo-api:latest

Hwoever, when I want to run the application, it doesn't come up as a running docker image

[root@******]# docker run -d -p 3000:3000 --name thermo-api-app thermo-api
51b873637ac12ee2a89d075edf1953c29b41826b6b3847659d7763d42516916a
INFO[2020-07-29T01:01:30.555414295Z] shim containerd-shim started                  address="/containerd-shim/moby/51b873637ac12ee2a89d075edf1953c29b41826b6b3847659d7763d42516916a/shim.sock" debug=false pid=8021
[root@********]# INFO[2020-07-29T01:01:31.081201052Z] shim reaped                                   id=51b873637ac12ee2a89d075edf1953c29b41826b6b3847659d7763d42516916a
INFO[2020-07-29T01:01:31.088559217Z] ignoring event                                module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
[root@******]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@thermo-breast-cancer-devvm1 thermo-api]#

I am not sure where I am doing mistake?

[PS:] Per comments I have changed the CMD to

RUN [ "npm", "install","--python","python2" ]

however I can not build image

Step 6/8 : RUN [ "npm", "install","--python","python2" ]
 ---> Running in 77155726f35e
INFO[2020-07-29T01:35:38.577900480Z] shim containerd-shim started                  address="/containerd-shim/moby/77155726f35ed6f7464d6e801d767221d62e0e31fd7e342835b6d861e3694b57/shim.sock" debug=false pid=8717
npm ERR! code EAI_AGAIN
npm ERR! errno EAI_AGAIN
npm ERR! request to https://registry.npmjs.org/python2 failed, reason: getaddrinfo EAI_AGAIN registry.npmjs.org registry.npmjs.org:443

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-07-29T01_35_44_304Z-debug.log
INFO[2020-07-29T01:35:44.354721669Z] shim reaped                                   id=77155726f35ed6f7464d6e801d767221d62e0e31fd7e342835b6d861e3694b57
INFO[2020-07-29T01:35:44.361799117Z] ignoring event
Mehdi
  • 133
  • 1
  • 12
  • 1
    FWIW, for development builds, a typical pattern is to copy just the package.json in the first layer, then run npm install, and then copy the source in a subsequent layer, this means that the npm install doesn't have to run every time you change source. See [Dockerizing a Node.js web app](https://nodejs.org/en/docs/guides/nodejs-docker-webapp/#creating-a-dockerfile) – Wyck Jul 29 '20 at 01:32
  • Hi, I have changed CMD to RUN, but I am getting new error when building the image! Do I need to bundle python into image as well? – Mehdi Jul 29 '20 at 01:41
  • What (if anything) is in the container logs; try running `docker logs thermo-api-app` or repeating the `docker run` command without the `-d` option? What benefits are you hoping to get from Docker over just directly running Node here? – David Maze Jul 29 '20 at 11:05
  • Please be more careful how you edit your question. Your question now says both: 1) "I can successfully create image" AND 2) "however I can not build image". I can only imagine that your build is failing on `npm install`. Just omit that step (`tail -f /dev/null` is a reasonable do-nothing CMD to keep the container alive while you are troubleshooting), then and exec into the container and run the npm install command yourself interactively to troubleshoot. You will likely feel more familiar and comfortable debugging this way. – Wyck Jul 29 '20 at 15:16

1 Answers1

0

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

You likely meant for your npm install to be a RUN instruction, not a CMD.

As is, it only does npm run without ever having done npm install, and therefore likely immediately exits with an error.

Wyck
  • 10,311
  • 6
  • 39
  • 60
  • I fixed it but I am geeting new error when I run npm run, I have edited the question... – Mehdi Jul 29 '20 at 01:45
  • You need some [basic docker debugging skills](https://stackoverflow.com/questions/26220957/how-can-i-inspect-the-file-system-of-a-failed-docker-build). – Wyck Jul 29 '20 at 15:19