Simplify dockerfile

Signed-off-by: Shkar T. Noori <shkarface@gmail.com>
This commit is contained in:
Shkar T. Noori 2021-10-28 20:57:40 +03:00
parent db6d6a0e7d
commit 8c913e7670
No known key found for this signature in database
GPG Key ID: E7AD76088FB6FE02

View File

@ -272,34 +272,27 @@ Docker Layer Caching mainly works on `RUN`, `COPY` and `ADD` commands.
Image layers in the Dockerfile must be properly cached and necessary techniques to invalidate the cache must be put in place. Here is an example of how caching the layers could be achieved:
# TODO: Simplify dockerfile
```dockerfile
FROM reg.dev.krd/phusion/passenger-full:1.0.14 AS base
ENV HOME /root
CMD ["/sbin/my_init"]
ADD nginx.conf /etc/nginx/sites-enabled/webapp.conf
RUN rm -f /etc/service/nginx/down
RUN rm /etc/nginx/sites-enabled/default
RUN mkdir /home/app/webapp
RUN nginx -t
FROM reg.dev.krd/library/ubuntu:20.04 AS base
COPY file.txt /target.txt
RUN apt-get update -qq && apt-get install -y postgresql-client
RUN gem install bundler -v 2.2.17
FROM base as installation
ENV BUNDLER_VERSION=2.2.17
```
In this example, when building the dockerfile, any changes to the `file.txt` will result in reinstalling the dependencies; which is most likely not what we want. we can fix that by simply moving the copy command below the run command:
```dockerfile
FROM reg.dev.krd/library/ubuntu:20.04 AS base
RUN apt-get update -qq && apt-get install -y postgresql-client
COPY file.txt /target.txt
```
This way, even when we make changes to the `file.txt`, we do not have to reinstall the dependencies.
We also recommend reading the official [Best practices for writing Dockerfiles](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/).
### Statelessness
Distributed applications must maintain statelessness all during its life cycle. This is a founding principle of all distributed systems.