如何导入本地运行的WordPress站点以构建自定义容器图像并在Docker上运行它

发布于 2025-01-29 03:58:31 字数 3171 浏览 6 评论 0原文

我在本地计算机上安装了WordPress,然后在Docker上拉了MySQL数据库,并将MySQL与使用XAMPP软件在本地运行的WordPress连接了。这很好。以下是mysql的docker-compose.yml文件:

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

现在在第二阶段,我想抓住在本地计算机上运行的WordPress,并从中构建docker映像,并从容器映像而不是本地计算机上运行它。

以下是我在Dockerfile中包含的数据:

FROM wordpress

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

我用来从上述Dockerfile创建图像的命令:

docker build -t deshwp:1.5 .

一旦创建了图像,我就使用以下提到的Docker Run命令向前移动并运行了它

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

:当我运行LocalHost:8080时,我总是会得到“错误建立数据库连接”错误。

请帮助我弄清楚我在哪里犯错。 我的目标是从Windows PC(完美运行)将WordPress图像导入容器,并使用容器而不是本地计算机运行它。

我的目标是: 我正在用MySQL(在Docker容器上托管)本地运行WordPress(在LocalPC上托管)。现在,我想将本地托管的WordPress站点转换为容器。因此,我创建了一个Docker文件来构建Conatiner图像。 以下是我在Dockerfile中包含的脚本以转换为容器:

FROM wordpress

COPY ./ /var/www/html

使用该命令将文件转换为容器:

docker build -t tag_name .

现在我只想去连接WordPress(从本地PC导入),MySQL可以同步工作,并使导入的WordPress Conatiner通过以下Docker-Compose文件工作:

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:

但是每次显示以下错误时。 “在打开Local -Host:8000时,建立数据库连接的错误”

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文