This repository contains sample code on how to a Node.js app with Docker and docker-compose.
This repository is part of the Test Node with Docker tutorial series provided by productioncoder.com.
For updates, please reach out to @_jgoebel on Twitter.
You can run the project locally with your own local Postgres instance
npm run dev
Make sure to update the configuration app with your database credentials in the .config/local.json
file.
Running:
docker-compose up
will spin up:
- express server itself (internally port
8080
) forwarded to port8080
on the host machine - Postgres database (internally
5432
), forwarded to port2345
We forward to port 2345
on purpose so it does not collide with a local Postgres instance that might be running on the host machine.
To run in detached mode, use
docker-compose up -d
If you would like to do a clean run (i.e. remove all data in already stopped containers), you can run
docker container prune
to remove all unused containers
As an alternative you can run
docker system prune
to clean up all unused resources. Note that this removes all the unused images, volumens etc. on your host machine.
Note that typing ctrl
+ c
when running docker-compose up
will only stop the container, but not remove it - which is equivalent of running:
docker-compose stop
I.e. if you start up the container again, the database will contain the data you have put in before.
Please refer to the above section on the clean docker-compose section on how to clean up all unused containers.
If you want to to only run the webapp in a docker container, you need to adapt your configuration first.
Since the docker container itself is isolated, setting the database host to localhost
would result in the container trying to connect to itself to connect to the database.
To connect to the database running on your host machine, you need to set the host name to host.docker.internal
.
Change the hostname
field under the db
object inside the ./config.local.json
like so.
{
"db": {
"host": "host.docker.internal"
}
}
You can leave the remaining config as it was.
This is a special DNS entry within the container that allows the container to connect to your host machine. Please refer to the documentation for more details.
Once you have updated your config, you need to build an image and start a container from it.
Execute the following command from the root directory of the project (where the Dockerfile
is located)
docker image build -t productioncoder/test-node-with-docker:local .
After that you can start a container
docker container run -p 8080:8080 productioncoder test-node-with-docker:local
The -p
flag publishes the port from the docker container to your host machine.
Running
npm test
will spin up a Postgres database (internally port 5432
), port-forwarded to port 2345
on the host machine.
It will then run all the tests with mocha
as a test runner and then destroy the containers afterwards.
By testing with a real database, we achieve a high test-fidelity.
You can run the tests with your local Postgres database by running:
npm run test-local
Note that this add data to your local database and that the tests might fail if unique constraints are violated.