Docker has revolutionized the development process, allowing you to create isolated environments identical to production. In this article, we'll set up a full-fledged Docker environment for OpenCart.
Why Docker?
- Consistency — same environment for all developers
- Isolation — different PHP versions for different projects
- Simplicity — one
docker-compose upstarts everything - Production-ready — easy to migrate to server
Project Structure
opencart-docker/
├── docker/
│ ├── nginx/
│ │ └── default.conf
│ └── php/
│ └── Dockerfile
├── src/
│ └── (OpenCart files)
├── docker-compose.yml
└── .env
Docker Compose Configuration
# docker-compose.yml
version: '3.8'
services:
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./src:/var/www/html
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
networks:
- opencart
php:
build:
context: ./docker/php
volumes:
- ./src:/var/www/html
environment:
- PHP_MEMORY_LIMIT=256M
networks:
- opencart
mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_USER: ${DB_USERNAME}
MYSQL_PASSWORD: ${DB_PASSWORD}
volumes:
- mysql_data:/var/lib/mysql
ports:
- "3306:3306"
networks:
- opencart
redis:
image: redis:alpine
ports:
- "6379:6379"
networks:
- opencart
mailhog:
image: mailhog/mailhog
ports:
- "1025:1025"
- "8025:8025"
networks:
- opencart
networks:
opencart:
volumes:
mysql_data:
PHP Dockerfile
# docker/php/Dockerfile
FROM php:8.2-fpm
# Install dependencies
RUN apt-get update && apt-get install -y \
libpng-dev \
libjpeg-dev \
libfreetype6-dev \
libzip-dev \
unzip \
git \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) \
gd \
mysqli \
pdo_mysql \
zip \
opcache
# Redis extension
RUN pecl install redis && docker-php-ext-enable redis
# Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# PHP configuration
RUN echo "memory_limit=256M" > /usr/local/etc/php/conf.d/opencart.ini \
&& echo "upload_max_filesize=50M" >> /usr/local/etc/php/conf.d/opencart.ini \
&& echo "post_max_size=50M" >> /usr/local/etc/php/conf.d/opencart.ini
WORKDIR /var/www/html
Nginx Configuration
# docker/nginx/default.conf
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php;
location / {
try_files $uri $uri/ @opencart;
}
location @opencart {
rewrite ^/(.+)$ /index.php?_route_=$1 last;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
}
}
mailhog for testing emails without actual sending. Web interface is available on port 8025.
.env File
# .env
DB_ROOT_PASSWORD=root_password
DB_DATABASE=opencart
DB_USERNAME=opencart
DB_PASSWORD=opencart_password
Running
# Build and start
docker-compose up -d --build
# View logs
docker-compose logs -f
# Stop
docker-compose down
# Remove with volumes
docker-compose down -v
Docker is not just a tool, it's a development philosophy. "Works on my machine" is no longer an excuse.
— Solomon Hykes, Docker creator
Useful Commands
# Enter PHP container
docker-compose exec php bash
# Execute command in container
docker-compose exec php php -v
# MySQL console
docker-compose exec mysql mysql -u opencart -p
# Clean everything
docker system prune -a
Conclusion
Now you have a full-fledged Docker environment for OpenCart development. This same environment can be used on any computer and easily adapted for production.