Developing Magento 2 Locally with Docker: Bind-Mounting Files
Local development is an essential part of working with any e-commerce platform like Magento 2 or Adobe Commerce. Using Docker provides a consistent environment for your application, but sometimes you need to work directly with the files on your host machine. This guide will walk you through how to bind-mount Magento 2 files from Docker using docker-compose.
Understanding Bind-Mounts in Docker
Before we dive into the specifics of Magento 2, let’s quickly recap what a bind-mount is.
A bind-mount allows you to mount a file or directory on your host machine inside a container. This means any changes made to the files within the container will be reflected on the host and vice versa. It’s an excellent way to maintain local copies of your application while still utilizing Docker for environment consistency.
Bind-Mounting Magento 2 Files with docker-compose
In your current setup, you’re using a pre-built docker-compose.yml file from Bitnami. To implement bind-mounts, you’ll need to modify this file and add the necessary volume configurations.
Step 1: Create the Magento 2 Project Directory
First, create a directory on your host machine where you want the Magento 2 files to be stored. This will serve as the bind-mount location for both your local machine and the Docker container.
mkdir magento2
cd magento2
Step 2: Modify the docker-compose.yml File
Now, let’s modify the docker-compose.yml file you’re currently using to include the bind-mounts. Open this file in a text editor and update it as follows:
version: '2'
services:
mariadb:
image: docker.io/bitnami/mariadb:10.4
environment:
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
- MARIADB_USER=bn_magento
- MARIADB_DATABASE=bitnami_magento
volumes:
- 'mariadb_data:/bitnami/mariadb'
magento:
image: docker.io/bitnami/magento:2
ports:
- '80:8080'
- '443:8443'
environment:
- MAGENTO_HOST=localhost
- MAGENTO_DATABASE_HOST=mariadb
- MAGENTO_DATABASE_PORT_NUMBER=3306
- MAGENTO_DATABASE_USER=bn_magento
- MAGENTO_DATABASE_NAME=bitnami_magento
- ELASTICSEARCH_HOST=elasticsearch
- ELASTICSEARCH_PORT_NUMBER=9200
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
volumes:
- './:/bitnami/magento' # This line is new
depends_on:
- mariadb
- elasticsearch
elasticsearch:
image: docker.io/bitnami/elasticsearch:7
volumes:
- 'elasticsearch_data:/bitnami/elasticsearch/data'
volumes:
mariadb_data:
driver: local
magento_data:
driver: local
elasticsearch_data:
driver: local
The key change is the addition of the ./: line under the magento service’s volumes section. This line tells Docker to mount the current directory (.) on the host machine as the /bitnami/magento path inside the container.
Step 3: Initialize the Magento 2 Installation
Now, let’s initialize a new Magento 2 installation in this new directory:
cd magento2
./vendor/bin/magento setup:install --base-url=http://localhost/ --db-host=mariadb --db-name=bitnami_magento --db-user=bn_magento --db-password="" --admin-firstname=Admin --admin-lastname=User --admin-email=info@magento2.com --admin-password="YourStrongPassword!" --admin-username="admin"
Step 4: Start the Containers
Finally, start your containers using docker-compose:
docker-compose up -d
Conclusion
By following these steps and properly setting up bind-mounts in your docker-compose.yml file, you can now work directly with your Magento 2 files on your host machine while still utilizing the benefits of Docker for environment consistency.
This approach allows you to maintain a local copy of your Magento 2 project, making it easier to version control and collaborate with other developers. Remember that any changes made within the container will be reflected in the mounted directory and vice versa.
If you found this guide helpful or have any questions about local Magento 2 development using Docker, feel free to explore our Docker guides at Laravel Company. We’re always here to help!
Happy coding!