Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Docker Compose FIle For Wordpress, MySQL & phpmyadmin

Wordpress & Docker

This file will setup Wordpress, MySQL & PHPMyAdmin with a single command. Add the code below to a file called "docker-compose.yaml" and run the command

$ docker-compose up -d

# To Tear Down
$ docker-compose down --volumes
version: '3'

services:
  # Database
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - wpsite
  # phpmyadmin
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - '8080:80'
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: password 
    networks:
      - wpsite
  # Wordpress
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - '8000:80'
    restart: always
    volumes: ['./:/var/www/html']
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    networks:
      - wpsite
networks:
  wpsite:
volumes:
  db_data:
@petercr

This comment has been minimized.

Copy link

petercr commented Jan 22, 2019

Cool I can't wait to try this one out. Sounds better than setting up XAMPP and then WordPress just to make a site.
Thanks 😄 👍

@bknightly

This comment has been minimized.

Copy link

bknightly commented Jan 25, 2019

Big thanks @bradtraversy, this did the trick!

@adityakmr7

This comment has been minimized.

Copy link

adityakmr7 commented Feb 11, 2019

how can i start this. as i have composed it but it is not accessible to localhost:8000.

@ghost

This comment has been minimized.

Copy link

ghost commented Feb 14, 2019

how can i start this. as i have composed it but it is not accessible to localhost:8000.

You should after you installed Docker Compose. Then create a docker-compose.yml file. Copy the content from the above Gist file and paste them there. Run docker-compose up -d and wait until everything is completed. Then go to 127.0.0.1:8000.

@clissmancamacho

This comment has been minimized.

Copy link

clissmancamacho commented Feb 17, 2019

Thanks Brad! You are wonderfull!

@steffik02

This comment has been minimized.

Copy link

steffik02 commented Feb 20, 2019

how can i start this. as i have composed it but it is not accessible to localhost:8000.

I had the same problem. I had to run docker-machine ip to get the IP address, then instead of localhost:8000, I used <docker.ip.here>:8000

@david-j-davis

This comment has been minimized.

Copy link

david-j-davis commented Mar 1, 2019

No matter what setup I use, I cannot get localhost:8000 or to work. I've tried several tutorials for wordpress with docker, and every time I get ERR_CONNECTION_REFUSED. I don't get what the issue is. My docker log error is: Access denied for user 'wordpress'@'172.18.0.3' (using password: YES) and I've followed along with this tutorial exactly.

@galvakojis

This comment has been minimized.

Copy link

galvakojis commented Mar 4, 2019

@david-j-davis

No matter what setup I use, I cannot get localhost:8000 or to work. I've tried several tutorials for wordpress with docker, and every time I get ERR_CONNECTION_REFUSED. I don't get what the issue is. My docker log error is: Access denied for user 'wordpress'@'172.18.0.3' (using password: YES) and I've followed along with this tutorial exactly.

If this will not work i post full yml file


# Wordpress
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress

# phpmyadmin
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

# Database
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
@devzons

This comment has been minimized.

Copy link

devzons commented Mar 22, 2019

It already used by the other app. just change 8000 to 8800(or some other number). It works for me. Let me know if I'm wrong. Thanks

@vulcan25

This comment has been minimized.

Copy link

vulcan25 commented Mar 29, 2019

@bradtraversy This gist uses two different methods of mounting a volume.

The db service defines a named volume db_data, which sits on the host at/var/lib/docker/volumes.

However the wordpress service defines a host path, which is actually mounting the current directory on the host, which is where the compose file (with credentials) likely is:

volumes: ['./:/var/www/html']

If you save this gist as docker-compose.yml and run docker compose up the compose file allows itself to be downloaded by anyone at: http://localhost:8000/docker-compose.yml.

For more info: compose volume docs. Unless I'm missing something, this should really be updated so that the wordpress volume is defined in a similar manner to db_data.

@burkaslarry

This comment has been minimized.

Copy link

burkaslarry commented Mar 30, 2019

If I want to deploy to heroku or other web hosting, how do i change my database configuration so that No error in establishing the database connection?

@tbanys

This comment has been minimized.

Copy link

tbanys commented Apr 27, 2019

Just be careful with docker-compose down --volumes when you tear down the container. --volumes part will delete database and files of your project. Use docker-compose down without --volumes, then you won't lose files and data.

@Waka867

This comment has been minimized.

Copy link

Waka867 commented May 20, 2019

Thanks! @bradtraversy

@SipTech

This comment has been minimized.

Copy link

SipTech commented May 21, 2019

Thank you @bradtraversy

