In order to install a WordPress development environment on Docker, you'll need to install Docker and docker-compose.

Once you have those installed, install your WordPress in the folder of your choice, and add docker-compose.yml file to the root of your WordPress installation folder.

version: '3'
      - db
    image: wordpress:latest
      - 8080:80
      - 443:443
      - ./data:/data # Required if importing an existing database
      - ./:/var/www/html   # Theme development
      WORDPRESS_DB_NAME: db_name # pull this from env variables
      WORDPRESS_DB_HOST: db:3306
    image: mysql:5.7
      - data:/var/lib/mysql
      MYSQL_DATABASE: db_name
      MYSQL_USER: root
      MYSQL_PASSWORD: root
    image: phpmyadmin/phpmyadmin
      - 3306:80
  data: {}

After adding that file and changing the database name, run

docker-compose up -d

This will build a Docker container and image with the specified settings from the .yml file. Alternatively, you can run

docker-compose up -d && docker-compose logs -f wordpress

which is useful because it gives you WP log output.

For more information about setting up and using Docker with WordPress, click here.

Another way of using Docker is to use Dockerfile, which is especially useful when working on continuous integration and deployment (CI/CD). For instance, one such Dockerfile can look like this

# Stage 0, build app
FROM php:7.2-fpm as build-container

RUN curl -sS | php \
  && chmod +x composer.phar && mv composer.phar /usr/local/bin/composer

RUN apt-get update && apt-get install -y gnupg
RUN curl -sL | bash - && \
    apt-get install -yq nodejs build-essential \
        git unzip \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
    && curl -sL | bash - \
    && pecl install mcrypt-1.0.1 \
    && docker-php-ext-enable mcrypt \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install -j$(nproc) mysqli

RUN npm install -g npm

WORKDIR /build
COPY . /build
RUN cp /build/wp-config.php.template /build/wp-config.php
RUN bash /build/scripts/

# Stage 1, build app container
FROM php:7.2-fpm

RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
        unzip \
        mysql-client \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install -j$(nproc) iconv \
    && docker-php-ext-install -j$(nproc) mysqli \
    && docker-php-ext-install gd mysqli opcache zip

ADD /var/www/
RUN cd /var/www && unzip && rm
RUN rm -rf /var/www/html
RUN mkdir -p /var/www/html/ \
    && mv /var/www/wordpress/* /var/www/html/

# Copy wp files
COPY --from=build-container /build/ /var/www/html/
RUN chown www-data:www-data /var/www/html/ -R
COPY config/php.ini /usr/local/etc/php/
WORKDIR /var/www/html/

CMD ["exec","php-fpm"]

This Dockerfile will first build a local container with php, composer, and node, and in that stage your theme/plugins can be built. It also contains the RUN bash bin/ command. You can replace this with your installation script (be it for plugins or theme). You'll place composer and npm builds in that shell script.

After that, you'll build the WordPress container and copy the plugins built in the previous stage. You can modify this to your liking. Beside this, you'll probably need a Docker image for nginx. But we won't be going into too much detail about it.

If you named this file Dockerfile, you'll run

docker build -t yourtag .

In case you named it something like Dockerfile.php (if you need multiple stages and builds), you'll run

docker build -t yourtag -f Dockerfile.php .

The Dockerfiles should be located in the root of your project.

To go in the interactive shell of the built image, type

docker run -it CONTAINER_NAME bash

If that fails, you can try

docker run -d CONTAINER_NAME
docker exec -it CONTAINER_NAME bash

Running the docker run with -d means you are detaching it, and you can check the logs created while building it.

Useful Docker commands

Here are some useful Docker commands that you might use.

List all containers

docker ps -as

List all images

docker images

Stop all running containers

docker stop $(docker ps -aq)

Remove all containers

docker rm $(docker ps -a -q)

Remove all images

docker rmi $(docker images -q)

Prune the system from unused images, containers, and networks documentation link

docker system prune

Docker notes

Depending on your project, the Docker compose or Dockerfile may differ. For more information, consult your friendly devops.