How to Set Up Docker to Work as a WAMP Stack

In this tutorial, we will guide you through the process of setting up Docker to work as a WAMP (Windows, Apache, MySQL, PHP) stack. This setup will allow you to run your PHP applications in a Docker container with Apache as the web server and MySQL as the database.

Step 1: Create a Dockerfile
Firstly, we need to create a Dockerfile. This file will define the environment for our PHP and Apache services. Here’s a basic Dockerfile that you can use:

FROM php:7.4-apache
RUN docker-php-ext-install mysqli
COPY . /var/www/html/
EXPOSE 80

This Dockerfile does the following:

  • Starts from the php:7.4-apache image, which includes PHP 7.4 and Apache.
  • Installs the mysqli extension for PHP, which is needed to connect to MySQL.
  • Copies the current directory (.) into /var/www/html/, which is the default Apache document root.
  • Exposes port 80, which is the default port for HTTP.

Step 2: Create a docker-compose.yml File
Next, we need to create a docker-compose.yml file. This file will define and manage our MySQL service and link it to our PHP and Apache services. Here’s a basic docker-compose.yml file that you can use:

version: '3'
services:
  web:
    build: .
    ports:
      - "8080:80"
    volumes:
      - .:/var/www/html
    links:
      - db
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: mydb
      MYSQL_USER: user
      MYSQL_PASSWORD: password

This docker-compose.yml file does the following:

  • Defines two services: web and db.
  • The web service is built from the current directory (which should contain your Dockerfile) and maps port 8080 on your host to port 80 on the container. It also mounts the current directory to /var/www/html in the container and links to the db service.
  • The db service uses the mysql:5.7 image and sets up a number of environment variables to configure MySQL.

Step 3: Build and Run Your Services
Finally, you can build and run your services with the following command:

docker-compose up -d

This command will start your services in the background (-d stands for “detached mode”).

Step 4: Accessing Your Application
Once the services are up and running, you can access your PHP application by navigating to http://localhost:8080 in your web browser. This URL maps to port 8080 on your local machine, which is forwarded to port 80 in the Docker container where Apache is running.

Step 5: Managing Your MySQL Database
To interact with your MySQL database, you can use any MySQL client or a tool like phpMyAdmin. Connect using the following details:

  • Host: db (as defined in your docker-compose.yml file)
  • User: user
  • Password: password
  • Database: mydb

Step 6: Adding Persistent Storage
In a production environment, you would want to ensure that your MySQL data is not lost when the container is removed. To achieve this, you can use Docker volumes. Update your docker-compose.yml file as follows to add a volume for MySQL:

version: '3'
services:
  web:
    build: .
    ports:
      - "8080:80"
    volumes:
      - .:/var/www/html
    links:
      - db
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: mydb
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    volumes:
      - db_data:/var/lib/mysql
volumes:
  db_data:

This addition creates a named volume db_data that will persist MySQL data.

Step 7: Environment Variables and Docker Secrets
For better security, especially in a production environment, you should manage sensitive information like database passwords using Docker secrets or environment variables. Here’s an example of how you can use environment variables in your docker-compose.yml file:

version: '3.1'
services:
  web:
    build: .
    ports:
      - "8080:80"
    volumes:
      - .:/var/www/html
    links:
      - db
    environment:
      MYSQL_DATABASE: mydb
      MYSQL_USER: user
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: mydb
      MYSQL_USER: user
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - db_data:/var/lib/mysql
volumes:
  db_data:

You would then set the environment variables in a .env file or export them in your shell before running docker-compose up:

export MYSQL_ROOT_PASSWORD=root
export MYSQL_PASSWORD=password
docker-compose up -d

And there you have it! You’ve successfully set up Docker to work as a WAMP stack. This guide covered the basic setup, accessing your application, managing the database, adding persistent storage, and securing sensitive information. While this setup is a good starting point, there are many other configurations and optimizations you might want to explore for a production-ready environment. Happy Dockerizing!


Posted

in

by

Tags: