Search code examples
dockerjenkinsnetworkingmailhog

Connect Docker container to machine's localhost


I'm using Jenkins for the first time and have chosen to use its Docker image instead of installing it locally. I used this Docker compose:

services:
  jenkins:
    image: jenkins/jenkins
    ports:
      - "127.0.0.1:8080:8080"
    volumes:
      - jenkins_home:/var/jenkins_home 
    restart: unless-stopped
    

volumes:
  jenkins_home:

I am trying to test the emailing features and for that have needed to use MailHog, which I also used as a Docker images, using the Docker compose below:

services:
  email:
    image: mailhog/mailhog
    platform: linux/amd6
    ports:
      - "127.0.0.1:1025:1025" # SMTP
      - "127.0.0.1:8025:8025" # web UI
    restart: unless-stopped

Now I have configured Jenkins to use SMTP and the port 1025: enter image description here enter image description here Except when I try to test this, I get the following error: enter image description here

I think that since Jenkins is on a Docker container, it looks for its own localhost, maybe? I tried to do an ipconfig and get the IP address of my machine to replace localhost but it didn't work. Do you have any idea how to solve this please? and thank you!


Solution

  • You can combine two service in one docker-compose file, and they(two container) can find each other just use service name.

    Therefore, you can set 'email' as SMTP SERVER.

    services:
      jenkins:
        image: jenkins/jenkins
        ports:
          - "8080:8080"
        volumes:
          - jenkins_home:/var/jenkins_home 
        restart: unless-stopped
      email:
        image: mailhog/mailhog
        platform: linux/amd6
        ports:
          - "1025:1025" # SMTP
          - "8025:8025" # web UI
        restart: unless-stopped
    volumes:
      jenkins_home:
    

    I tried to do an ipconfig and get the IP address of my machine to replace localhost but it didn't work.

    If you wanna use IP address of your machine, you should remove 127.0.0.1 of port configuration.