dockerdocker-containerdocker-image

How to install npm packages correctly in docker container


I have a default CI image, assume its name is ci:default, and it can be, and must be run by -d arguments. I want to install some npm packages in it and then use the new image as my customized CI image. Here is how I try to do:

  1. docker pull ci:default

  2. use docker run -it ci:default /bin/bash to run and enter the container.

  3. In that container, use npm i -g XXX to install npm packages I need, and it works.

  4. use apt-get update to save the changes I did to the container.

  5. exit the container, and then use docker commit command to create my customized image, let's name it by ci:customized.

So far so good, as I thought. But when I try to run the new image by using docker run -d ci:customized, it cannot be run successfully, and I don't know when I did wrong since I'm new to docker.

Can somebody please tell me how to achieve my goal?


Solution

  • I don't know what your exact issue is because you didn't post an error message but there is a much better way to create an image with the packages you want by using dockerfiles

    create a file called Dockerfile

    in it you can do something like

    FROM ci:default
    RUN npm install
    

    You can then use docker build to build that in to an image. Check the documentation for docker build but you should just need something like docker build --tag ci:customized

    The benefit of doing it this way is you now have a text file documenting how to set up your image so when you need to modify it later you don't forget what steps were performed before.