Junaid Anwar
Junaid Anwar

Reputation: 892

Docker: I can't map ports other than 80 to my WordPress container

I want to map some random port on my computer e.g. localhost:7006 to my WordPress docker container's port 80.When I change the port of WordPress from 80:80 to 7006:80 it's not only stops working on localhost(port 80) but also don't respond on localhost:7006.

docker-compose.yml file looks like this:

        version: '3'
    services:
      wordpress:
        depends_on:
          - db
        image: wordpress:4.7.1
        restart: always
        volumes:
          - ./wp-content:/var/www/html/wp-content 
        environment:
          WORDPRESS_DB_HOST: db:3306
          WORDPRESS_DB_PASSWORD: p4ssw0rd!
        ports:
          - 80:80 # Expose http and https
          - 8443:443
        networks:
          - wp_nwk
      db:
        image: mysql:5.7
        restart: always
        volumes:
           - db_data:/var/lib/mysql
        environment:
          MYSQL_ROOT_PASSWORD: p4ssw0rd!
        networks:
          - wp_nwk
      phpmyadmin:
        depends_on:
          - db
        image: phpmyadmin/phpmyadmin
        restart: always
        ports:
          - 7005:80
        environment:
          PMA_HOST: db
          MYSQL_ROOT_PASSWORD: p4ssw0rd!
        networks:
          - wp_nwk
    networks:
      wp_nwk:
    volumes:
      db_data:

Upvotes: 12

Views: 18012

Answers (3)

Chio Quavario
Chio Quavario

Reputation: 781

I|f you want to change the port, you need to do the follow step. I successfully changed my WordPress port

  1. run WordPress with default docker-compose.yml
version: '3.3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
volumes:
    db_data: {}
  1. login wordpress and change site url and home in setting to you want
  2. use follow command in wordpress container
docker exec -it *wordpres_container_id* bash
  1. add the follow line to wp_config.php
define( 'WP_HOME', 'http://example.com' );
define( 'WP_SITEURL', 'http://example.com' );
  1. change docker-compose.yml ports to 80
  2. restart container use follow command
docker-compose down
docker-compose up -d

Upvotes: 5

Mohammed Ali Ahmed
Mohammed Ali Ahmed

Reputation: 11

you will need to change the [WordPress Address (URL) and Site Address (URL)] from wordpress admin, and then change the port in docker-compose without destroying the data in the volume.

Upvotes: 1

Junaid Anwar
Junaid Anwar

Reputation: 892

After a bit of research I found out that the WordPress container sets it's ports once since it needs to save the URLs(localhost:7006) in the db because I am persisting the db data.

I ran the docker-compose up once with the default port 80:80 configuration which caused the localhost:80 or localhost to be saved in the db. So when I changed the ports again and ran docker-compose up, I actually messed up the URLs that are stored in the linked mysql db container with my WordPress container.

I ran docker-compose down --volumes (this causes the persisted data destruction) and then changed the ports of my WordPress container in docker-compse.yml. Running the following command again created my WordPress container live on port 7006 (localhost:7006). docker-compose up

wordpress:
depends_on:
  - db
image: wordpress:4.7.1
restart: always
volumes:
  - ./wp-content:/var/www/html/wp-content 
environment:
  WORDPRESS_DB_HOST: db:3306
  WORDPRESS_DB_PASSWORD: p4ssw0rd!
ports:
  - 7006:80 # Expose http and https
  - 8443:443
networks:
  - wp_nwk

IMPORTANT: I am just playing with docker, so I don't want to save my volumes data. Anyone wanting to keep their data must not use the docker-compose down --volumes

It's running on the desired port now enter image description here

Upvotes: 21

Related Questions