Docker connect Mail catcher with WordPress

I want to set a sendmail_path in WordPress’ container and use a sendmail provided by another container. In my case its MailHog.

So this is my docker-compose:

  • How to Ssh into docker container that is running inside Vagrant?
  • Docker compose v3: The difference between volume type mount and bind
  • Dockerfile: command not found
  • Unable to access Container url in Bluemix for Spring Boot Application
  • Can't get clipboard forwarding working, while being in Docker under SSH
  • Issue in building docker image using habitus
  • version: '2'
    services:
        wordpress:
            image: wordpress
            links:
                - db:mysql
                - mailhog
            ports:
                - 80:80
            domainname: foo.com
            hostname: foo
            volumes:
                - ./public:/var/www/html
            environment:
                WORDPRESS_DB_PASSWORD: example
            depends_on:
               - mailhog
    
        mailhog:
            image: mailhog/mailhog
            ports:
                - 1025:1025
                - 8025:8025
    
        db:
            image: mariadb
            environment:
                MYSQL_ROOT_PASSWORD: example
    

    I tried executing the command: "echo 'sendmail_path = \"/usr/local/bin/mailhog sendmail\"' > /usr/local/etc/php/conf.d/mail.ini" on WordPress container but it actually prints it…

    Does these two have to share the volumes?

    PS. I know I can use it as a SMTP server in the APP but I want to deal with it in more automated way.

  • How to check if the docker image has all the files?
  • Not able to connect cassandra port 9042 from other machines.
  • Why is inter container communication in Docker the default?
  • process.env.VARIABLE is undefined in webpack/docker/heroku deployment
  • difference between docker attach and docker exec
  • Docker Service Update Error: EOF
  • One Solution collect form web for “Docker connect Mail catcher with WordPress”

    You don’t have mailhog installed in the wordpress container, the path /usr/local/bin/mailhog doesn’t exit.

    What you want to do is sending mail via sendmail and this mail must be catched by mailhog. To do this you must extend the wordpress dockerfile :

    FROM wordpress
    RUN curl --location --output /usr/local/bin/mhsendmail https://github.com/mailhog/mhsendmail/releases/download/v0.2.0/mhsendmail_linux_amd64 && \
        chmod +x /usr/local/bin/mhsendmail
    
    RUN echo 'sendmail_path="/usr/local/bin/mhsendmail --smtp-addr=mailhog:1025 --from=no-reply@docker.dev"' > /usr/local/etc/php/conf.d/mailhog.ini
    

    Note the --smtp-adrr parameter : it must be in the form <mailhog_hostname>:<mailhog_port>.

    Change your docker-compose.yml to build your dockerfile.

    version: '2'
    services:
        wordpress:
            build:
                context: ./
                dockerfile: ./Dockerfile
            links:
                - db:mysql
                - mailhog
            ports:
                - 80:80
            domainname: foo.com
            hostname: foo
            volumes:
                - ./public:/var/www/html
            environment:
                WORDPRESS_DB_PASSWORD: example
            depends_on:
               - mailhog
    
        mailhog:
            image: mailhog/mailhog
            ports:
                - 1025:1025
                - 8025:8025
    
        db:
            image: mariadb
            environment:
                MYSQL_ROOT_PASSWORD: example
    

    In this exemple, the dockerfile you have written must be named “Dockerfile” and must be in the current directory (where you run docker-compose). You can change the path accordingly. You can remove the 1025:1025 ports entry if you don’t need to connect to it from the host.

    Now the function mail() should work as intended.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.