Docker Npm Run

2/11/2022by admin
  1. Before we can run npm install, we need to get our package.json and package-lock.json files into our images. We use the COPY command to do this. The COPY command takes two parameters. The first parameter tells Docker what file(s) you would like to copy into the image. The second parameter tells Docker where you want that file(s) to be copied to.
  2. May 10, 2021 In this example we'll use Docker Hub, the most popular one. Line 2: Set the working directory in the container to /app. We shall use this directory to store files, run npm, and launch our application: WORKDIR /app. Lines 3-5: Copy the application to the /app directory and install dependencies.
  3. What to do when the ‘RUN npm install’ does not work in the build command? Source: Docker Questions Kafka integration tests in Gradle runs into GitHub Actions How to pass arguments in kubernetes to start a rq-worker container.

It run locally same as in container, i tried with single & but still same:( then i changed in my script to 'npm start && npm run serve' and it just run npm start NOT npm run serve so its very clear that my script is incorrect to run two cmd.

Estimated reading time: 9 minutes

Prerequisites

Work through the steps to build an image and run it as a containerized application in Use your container for development.

Introduction

Testing is an essential part of modern software development. Testing can mean a lot of things to different development teams. There are unit tests, integration tests and end-to-end testing. In this guide we take a look at running your unit tests in Docker.

Create a test

Let’s define a Mocha test in a ./test directory within our application.

Save the following code in ./test/test.js.

Npm

Running locally and testing the application

Let’s build our Docker image and confirm everything is running properly. Run the following command to build and run your Docker image in a container.

Now let’s test our application by POSTing a JSON payload and then make an HTTP GET request to make sure our JSON was saved correctly.

Now, perform a GET request to the same endpoint to make sure our JSON payload was saved and retrieved correctly. The “id” and “createDate” will be different for you.

Install Mocha

Run the following command to install Mocha and add it to the developer dependencies:

Update package.json and Dockerfile to run tests

Okay, now that we know our application is running properly, let’s try and run our tests inside of the container. We’ll use the same docker run command we used above but this time, we’ll override the CMD that is inside of our container with npm run test. This will invoke the command that is in the package.json file under the “script” section. See below.

Below is the Docker command to start the container and run tests:

Multi-stage Dockerfile for testing

In addition to running the tests on command, we can run them when we build our image, using a multi-stage Dockerfile. The following Dockerfile will run our tests and build our production image.

We first add a label as base to the FROM node:14.15.4 statement. This allows us to refer to this build stage in other build stages. Next we add a new build stage labeled test. We will use this stage for running our tests.

Now let’s rebuild our image and run our tests. We will run the same docker build command as above but this time we will add the --target test flag so that we specifically run the test build stage.

Now that our test image is built, we can run it in a container and see if our tests pass.

I’ve truncated the build output but you can see that the Mocha test runner completed and all our tests passed.

This is great but at the moment we have to run two docker commands to build and run our tests. We can improve this slightly by using a RUN statement instead of the CMD statement in the test stage. The CMD statement is not executed during the building of the image but is executed when you run the image in a container. While with the RUN statement, our tests will be run during the building of the image and stop the build when they fail.

Update your Dockerfile with the highlighted line below.

Now to run our tests, we just need to run the docker build command as above.

I’ve truncated the output again for simplicity but you can see that our tests are run and passed. Let’s break one of the tests and observe the output when our tests fail.

Docker Npm Run Test

Open the test/test.js file and change line 5 as follows.

Now, run the same docker build command from above and observe that the build fails and the failing testing information is printed to the console.

Next steps

In this module, we took a look at running tests as part of our Docker image build process.

In the next module, we’ll take a look at how to set up a CI/CD pipeline using GitHub Actions. See:

Feedback

Help us improve this topic by providing your feedback. Let us know what you think by creating an issue in the Docker Docs GitHub repository. Alternatively, create a PR to suggest updates.


Node.js, build, Mocha, test

To pull the latest pre-built docker image:

Tagged Versions#

Since version v2.x you can pull docker images by tag, as follows:

For a major version:

For a minor version:

For a specific (patch) version:

If you are interested on a list of tags, please visit the Docker Hub website.

Running Verdaccio using Docker#

To run the docker container:

The last argument defines which image to use.The above line will pull the latest prebuilt image from dockerhub, if you haven't done that already.

If you have build an image locally use verdaccio as the last argument.

You can use -v to bind mount conf, storage and plugins to the hosts filesystem:

if you are running in a server, you might want to add -d to run it in the background

Note: Verdaccio runs as a non-root user (uid=10001) inside the container, if you use bind mount to override default,you need to make sure the mount directory is assigned to the right user. In above example, you need to run sudo chown -R 10001:65533 /path/for/verdaccio otherwiseyou will get permission errors at runtime.Use docker volume is recommended over using bind mount.

Verdaccio 4 provides a new set of environment variables to modify either permissions, port or http protocol. Here the complete list:

PropertydefaultDescription
VERDACCIO_APPDIR/opt/verdacciothe docker working directory
VERDACCIO_USER_NAMEverdacciothe system user
VERDACCIO_USER_UID10001the user id being used to apply folder permissions
VERDACCIO_PORT4873the verdaccio port
VERDACCIO_PROTOCOLhttpthe default http protocol

Docker Npm Run Watch

Docker npm run start

SELinux#

If SELinux is enforced in your system, the directories to be bind-mounted in the container need to be relabeled. Otherwise verdaccio will be forbidden from reading those files.

If verdaccio can't read files on a bind-mounted directory and you are unsure, please check /var/log/audit/audit.log to confirm that it's a SELinux issue. In this example, the error above produced the following AVC denial.

chcon can change the labels of shared files and directories. To make a directory accessible to containers, change the directory type to container_file_t.

If you want to make the directory accessible only to a specific container, use chcat to specify a matching SELinux category.

Dev

An alternative solution is to use z and Z flags. To add the z flag to the mountpoint ./conf:/verdaccio/conf simply change it to ./conf:/verdaccio/conf:z. The z flag relabels the directory and makes it accessible by every container while the Z flags relables the directory and makes it accessible only to that specific container. However using these flags is dangerous. A small configuration mistake, like mounting /home/user or /var can mess up the labels on those directories and make the system unbootable.

Plugins#

Plugins can be installed in a separate directory and mounted using Docker or Kubernetes, however make sure you build plugins with native dependencies using the same base image as the Verdaccio Dockerfile.

Docker and custom port configuration#

Any host:port configured in conf/config.yaml under listenis currently ignored when using docker.

If you want to reach Verdaccio docker instance under different port, lets say 5000in your docker run command add the environment variable VERDACCIO_PORT=5000 and then expose the port -p 5000:5000.

Of course the numbers you give to the -p parameter need to match.

Using HTTPS with Docker#

You can configure the protocol verdaccio is going to listen on, similarly to the port configuration.You have to overwrite the default value('http') of the PROTOCOL environment variable to 'https', after you specified the certificates in the config.yaml.

Using docker-compose#

Docker Npm Run
  1. Get the latest version of docker-compose.
  2. Build and run the container:

You can set the port to use (for both container and host) by prefixing the above command with VERDACCIO_PORT=5000 .

Docker will generate a named volume in which to store persistent application data. You can use docker inspect or docker volume inspect to reveal the physical location of the volume and edit the configuration, such as:

Build your own Docker image#

There is also an npm script for building the docker image, so you can also do:

Note: The first build takes some minutes to build because it needs to run npm install,and it will take that long again whenever you change any file that is not listed in .dockerignore.

Please note that for any of the above docker commands you need to have docker installed on your machine and the docker executable should be available on your $PATH.

Docker Examples#

There is a separate repository that hosts multiple configurations to compose Docker images with verdaccio, for instance, as reverse proxy:

Docker Custom Builds#

If you have made an image based on Verdaccio, feel free to add it to this list.

  • docker-verdaccio-multiarch Multiarch image mirrors
  • docker-verdaccio-s3 Private NPM container that can backup to s3
  • coldrye-debian-verdaccio docker image providing verdaccio from coldrye-debian-nodejs.
Comments are closed.