I also want to add that if you want to spin up mysql from backups/dumps you can use following:

  # Database
  db:
    image: mysql:5.7
    volumes:
      - ./mysql-dump:/docker-entrypoint-initdb.d
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: your_db
      MYSQL_USER: your_user
      MYSQL_PASSWORD: your_password
    networks:

Use mysql-dump to export you data into a self contained backup.sql file.
Create a folder in your project directory, save your backup.sql file(s) there.
When docker-compose runs it will restore your databases in the mysql container.

@SipTech

This comment has been minimized.

Copy link

SipTech commented May 23, 2019

@bradtraversy how do I shutdown/restart docker without losing my data?

@zoechi

This comment has been minimized.

Copy link

zoechi commented Jun 17, 2019

@SipTech add

    volumes:
      - ./db_data:/var/lib/mysql

to db:
Then the Database will be created on the local file system in ./db_data.
You can use docker cp ... to copy files from the Docker volume to the local directory (before you add above code to docker-compose.yml).

@delriodev

This comment has been minimized.

Copy link

delriodev commented Jul 8, 2019

Hello! I'm new to programming!

I also had the problem described above: localhost:8000 doesn't work, so I did <docker.ip.here>:8000 like @steffik02 said and it worked

Then, inside wp-admin, I had trouble with plug in installation because of FTP permission. The solution was to add the following to my wp-config.php file :

define( 'FS_METHOD', 'direct');

But now, when I try to install a plugin I get: "Installation failed: Could not create directory."

I ran a similar project in an ubuntu VM and it worked perfectly,
I am now using a Mac and I'm having all these problems

@tdse13

This comment has been minimized.

Copy link

tdse13 commented Jul 12, 2019

One question: how to update the container? Does Watchtower work? Thank you.

@paultofunmi

This comment has been minimized.

Copy link

paultofunmi commented Aug 24, 2019

Hi, can someone help with the Dockerfile.

@Ashleybrandon

This comment has been minimized.

Copy link

Ashleybrandon commented Aug 29, 2019

Can you use this method to set up multiple wordpress instances locally?

@goodguy000

This comment has been minimized.

Copy link

goodguy000 commented Oct 7, 2019

Can you use this method to set up multiple wordpress instances locally?

docker compose 1: use wordpress port: 8000, phpadmin: 8080, network: wpsite
docker compose 2: use wordpress port: 8001, phpadmin: 8081, network: wpsite1

@zeitgeist2018

This comment has been minimized.

Copy link

zeitgeist2018 commented Oct 9, 2019

This just works out of the box. Many thanks for your collaboration to the community, we really appreciate your labor!

@sharker92

This comment has been minimized.

Copy link

sharker92 commented Oct 23, 2019

hey, why you use square brackets and single quotes here: volumes: ['./:/var/www/html']?

@kelwinsj

This comment has been minimized.

Copy link

kelwinsj commented Oct 27, 2019

Works like a charm. Thank you Brad.

@vulcan25

This comment has been minimized.

Copy link

vulcan25 commented Nov 4, 2019

@sharker92 see my earlier comment, this is VULNERABLE if deployed in its current form

Anyone who deploys this straight into production is making the compose file accessible at example.com/docker-compose.yml along with the db credentials, unless you take steps to prevent this.

@trung1704ptit

This comment has been minimized.

Copy link

trung1704ptit commented Nov 6, 2019

Hi everyone, How can we backup wordpress by using this docker compose.
I tried to backup by using plugin, but when restore it take too long for response, something still loading.
Anyone have solution, please help me

@krisnagunarno

This comment has been minimized.

Copy link

krisnagunarno commented Nov 28, 2019

Hello everyone, I've a trouble to make this kind of docker compose when I try to make the credentials "stored"(or encrypted? I don't really understand) in docker secret. Any solutions?

@guidosantillan01

This comment has been minimized.

Copy link

guidosantillan01 commented Dec 10, 2019

Hi everyone! I am having a problem when I want to install the plugins. Everything else seems to be ok.

image

Did anyone have the same error?

@shahabbukhari

This comment has been minimized.

Copy link

shahabbukhari commented Jan 14, 2020

I'm getting an "Error establishing a connection to database" when I browse to localhost:8000.
Help

@Alhamou

This comment has been minimized.

Copy link

Alhamou commented Jan 14, 2020

thank you pro, keep coning :)

@edrichmkhasi

This comment has been minimized.

Copy link

edrichmkhasi commented Jan 16, 2020

hi.

i'm working on a project where i'm using mongoDB as my database and nodejs.... i'm trying to create a docker image and connect to my project.... but it keeps giving me an error (/usr/local/bin/docker-entrypoint.sh: 8: exec: .: Permission denied) please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.