Search code examples
node.jsdockerdocker-imageazure-container-instancestermination

Docker image "node" terminates before I can access it (using azure cli)


I try to deploy my image that is based on node (node:latest) on azure. When I do it terminates automatically and does not let me do what I need to do with it.

My docker file:

WORKDIR /usr/src/app
COPY package.json .
COPY artillery-scripts.sh .
COPY images images
COPY src src
EXPOSE 80
RUN npm install -g artillery && \
    npm install faker && \
    npm install worker && \
    npm install -g node-fetch -save && \
    npm install -g https://github.com/preguica/artillery-plugin-metrics-by-endpoint.git
    

I have tried adding && \ while true; do echo SLEEP; sleep 10; done at the end so it wouldn't terminate automatically but that produces an error.

Any one know what this problem is?


Solution

  • Probably good to first try it all locally. It seems you misunderstand some fundamental parts of docker. Writing something that will pause in your Dockerfile makes no sense at all, since that file is for building the image, not running the container.

    Once you have the image, you can run one or more containers based on this image. Usually you will want to put a CMD or ENTRYPOINT at the end that will tell the container what command to run. Read this article which gives a pretty good explanation of both.

    If you want to interact with the container look into the -i and -t (or short -it) flags of the run command. When you run your container, you can also provide a command, this will override any command given in CMD or be appended to anything in ENTRYPOINT. If you do not write an ENTRYPOINT or CMD it will default to running a shell. However, if you run it without -it it will start the shell, consider it's work done and stop immediately.

    Again if you would want to start a specific script for instance you can add a line to the end of your Dockerfile such as

    CMD "node somefile.js"
    

    So first build your image based on the dockerfile, then run the container based on the image:

    docker build -t someImageName:someTag .
    docker run -it someImageName:someTag // will run CMD, "node somefile.js" or:
    docker run -it someImageName:someTag node // will override it and just run node 
    

    You can install docker locally and just do that all on your local machine, and once you get a feel for it, and once you are sure your dockerfile is correct see how to deploy it to azure. That way it is easier to debug and learn.

    Extra tip: you wrote EXPOSE 80. Read the docs on EXPOSE and PUBLISH beacuse it can be confusing when you start out. EXPOSE is just there for documentation, it does NOT actually expose anything. If you would like to connect somehow to the container from the outside world you have to PUBLISH the port. This is done in the run command:

    docker run -it someImageName:someTag -p 80:80 // the first is host port, the second is the container port.