I previously had a Docker Swarm orchestration with 6 nodes. After adding a new Nginx to the orchestration, a connection issue arose between the ports of the nodes’ services. Below, I am sharing an exact example of my orchestration.
You can follow the steps below to install Docker on your Amazon Linux server:
- Update Your Server:
- First, update the existing packages.
sudo yum update -y
- Install the Docker Package:
- Install Docker from the repository for Amazon Linux 2.
sudo amazon-linux-extras install docker -y
sudo service docker start
- Set Up Automatic Start:
- To start Docker automatically when the server starts:
sudo systemctl enable docker
- Add User to Docker Group:
- This step is necessary if you want to run Docker commands without sudo. For example, to add the “ec2-user” user to the Docker group:
sudo usermod -a -G docker ec2-user
newgrp docker
Create docker swarm from first aws node
sudo docker swarm init --advertise-addr <IP_ADDRESS>
elastic ip available elastic ip
this is the part that comes up in amazon, privilege required
After added second as worked to swarm, We name the nodes by service name
docker node update <1st node> --label-add service=hello
docker node update <2nd node> --label-add service=nginx
We create overlay network
docker network create -d overlay --opt com.docker.network.driver.mtu=1450 nginx
First node will be hello-service
Copy code
sudo nano
const express = require('express');
const app = express();
const port = 3000;
app.get('/hello', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`App is running at http://localhost:${port}`);
});
save app.js
Copy code package.json
sudo nano
{
"name": "express-app",
"version": "1.0.0",
"description": "A simple Express.js application",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^4.17.1"
}
}
save package.json
sudo nano
# Get a Node.js-based image
FROM node:latest
# Create a working directory where we can run the application code
WORKDIR /usr/src/app
# Install application dependencies
COPY package*.json ./
RUN npm install
# Copy the application code
COPY . .
# Run the application
CMD [ "node", "app.js" ]
save Dockerfile
- Now, build the Docker image using the Dockerfile. Run the following command in the terminal in the directory containing this file and your application files:
Copy code
docker build -t express-app .
docker service create --name hello-service --network nginx --constraint 'node.labels.service==hello' --publish 3000:3000 express-app:latest
After create hello-service we create nginx-service
docker service create --name hello-service --network nginx --constraint ‘node.labels.service==hello’ --publish 3000:3000 express-app:latest
sudo nano
worker_processes 1;
events { worker_connections 1024; }
http {
resolver 127.0.0.11 valid=10s;
server {
listen 80;
location / {
default_type text/plain;
return 200 'Hello World!';
}
location ~ ^/hello-service {
rewrite ^/hello-service/(.*) /$1 break;
proxy_pass http://hello-service:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'Upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}
save nginx.conf
- Then, create a file named Dockerfile and paste the following content into it:
Dockerfile
Copy code
sudo nano
# Get an Nginx-based image
FROM nginx:latest
# Copy custom Nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf
save Dockerfile
- Build the Docker image using the Dockerfile. Run the following command in the terminal in the directory containing this file and the Nginx configuration file:
bash
Copy code
docker build -t mynginx -f Dockerfile .
- You can use the following command to start the Docker image:
bash
Copy code
docker service create --name nginx-service --network nginx --constraint 'node.labels.service==nginx' --publish 80:80 mynginx:latest
Now problem is when we try to get request nginx node ip4 address, response is coming. When we ping inside nginx container to other node inside helle-service container by service-name, It’s working hostname resolver. But when we try to connect port, problem is starting
Do you have an any idea?