Master Docker Init: Quickly Scaffold Dockerfiles and Compose for PHP Apps
This guide explains what the Docker init command does, walks through a step‑by‑step example of creating a simple PHP application, shows the automatically generated Dockerfile and docker‑compose.yml, and demonstrates how to build and run the container, highlighting why docker init simplifies containerization for beginners.
Overview
Docker is an open‑source container engine that lets developers package applications and their dependencies into portable containers with minimal performance overhead.
What Is docker init ?
docker initis a CLI tool that bootstraps Docker resources for a project, automatically generating Dockerfile, docker-compose.yml, and .dockerignore based on the selected template. The latest version supports Go, Python, Node.js, Rust, ASP.NET, PHP, and Java, but currently works only with Docker Desktop.
How to Use docker init
Navigate to the project directory and run docker init. The command scans the code, prompts you to choose a template and platform, and then creates the necessary configuration files.
Example: PHP Application
Create a simple index.php file:
<?php
echo 'Hello World! 开源技术小栈!';Run docker init and follow the prompts to select a PHP template. The tool suggests a PHP version, port, and entry‑point command.
After confirming the defaults, Docker init creates a Dockerfile and a docker-compose.yml for you.
Generated Dockerfile
# syntax=docker/dockerfile:1
FROM php:7.4.28-apache
COPY . /var/www/html
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
USER www-dataIt follows the performance and security best practices commonly recommended in community posts.
Generated docker-compose.yml
services:
server:
build:
context: .
ports:
- 9009:80The compose file defines a single service called server that builds the image from the Dockerfile and maps host port 9009 to container port 80.
Build the Image
docker compose up --buildRun the Container
[+] Running 1/1
✔ Container init-server-1 Recreated 0.2s
Attaching to server-1
...Access the Application
$ curl http://127.0.0.1:9009/
Hello World! 开源技术小栈!Why Use docker init ?
The command makes Dockerizing an app effortless, especially for newcomers, by eliminating manual Dockerfile authoring, applying language‑specific templates, and adhering to industry best practices, which saves time and reduces errors.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
