Can't setup an email account on mail app, when both the email and nextcloud dockers are on the same docker network

Hello all.
I have a nextcloud docker and a poste.io mail server docker running on my ubuntu server. Both dockers are on the same docker network. I am not able to setup my poste email-account on the mail app of nextcloud. I am getting the error[mail] Error: Creating account failed: Connection to IMAP at imap.*****.com:143 failed. Error connecting to mail server. I have already tried other ports and security protocols. But none works. There might be a problem of communication between the apps in the docker environment.
Poste mail server and mail app of nextcloud are working perfectly individually. I managed to setup a poste email account on thunderbird and it works. I tried to add a gmail account to the mail app of the nextcloud and it works. But my poste mail server and nextcloud does not seem to work together.
I experienced similar problems when I tried to set up the SMTP server on nextcloud using the same email server, I have the following error: A problem occurred while sending the email. Please revise your settings. (Error: Connection could not be established with host smtp.*******.com :stream_socket_client(): unable to connect to ssl://smtp.***********.com:465 (Connection timed out))
Any suggestions will be appreciated. Please ask me if I need to provide more info. Thanks in advance.