5

I can send mails from host, using mail:

mail -s "Hooray" smb@example.com < /dev/null

But I want to send mails from docker container using host server. Docker says "port is already in use" when I try to map it to 25 port in run command:

run -ti -p 25:25 container

How I can achieve the goal? Host is Centos, docker uses Ubuntu.

Snobby
  • 1,067
  • 3
  • 18
  • 38

2 Answers2

-1

Using port forwarding with docker container you forward container's port to host. So you have port 25 already in use on host by mail server. Here you need to forward port from host to the container:

Forward host port to docker container

The easiest way is to use --net=host option:

docker run --rm -it --net=host container mail -s "Hooray" smb@example.com < /dev/null
Community
  • 1
  • 1
oryades
  • 364
  • 2
  • 5
  • Maybe there is a way to do this without using --net=host? I am using several other containers in network also. – Snobby Feb 07 '17 at 09:23
-1

You can't listen on the same port with multiple applications. So if there's already an application on the host listening on port 25, you can either stop that application, or configure your container to listen on a different host port, e.g.:

run -ti -p 2525:25 container

That causes the port to be mapped from the host port 2525 to the container port 25. If you don't need to receive mail from the container, you can remove this port mapping entirely, that will still allow you to send outbound messages.

If you don't know what is using port 25 on the host, you can look this up with a netstat command:

sudo netstat -lntp

Note that the sudo is required if you want to see the process that is listening on the port.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • I want to send mails (don't want to recieve them at all) from container, so I don't need to map ports at all? – Snobby Feb 07 '17 at 15:39
  • If you don't need to receive mail from the container, you can remove this port mapping entirely, that will still allow you to send outbound messages. – BMitch Feb 07 '17 at 15:40