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:

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

docker run, A stopped container can be restarted with all its previous changes intact using docker start . See docker ps -a to view a list of all containers. The docker run� Shop Devices, Apparel, Books, Music & More. Free Shipping on Qualified Orders.

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

Legacy container links, This would bind port 5000 in the container to a randomly available port between 8000 and 9000 on the host. There are also a few other ways you can configure the� Docker Desktop. The preferred choice for millions of developers that are building containerized apps. Docker Desktop is a tool for MacOS and Windows machines for the building and sharing of containerized applications and microservices. Access Docker Desktop and follow the guided onboarding to build your first containerized application in minutes.

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.

Docker run reference, You can download and install Docker on multiple platforms. Refer to the following section and choose the best installation path for you. Docker Desktop for Mac� Docker Desktop. The preferred choice for millions of developers that are building containerized apps. Docker Desktop is an application for MacOS and Windows machines for the building and sharing of containerized applications. Access Docker Desktop and follow the guided onboarding to build your first containerized application in minutes.

Get Docker, You can run both Linux and Windows programs and executables in Docker containers. The Docker platform runs natively on Linux (on x86-64, ARM� Docker Desktop allows you to switch between Stable and Edge releases. However, you can only have one version of Docker Desktop installed at a time. Switching between Stable and Edge versions can destabilize your development environment, particularly in cases where you switch from a newer (Edge) channel to an older (Stable) channel.

Docker frequently asked questions (FAQ), You can also use DOCKER_HOST=ssh://USER@HOST or ssh -L /path/to/docker. sock:/var/run/docker.sock instead if you prefer SSH over TLS. The daemon is also � You can follow these steps: Use docker ps to get the name of the existing container. Use the command docker exec -it <container name> /bin/bash to get a bash shell in the container. Or directly use docker exec -it <container name> <command> to execute whatever command you specify in the container.

Docker security, Whether your Docker hosts run Linux, Windows, or a mix of the two, you can use how Docker networks work, so you will not find information about how Docker� Reproducibility: Similar to a Java application, which will run exactly the same on any device capable of running a Java Virtual Machine, a Docker container is guaranteed to be identical on any system that can run Docker. The exact specifications of a container are stored in a Dockerfile.

Comments
  • Have you made sure, that there is nothing else running on port 7006? You could check that with the command sudo netstat -tulpn
  • Yes there is nothing running on the port before I run docker-compose up.
  • Any idea if it's possible to change the port number in the db? I need to persist the volume data.
  • You would have to edit your database manually using phpmyadmin.