'How to import locally running wordpress site to build custom container image and run it on docker

I installed WordPress on the local machine and then pulled the MySQL database on the docker and connected that MySQL with the WordPress running locally using the XAMPP software. This worked perfectly. The following was the docker-compose.yml file for mysql:

docker run -p 3306:3306 -d -e MYSQL_ROOT_PASSWORD=password -e MYSQL_USER=wordpress -e MYSQL_PASSWORD=wordpress -e MYSQL_DATABASE=crud_links --name desh-mysql  mysql

Now in the second phase, I wanted to grab the WordPress running on the local machine and build a docker image from it and run it from the container image instead of the local machine.

The following is the data that I included in the Dockerfile:

FROM wordpress

COPY ./ /var/www/html #to copy the wordpress files to container

The command that I used to create the image from the above-mentined Dockerfile:

docker build -t deshwp:1.5 .

And once the image was created, I moved ahead and runned it using the below mentioned docker run command:

docker run -e WORDPRESS_DB_HOST=localhost -e WORDPRESS_DB_USER=wordpress -e WORDPRESS_DB_PASSWORD=wordpress -e WORDPRESS_DB_NAME=crud_links -e WORDPRESS_TABLE_PREFIX=wp_ --links desh-mysql -p 8080:80 --name desh-wordpress -d deshwp:1.5

And when I run the localhost:8080, I always get the "Error Establishing a Database Connection" error. Although the WordPress config file contains all the credentials as mentioned on the above docker run command.

Please help me figure out where I am making a mistake. My goal is to import the WordPress image from the Windows PC (that is running perfectly) to a container and run it using the container instead of the local machine.

My Goal is: I am having WordPress running locally (hosted on localPC) with MySQL (hosted on Docker container). Now I wanted to convert the locally hosted wordpress site into a container. Thus I created a Docker file to build a conatiner image. The following is the script that I included in the Dockerfile to convert into a container:

FROM wordpress

COPY ./ /var/www/html

The file was converted into a container using the command:

docker build -t tag_name .

Now I just wanted to connect the wordpress (imported from the local PC), and mysql to work in sync and make the imported wordpress conatiner to work through the following docker-compose file:

version: "3.8"

services: # adds 2 services: mysql and phpmyadmin to connect with
  #Database
  desh-db:
    image: mysql:latest # use latest version of mysql
    container_name: desh-db # add a name for the container
    restart: unless-stopped
    environment: # add default values, see docs for more info.
      MYSQL_USER: user
      MYSQL_ROOT_PASSWORD: mypassword
      MYSQL_PASSWORD: mypassword
      MYSQL_DATABASE: test_db # create this database on startup
    volumes:
      - my-db:/var/lib/mysql
    ports:
      - '3306:3306'
  #phpmyadmin
  desh-phpmyadmin:
    container_name: desh-phpmyadmin
    image: phpmyadmin:latest
    ports:
     - "8082:80"
    environment:
      MYSQL_ROOT_PASSWORD: mypassword #(Required) set the password for the root superuser account.
      PMA_HOST: desh-db   # define the address/hostname of the mysql server eg mysql container name.
      PMA_USER: root # this is the root user to login on startup
      PMA_PASSWORD: mypassword # use the root password to login on startup.
      # Create a new user on startup (optional)
      # MYSQL_USER: newuser
      # MYSQL_PASSWORD: mypassword
  #wordpress
  desh-wordpress:
    depends_on:
      - desh-db
    image: deshwp
    container_name: desh-wordpress
    ports:
      - '8000:80'
    restart: always
    volumes: ['./:/var/www/html']
    environment:
      WORDPRESS_DB_HOST: desh-db:3306
      WORDPRESS_DB_USER: user
      WORDPRESS_DB_PASSWORD: mypassword
      WORDPRESS_DB_NAME: test_db
volumes: # add persistent data even if container is removed.
  my-db:

But every time it shows the following error. "Error Establishing a Database Connection" when opening the localhost:8000



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source