You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Alpine容器中部署含MySQL、PhpMyAdmin的网站问题咨询

Docker部署多服务问题解答

Hey there! Let's break down your problem and go over the best approaches here. First, let's recap your situation: you're new to Docker, have a website relying on PHP, Java/Tomcat, MySQL, and want to get MySQL + PhpMyAdmin working alongside your existing Tomcat container.


你的当前配置

Dockerfile

FROM alpine:latest
ENV http_proxy http://not_important/
RUN apk update
RUN apk --no-cache --quiet add openjdk8
RUN apk --no-cache --quiet add nano
RUN apk --no-cache --quiet add php7
RUN apk --no-cache --quiet add mysql
RUN apk --no-cache --quiet add phpmyadmin
RUN mkdir -p /usr/local/tomcat/
COPY apache-tomcat-9.0.4.tar.gz /usr/local/tomcat/
RUN cd /usr/local/tomcat/ && tar xzf /usr/local/tomcat/apache-tomcat-9.0.4.tar.gz
RUN mv /usr/local/tomcat/apache-tomcat-9.0.4/* /usr/local/tomcat
RUN rm -r /usr/local/tomcat/apache-tomcat-9.0.4
RUN rm -r /usr/local/tomcat/apache-tomcat-9.0.4.tar.gz
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]

启动命令

docker run --name alpine_custom -dit -p 30000:8080 -p31000:80 alpine:custom

核心问题解答:单容器 vs 多容器

1. 能不能在当前单容器里实现?(可以,但不推荐)

You technically can get MySQL and PhpMyAdmin running in the same Alpine container as Tomcat, but this goes against Docker's core best practice: one container, one service. Here's why this is a bad idea long-term:

  • Logs from Tomcat, MySQL, and PhpMyAdmin will be mixed together, making debugging a nightmare
  • If one service crashes, the whole container restarts, taking down your other services with it
  • Updating one component (like MySQL) requires rebuilding the entire container, risking breaks to Tomcat/PHP
  • Resource management is less efficient

If you still want to test this temporarily, here's what you'd need to do:

  • Install a process manager: Docker only runs one main process by default. You'll need supervisord to run Tomcat, MySQL, and a web server (for PhpMyAdmin) at the same time. Add this to your Dockerfile:
    RUN apk add --no-cache supervisor
    COPY supervisord.conf /etc/supervisord.conf
    CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]
    
    Create supervisord.conf with:
    [supervisord]
    nodaemon=true
    
    [program:tomcat]
    command=/usr/local/tomcat/bin/catalina.sh run
    user=root
    
    [program:mysqld]
    command=/usr/bin/mysqld_safe --datadir=/var/lib/mysql
    user=mysql
    
    [program:httpd]
    command=/usr/sbin/httpd -D FOREGROUND
    user=root
    
  • Initialize MySQL: Add RUN mysql_install_db --user=mysql --datadir=/var/lib/mysql to your Dockerfile to set up the initial database files. You'll also need to set a root password (e.g., via mysql_secure_installation or an init script).
  • Configure PhpMyAdmin: Alpine's PhpMyAdmin lives in /usr/share/webapps/phpmyadmin. You'll need to configure Apache (httpd) to serve this directory, since your current container has no web server listening on port 80.

2. 推荐方案:多容器编排(Docker最佳实践)

The cleanest, most maintainable approach is to split each service into its own container, then use Docker Compose to orchestrate them. This way, each service is isolated, easy to update, and you can manage dependencies clearly.

Step 1: Update your Dockerfile

Remove MySQL and PhpMyAdmin-related installs, since those will run in separate containers:

FROM alpine:latest
ENV http_proxy http://not_important/
RUN apk update
RUN apk --no-cache --quiet add openjdk8
RUN apk --no-cache --quiet add nano
RUN apk --no-cache --quiet add php7
RUN mkdir -p /usr/local/tomcat/
COPY apache-tomcat-9.0.4.tar.gz /usr/local/tomcat/
RUN cd /usr/local/tomcat/ && tar xzf /usr/local/tomcat/apache-tomcat-9.0.4.tar.gz
RUN mv /usr/local/tomcat/apache-tomcat-9.0.4/* /usr/local/tomcat
RUN rm -r /usr/local/tomcat/apache-tomcat-9.0.4
RUN rm -r /usr/local/tomcat/apache-tomcat-9.0.4.tar.gz
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]

Step 2: Create a Docker Compose file

Make a docker-compose.yml in the same directory as your Dockerfile:

version: '3.8'
services:
  # Your Tomcat/PHP service
  tomcat:
    build: .
    ports:
      - "30000:8080"
    depends_on:
      - mysql
    networks:
      - app-network

  # MySQL database container (official, stable image)
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: your_secure_root_password
      MYSQL_DATABASE: your_app_database_name
      MYSQL_USER: app_db_user
      MYSQL_PASSWORD: app_db_user_password
    volumes:
      - mysql-data:/var/lib/mysql  # Persist database data even if container is deleted
    networks:
      - app-network

  # PhpMyAdmin container (official image)
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - "31000:80"
    environment:
      PMA_HOST: mysql  # Docker Compose automatically resolves this service name to the MySQL container's IP
      PMA_PORT: 3306
    depends_on:
      - mysql
    networks:
      - app-network

# Persistent volume for MySQL data
volumes:
  mysql-data:

# Shared network for all containers to communicate
networks:
  app-network:
    driver: bridge

Step 3: Start everything up

Run this command in the directory with your docker-compose.yml:

docker-compose up -d

All containers will start, connect to the same network, and be able to communicate with each other. You can access:

  • Tomcat at http://localhost:30000
  • PhpMyAdmin at http://localhost:31000 (log in with your MySQL root credentials or the app user you created)

Step 4: Using your local MySQL instead of a container

If you want to keep using your local host's MySQL instead of a containerized one:

  • For Tomcat: Update your app's database connection string to use host.docker.internal (Windows/macOS) or your local machine's IP address (Linux) instead of mysql.
  • For PhpMyAdmin: Change the PMA_HOST environment variable in the Compose file to host.docker.internal (or your local IP) so it connects to your local MySQL instance.

内容的提问来源于stack exchange,提问作者Telvanis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:37:15