Skip to content

Improving Docker build cache docs #20449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions content/build/cache/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,68 @@ EOF
(Note the `set -e` command to exit immediately after any command fails, instead
of continuing.)

## Dealing with dependencies in development

In previous examples we saw the importance of layer caching to deal with dependencies when packaging the application within the container, being an ideal scenario for production environments.

But remember, in development environments most of the time the application code is shared with the container through [`volumes`](../../storage/volumes.md), and is not copied into the image through Dockerfile

In these cases:
Do not run your package manager (NPM, Composer, Maven, Pip, etc.) inside the image, leave the installation to **CMD** or **ENTRYPOINT**. This is important so that the volume that we will normally share when developing does not overwrite the files installed by the package manager in the Dockerfile.

Exemple, In an application with this structure:

- app.js
- package.json
- Dockerfile
- docker-compose.yaml
- entrypoint.sh

**Dockerfile**
```dockerfile
FROM node

WORKDIR /app

RUN mkdir -p /dockerfiles

COPY ./entrypoint.sh /dockerfiles/entrypoint.sh

RUN chmod +x /dockerfiles/entrypoint.sh

ENTRYPOINT [ "/dockerfiles/entrypoint.sh" ]
```

**entrypoint.sh**
```shell
#!/bin/bash

npm install

tail -f /dev/null
```


**docker-compose.yaml**:
```ỳaml
services:
node:
build: .
container_name: node
volumes:
- .:/app
networks:
- default

networks:
default:
driver: bridge
```

The volume is sharing all the application code with the container, completely overwriting the installation of dependencies made within the Dockerfile.

The installation of the dependencies must be persisted in the volume that was created to handle the application code, in this case, the **ENTRYPOINT** will install the dependencies when the container is started.

## Other resources

For more information on using cache to do efficient builds, see:
Expand Down