Using RabbitMQ with Node.js to offload things to be processed in the background is very useful. Adding Docker and docker-compose to this mix for local development makes it easier to set up RabbitMQ and node.js. In this article, we will explore how to set up RabbitMQ and Node.js using docker and docker-compose using a fake email example, let`s get started! To publish a message, we use NPM`s AMQP library. To configure a Node.js project and install the AMQP library, we run the following commands in the root directory of our folder containing the docker-compose file: When you open localhost:15672/ in a browser, you can access the management interface and now log in with the username and password of the docker-compose set. Therefore, we map volumes so that our persistent queues and logs are not lost when the container is restarted. We then created a bridge network called rabbitmq_nodejs that we will use later when we publish and process the message with node.js code. Please note that we use the default configuration used by the official RabbitMQ Docker image. This means that guest:guest is used for the username and password, including other default settings. To check if our RabbitMQ is working properly, click on localhost:156763 in the browser of our choice.

We should see a long screen as below: OK. With the docker-compose.yml file, we can easily start a RabbitMQ server with our custom definitions and configurations. The command we will use is docker-compose up. A quick way to verify custom definitions is to log in to the administration site as an administrator user (admin:admin123). We should see that the guest user no longer exists on the RabbitMQ server. In this step-by-step tutorial, we have seen how to set up RabbitMQ using Docker and Docker Compose first. Next, we added publisher and consumer code with Node.js. After that, we pasted the Node.js code into a Docker container and inserted it into the existing docker-compose, where the RabbitMQ container was already defined. Then create a file named sendMessage.go in the rabbitmq-go directory.

Add the following: In this article, we will talk about how to quickly set up RabbitMQ instances on Docker. We will go through two ways to run a Docker RabbitMQ image in a container: (1) using the docker run command; (2) Using Docker Compose. Familiarizing yourself with these two approaches should significantly speed up your learning on RabbitMQ. Make sure that the docker rabbitmq container is still running in another shell. Now in a new shell we should be able to run the producer and pass the environment variables: now let`s test the compose file by running the docker-compose up command. When you run docker-compose, the MQ Rabbit container is downloaded and configured. The other code is self-explanatory. You can even try our Node .js with Docker for an Express JS app.

The consumer code is available in this pull request. Therefore, we run the Node.js code in a Docker container. To configure a well-formed Docker file that uses a tiered Docker version that uses the Docker cache to speed up Docker generation, let`s use the following Docker file: This makes sense: localhost no longer resolves in RabbitMQ because we run the producer in a Docker container. The RabbitMQ instance runs in a different container! We could make this work by changing the URL to host.docker.internal, but it`s just as fragile: now we can`t run the producer without docker. Once we try to deploy that remotely, we need to change that again. Now, your docker-compose.yml file is configured to start this RabbitMQ instance with Docker containers. After you complete the installation of Docker on Windows in the previous tutorial, you will learn how to upload a specific image or set of images using the docker pull command. Adding a .dockerigner file next to the manufacturer`s Dockerfile also saves us build time here: in this case, we`ll use docker-compose to configure the container name, volumes and networks, as well as the ports that RabbitMQ will use. This ensures that everything is isolated and easy to change. We use docker compose.yml to set up a RabbitMQ instance. So create a docker-compose.yml file in the desired directory. Once the Docker file is configured, we make some changes to the Docker Compose file to include this Docker file.

The new docker-compose.yml file must look like this to embed the Docker file that runs the consumer: it creates the container for Node.js if it does not exist, and also extracts the RabbitMQ container. It launches both the RabbitMQ Docker container with the management plug-in and the Node.js container, which runs the consumer and returns output that looks like this: Now you know how to set up a Docker image, launch RabbitMQ, dynamically send messages to the queue, and read messages from the queue. Well done! You have everything you need to create amazing apps that send and receive messages effectively.