Skip to content

Commit 5b022d9

Browse files
authored
Update _index.md
As per the following PR "docker#20449" explaining that installing dependencies within the Dockerfile can be rolled back across volumes in development environments, it is interesting that you are also warned about dependency managers.
1 parent c9df395 commit 5b022d9

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

content/build/cache/_index.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,71 @@ EOF
269269
(Note the `set -e` command to exit immediately after any command fails, instead
270270
of continuing.)
271271

272+
## Dealing with dependencies in development
273+
274+
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.
275+
276+
But remember, in development environments most of the time the application code is shared with the container through volumes, and is not copied into the image through Dockerfile
277+
278+
In these cases:
279+
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.
280+
281+
Exemple:
282+
283+
- app.js
284+
- package.json
285+
- Dockerfile
286+
- docker-compose.yaml
287+
- entrypoint.sh
288+
289+
**Dockerfile**
290+
```dockerfile
291+
FROM node
292+
293+
WORKDIR /app
294+
295+
RUN mkdir -p /dockerfiles
296+
297+
COPY ./entrypoint.sh /dockerfiles/entrypoint.sh
298+
299+
RUN chmod +x /dockerfiles/entrypoint.sh
300+
301+
ENTRYPOINT [ "/dockerfiles/entrypoint.sh" ]
302+
```
303+
304+
**entrypoint.sh**
305+
```shell
306+
#!/bin/bash
307+
308+
npm install
309+
310+
tail -f /dev/null
311+
```
312+
313+
314+
**docker-compose.yaml**:
315+
```ỳaml
316+
services:
317+
node:
318+
build: .
319+
container_name: node
320+
volumes:
321+
- .:/app
322+
networks:
323+
- default
324+
networks:
325+
default:
326+
driver: bridge
327+
```
328+
329+
In this case, the volume is sharing the application code with the container, completely overwriting the installation of dependencies made within the Dockerfile.
330+
331+
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.
332+
333+
For production environments, the current example is perfect. However, in development environments, we could have a new section explaining that using volumes would be sufficient instead of copying the content again with "COPY . .". Additionally, it is not necessary to run "npm run build" just to cache the layer, as this is not advantageous in development.
334+
335+
It is also recommended not to run package managers like npm, Composer, Maven, pip, etc., inside the image. Leave the installation to the CMD or ENTRYPOINT. This is important to ensure that the shared volume in development does not overwrite the files installed by the package manager.
336+
272337
## Other resources
273338

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

0 commit comments

Comments
 (0)