Custom image
Customizing the Docker Image Used by a Scenario/Platform¶
The docker driver supports using pre-built images and docker build
-ing local customizations for each scenario's platform. The Docker
image used by a scenario is governed by the following configuration
items:
-
platforms[*].image: Docker image name:tag to use as base image. -
platforms[*].pre_build_image: Whether to customize base image or use as-is1.- When
true, use the specifiedplatform[].imageas-is. - When
false, execdocker buildto customize base image using either:
- Dockerfile specified by
platforms[*].dockerfileor - Dockerfile rendered from
Dockerfile.j2template (in scenario dir)
- When
To customize the Docker image, create a Dockerfile.j2 template file in your scenario directory. Molecule renders this template to build the image for the scenario.
Here is a commonly used example Dockerfile.j2 that substitutes the base image defined in molecule.yml and installs basic dependencies:
# The `item` variable contains the platform configuration from molecule.yml
FROM {{ item.image }}
# Install basic dependencies (example for Debian/Ubuntu based images)
RUN apt-get update && apt-get install -y python3 sudo bash \
&& rm -rf /var/lib/apt/lists/*
Note: platforms[*].pre_build_image defaults to true in each
scenario's generated molecule.yml file. You must set it to false in your platform configuration to instruct Molecule to build the custom image.