Skip to content

Commit a6cbd15

Browse files
authored
Merge pull request docker-library#968 from zakame/perl-document-making-onbuild-variant
Add example on how to make an onbuild image for Perl
2 parents 6bef8d3 + 43b7e86 commit a6cbd15

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

perl/content.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,27 @@ For many simple, single file projects, you may find it inconvenient to write a c
3131
```console
3232
$ docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp perl:5.20 perl your-daemon-or-script.pl
3333
```
34+
35+
## Example: Creating a reusable Carton image for Perl projects
36+
37+
Suppose you have a project that uses [Carton](https://metacpan.org/pod/Carton) to manage Perl dependencies. You can create a `perl:carton` image that makes use of the [ONBUILD](https://docs.docker.com/engine/reference/builder/#onbuild) instruction in its `Dockerfile`, like this:
38+
39+
```dockerfile
40+
FROM perl:5.26
41+
42+
RUN cpanm Carton \
43+
&& mkdir -p /usr/src/app
44+
WORKDIR /usr/src/app
45+
46+
ONBUILD COPY cpanfile* /usr/src/myapp
47+
ONBUILD RUN carton install
48+
49+
ONBUILD COPY . /usr/src/app
50+
```
51+
52+
Then, in your Carton project, you can now reduce your project's `Dockerfile` into a single line of `FROM perl:carton`, which may be enough to build a stand-alone image.
53+
54+
Having a single `perl:carton` base image is useful especially if you have multiple Carton-based projects in development, to avoid "boilerplate" coding of installing Carton and/or copying the project source files into the derived image. Keep in mind, though, about certain things to consider when using the Perl image in this way:
55+
56+
- This kind of base image will hide the useful bits (such as the`COPY`/`RUN` above) in the image, separating it from more specific Dockerfiles using the base image. This might lead to confusion when creating further derived images, so be aware of how [ONBUILD triggers](https://docs.docker.com/engine/reference/builder/#onbuild) work and plan appropriately.
57+
- There is the cost of maintaining an extra base image build, so if you're working on a single Carton project and/or plan to publish it, then it may be more preferable to derive directly from a versioned `perl` image instead.

0 commit comments

Comments
 (0)