docker buildx build command
Overview
docker buildx buildis an extended version of the traditionaldocker buildcommand, offering multi-architecture builds and advanced build options.
- This allows you to build images for multiple platforms such as x86 and ARM in a single command.
- Basically Build = Making a Docker Image
- Used to use just
buildin the pastbuildxis an expanded version ofbuild, it hasbuild kit
- Docker Commands
Basic Structure
my-app/
├── src/
├── Dockerfile
└── ...
Basic Usage Example
Initially, you need to ensure you have a buildx builder instance ready. (This is a one time setup)
# Create a new builder instance
docker buildx create --name mybuilder --use
# (Optional) Inspect the builder
docker buildx inspect --bootstrap# Build an image using the Dockerfile in the current directory (.)
# -t : specify tag
# --platform : specify target platforms
# --push : push to registry after build
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t username/my-app:1.0.0 \
.| Option | Description |
|---|---|
--platform | Specify platforms to build (comma-separated) |
-t | Specify image name and tag |
--push | Automatically push to registry after build - default is Docker Hub |
--load | Load the built image into the local Docker engine |
You have to know
--platform, --t, --push, --load
- Tips
- On M1/M2 Macs, use
--platform linux/amd64to test with the same architecture as the server. - When building multi-architecture images, use
--pushto upload the manifest list to the registry.
- On M1/M2 Macs, use
Summary
| Keyword | Summary |
|---|---|
| buildx | Provides extended build features (uses BuildKit) |
| —platform | Specify architecture |
| —push | Push after build |
Image Tagging
Tagging
Tags are identifiers that distinguish image versions and purposes. You can assign multiple tags to the same image to easily separate development and production environments.
# <registry>/<username>/<image>:<tag>
docker tag my-app:latest username/my-app:1.0.0docker tag my-app:latest (1) username/my-app:1.0.0 (2)- 1 = original
- 2 = new tag
| Component | Example | Description |
|---|---|---|
| Registry | docker.io | Defaults to DockerHub |
| Username | username | DockerHub account name |
| Image Name | my-app | Application name |
| Tag | 1.0.0 | Version information |
- Tips
- Use
latestmainly for development/testing. - Use versioned tags like
1.0.0in production. - Manage environment-specific tags like
dev,staging,prodalongside version tags.
- Use
Summary
| Tag | Purpose |
|---|---|
| latest | Latest build |
| 1.0.0 | Fixed version |
| dev | Development |
DockerHub Deployment
docker login # Enter username and password
docker push username/my-app:1.0.0- Create a DockerHub account and log in.
- Build and tag the image locally
- Upload using
docker push. - Verify the image on DockerHub web interface.
Tips
- In CI/CD pipelines, use
DOCKER_USERNAMEandDOCKER_PASSWORDenvironment variables for automated build & push. - When using private repositories, manage account permissions carefully.