| by Arround The Web | No comments

How to Fix “Docker Network Connection Refused” Errors When Running Docker Containers in AWS EC2

Docker is a great tool for deploying, and running applications by utilizing the containers and “AWS EC2” corresponds to a cloud computing platform used to manage large-scale data processing tasks. However, there can be a possibility of facing the “Connection Refused” error while trying to execute Docker containers in AWS EC2.

In this blog, we will talk about:

What are the Reasons for the “Docker Network Connection Refused” Errors When Executing Docker Containers in AWS EC2?

The “Connection Refused” limitation is faced when the server being connected is not accepting connections on the target port. This can be due to the below-listed causes:

    • Incorrect configuration.
    • Firewall restrictions.
    • Network issues.

How to Check Network Connectivity in Docker?

To analyze the network connectivity and log detailed information on one or more networks, apply the following cmdlet:

docker network inspect [OPTIONS] NETWORK [NETWORK...]

 
After executing this cmdlet gives all the outcomes as a JSON object.

How to Resolve the “Docker Network Connection Refused” Errors When Running/Executing Docker Containers in AWS EC2?

The stated limitation can be fixed via the below-listed approaches:

Fix 1: Analyze Docker Container Configuration

The first step in resolving the limitation is to ensure that the Docker container is configured properly via the following Dockerfile:

FROM python:3.7
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]

 
In the above Dockerfile, the “EXPOSE” directive notifies Docker that the container listens on the target network port. Also, make sure that the port number is identical to the one in the app.

Fix 2: Check Network Accessibility

Another approach to resolve the discussed limitation or resolving the “Network Connectivity” issues can be to analyze the network accessibility. To do so, apply the below-stated “netstat” cmdlet syntax to check if the application is listening on the correct port:

docker exec -it <container’s id> netstat -tuln

 
Demonstration


The above cmdlet lists all the ports that the Docker container (having the specified id) is listening on. Also, make sure that your application port is listed here.

Fix 3: Inspect Docker Logs

Docker logs can log vital information regarding the container, thereby assisting in resolving the discussed network connection refused error. Therefore, inspect the logs via the following “docker logs” cmdlet syntax:

docker logs <container's ID>

 
Demonstration


Here, “d307b503b39d” corresponds to the container’s ID.

After executing the cmdlet, analyze any error messages related to the application startup or network configuration.

Fix 4: Test the Connection Locally

If the “Docker Network Connection Refused” error still persists, try executing the Docker container locally. It is such that if it functions on the local machine but not on “AWS EC2”, the error can be with the EC2 instance or with the network between the local machine and AWS.

Conclusion

The “Connection Refused” limitation while running containers in “AWS EC2” is faced when the server being connected is not accepting connections on the target port. It can be resolved by analyzing the Docker container configuration, checking network accessibility, inspecting Docker logs, or testing the connection locally.

Share Button

Source: linuxhint.com

Leave a Reply