Docker: I can't map ports other than 80 to my WordPress container
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: - dbimage: wordpress:4.7.1restart: alwaysvolumes: - ./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:443networks: - 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
I|f you want to change the port, you need to do the follow step. I successfully changed my WordPress port
- 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: wordpressvolumes: db_data: {}
- login wordpress and change site url and home in setting to you want
- use follow command in wordpress container
docker exec -it *wordpres_container_id* bash
- add the follow line to
wp_config.php
define( 'WP_HOME', 'http://example.com' );define( 'WP_SITEURL', 'http://example.com' );
- change
docker-compose.yml
ports to 80 - restart container use follow command
docker-compose downdocker-compose up -d
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.