You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Docker中如何将CentOS容器与Apache、PHP、MySQL容器联动部署服务?

Hey there! I get where you're coming from—you want to stick to Docker's best practices instead of shoving everything into a single CentOS container, which makes total sense. Let's walk through how to split your CentOS 7.2 + Apache + PHP 5.6 + MySQL stack into separate, properly connected containers, and why you don't actually need a standalone CentOS container for this.

First: Ditch the standalone CentOS container

Here's the thing: Official and community service images (like Apache, PHP, MySQL) already include a base OS layer under the hood. For your use case, you can either use CentOS 7-based service images or build your own to match CentOS 7.2 specifically. Running a separate CentOS container just to host other services defeats Docker's core design—lightweight, single-purpose containers that are easy to update, scale, and maintain.

Let's break down your stack into 2 core containers

We'll split this into an Apache + PHP 5.6 container (these two are tightly coupled, so they belong together) and a MySQL 5.6 container. We'll connect them using Docker's networking features so they can communicate.

Option 1: Manual container setup (without Docker Compose)

Step 1: Run the MySQL container

First, create a persistent volume to store MySQL data (so you don't lose it if the container is deleted):

docker volume create mysql-data

Then start the MySQL container. If you want a CentOS 7-based MySQL image, use centos/mysql-56-centos7; otherwise, the official mysql:5.6 works too:

docker run -d \
  --name mysql-56 \
  --network my-app-network \
  -e MYSQL_ROOT_PASSWORD=your_secure_root_password \
  -e MYSQL_DATABASE=your_app_db \
  -e MYSQL_USER=your_db_user \
  -e MYSQL_PASSWORD=your_user_password \
  -v mysql-data:/var/lib/mysql \
  centos/mysql-56-centos7

Note: We're using a custom network my-app-network—create it first with docker network create my-app-network.

Step 2: Build & run the Apache + PHP 5.6 container (CentOS 7.2-based)

Create a Dockerfile in a new directory with this content to build your CentOS 7.2 + Apache + PHP 5.6 image:

# Use the exact CentOS 7.2 release you need
FROM centos:7.2.1511

# Install Apache, PHP 5.6, and MySQL extensions
RUN yum install -y httpd php php-mysqlnd php-cli && \
    # Enable Apache to start on container launch
    systemctl enable httpd

# Expose port 80 for web traffic
EXPOSE 80

# Start Apache in the foreground (required for Docker to keep the container running)
CMD ["httpd", "-D", "FOREGROUND"]

Build the image:

docker build -t centos72-apache-php56 .

Now run the container, connect it to the same network, and mount your local PHP code directory into the container's web root:

docker run -d \
  --name apache-php \
  --network my-app-network \
  -v /path/to/your/local/php/code:/var/www/html \
  -p 80:80 \
  centos72-apache-php56

Step 3: Connect PHP to MySQL

In your PHP code, use mysql-56 as the database hostname (that's the name of your MySQL container) to connect. For example:

<?php
$conn = mysqli_connect("mysql-56", "your_db_user", "your_user_password", "your_app_db");
?>

Since both containers are on the same custom network, Docker's DNS will resolve the container name to its IP address automatically.

Docker Compose lets you define all your services in a single YAML file, making it easy to start, stop, and manage the entire stack.

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

version: '3'

services:
  mysql:
    # Use CentOS 7-based MySQL image
    image: centos/mysql-56-centos7
    container_name: mysql-56
    environment:
      MYSQL_ROOT_PASSWORD: your_secure_root_password
      MYSQL_DATABASE: your_app_db
      MYSQL_USER: your_db_user
      MYSQL_PASSWORD: your_user_password
    volumes:
      - mysql-data:/var/lib/mysql
    networks:
      - my-app-network

  apache-php:
    # Build from the local Dockerfile (CentOS 7.2 + Apache + PHP 5.6)
    build: .
    container_name: apache-php
    volumes:
      # Mount local code directory to container's web root
      - ./your-php-code:/var/www/html
    ports:
      - "80:80"
    networks:
      - my-app-network
    # Ensure MySQL starts before Apache/PHP
    depends_on:
      - mysql

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

# Custom network for service communication
networks:
  my-app-network:

Create a your-php-code directory next to the file, add your PHP files, then start the stack with:

docker-compose up -d

To stop the stack:

docker-compose down
Key Best Practices to Remember
  • Single purpose per container: Each container should run one service—this makes updates (e.g., upgrading PHP without touching MySQL) and troubleshooting much easier.
  • Persistent volumes: Always use volumes for data that needs to survive container restarts/deletions (like MySQL data).
  • Custom networks: Avoid using --link (deprecated) and use custom networks instead for reliable service discovery between containers.
  • Avoid "containerized VMs": Don't install multiple services in a single container—this negates Docker's benefits of isolation and scalability.

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

火山引擎 最新活动