如何在VS Code中编辑Docker容器内的PrestaShop代码?
Standalone Docker Setup (Fix Code Editing & Remote Containers Access)
Your initial setup lacked a local volume mount to sync PrestaShop's code between your machine and the container—this is why you couldn't edit files in VS Code. Follow these adjusted steps:
Create a local directory for PrestaShop code
mkdir ~/prestashop-codeRecreate the network (if not already exists)
docker network create prestashop-netLaunch MySQL container (same as before)
docker run -d --name some-mysql --network prestashop-net -e MYSQL_ROOT_PASSWORD=admin -p 3307:3306 mysql:5.7Launch PrestaShop container with volume mount
The-vflag maps your local code directory to the container's/var/www/html(where PrestaShop stores its source files):docker run -d --name some-prestashop --network prestashop-net -e DB_SERVER=some-mysql -p 8080:80 -v ~/prestashop-code:/var/www/html prestashop/prestashopAccess code in VS Code
- Option 1: Open the local
~/prestashop-codedirectory directly in VS Code—any edits will sync to the container automatically. - Option 2: Use Remote Containers plugin:
- Open VS Code's Remote Containers extension.
- Under the "Containers" list, find
some-prestashop(refresh the list if it doesn't appear). - Click "Attach to Container" to open the container's filesystem in VS Code for direct editing.
- Option 1: Open the local
Docker Compose Setup (Fix Port Access & Code Editing)
If your docker-compose setup had port issues, use this working configuration to ensure proper service linking, port mapping, and code sync:
Create a project directory and add
docker-compose.ymlversion: '3.8' services: mysql: image: mysql:5.7 container_name: prestashop-mysql networks: - prestashop-net environment: MYSQL_ROOT_PASSWORD: admin MYSQL_DATABASE: prestashop MYSQL_USER: prestashop MYSQL_PASSWORD: prestashop volumes: - mysql-data:/var/lib/mysql prestashop: image: prestashop/prestashop container_name: prestashop-app networks: - prestashop-net environment: DB_SERVER: mysql DB_USER: prestashop DB_PASSWORD: prestashop DB_NAME: prestashop ports: - "8001:80" volumes: - ./prestashop-code:/var/www/html depends_on: - mysql volumes: mysql-data: networks: prestashop-net:Start the services
docker-compose up -dVerify access
Wait 2-3 minutes for the container to initialize, then visithttp://localhost:8001—the site should load.Fix permission issues (if needed)
If you can't edit local files or the site throws errors, set correct ownership for the code directory:sudo chown -R $USER:$USER ./prestashop-codeEdit code in VS Code
Use the same methods as the standalone setup: open the localprestashop-codedirectory or attach to theprestashop-appcontainer via Remote Containers.
内容的提问来源于stack exchange,提问作者wtsuport

