How can I make this docker image smaller?











up vote
-2
down vote

favorite












I have the following dockerfile:



FROM php:7.2-apache
LABEL name "medico-app"
COPY composer.json composer.lock ./
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
RUN apt-get update && apt-get install -y --no-install-recommends git zip && composer install
COPY . /var/www/html
EXPOSE 80


When this image is built, it has a size of ~500 Mbs. I'm trying to compress this image to < 100 Mb so that I can use it on Zeit Now. According to what I'm reading in the docker documentation, multi-stage builds sometimes help in making images smaller. My current idea is to split the dockerfile into two stages, one where I would install the dependencies with compose and the other where I'd just have php and apache. I can't seem to get it right though. Any suggestions?



This is what I have so far:



# first stage
FROM composer:latest
COPY composer.json composer.lock ./
RUN composer install


For the second stage, I tried this



FROM httpd:2.4-alpine
LABEL name "medico-app"
COPY --from=0 /app/vendor ./vendor
COPY . /usr/local/apache2/htdocs/
EXPOSE 80


However when I run the container now, the php files aren't served, I just see them as text. Im probably missing something here with PHP/Apache.



EDIT:



I also tried this for the second stage but I can't get it to work:



FROM php:7.2-alpine
LABEL name "medico-app"
RUN apk --no-cache update && apk --no-cache add apache2 openrc
COPY --from=0 /app/vendor ./vendor
COPY . /var/www/
EXPOSE 80


Now when I open my localhost I don't see the PHP files that I should see. I just see the default it works page.










share|improve this question
























  • You could purge apt lists after installing your dependencies to save some space: rm -rf /var/lib/apt/lists/*. But your problem will be more likely huge vendor folder.
    – Martin Adámek
    Nov 10 at 12:56










  • @martinadamek The vendor folder is actually less than 5 MBs. The problem is the base image itself is > 300 MB.
    – ninesalt
    Nov 10 at 13:06










  • Then maybe try installing alpine version of php and install apache manually.
    – Martin Adámek
    Nov 10 at 13:07










  • @martinadamek That's what I'm trying to do (my last edit).
    – ninesalt
    Nov 10 at 14:13















up vote
-2
down vote

favorite












I have the following dockerfile:



FROM php:7.2-apache
LABEL name "medico-app"
COPY composer.json composer.lock ./
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
RUN apt-get update && apt-get install -y --no-install-recommends git zip && composer install
COPY . /var/www/html
EXPOSE 80


When this image is built, it has a size of ~500 Mbs. I'm trying to compress this image to < 100 Mb so that I can use it on Zeit Now. According to what I'm reading in the docker documentation, multi-stage builds sometimes help in making images smaller. My current idea is to split the dockerfile into two stages, one where I would install the dependencies with compose and the other where I'd just have php and apache. I can't seem to get it right though. Any suggestions?



This is what I have so far:



# first stage
FROM composer:latest
COPY composer.json composer.lock ./
RUN composer install


For the second stage, I tried this



FROM httpd:2.4-alpine
LABEL name "medico-app"
COPY --from=0 /app/vendor ./vendor
COPY . /usr/local/apache2/htdocs/
EXPOSE 80


However when I run the container now, the php files aren't served, I just see them as text. Im probably missing something here with PHP/Apache.



EDIT:



I also tried this for the second stage but I can't get it to work:



FROM php:7.2-alpine
LABEL name "medico-app"
RUN apk --no-cache update && apk --no-cache add apache2 openrc
COPY --from=0 /app/vendor ./vendor
COPY . /var/www/
EXPOSE 80


Now when I open my localhost I don't see the PHP files that I should see. I just see the default it works page.










share|improve this question
























  • You could purge apt lists after installing your dependencies to save some space: rm -rf /var/lib/apt/lists/*. But your problem will be more likely huge vendor folder.
    – Martin Adámek
    Nov 10 at 12:56










  • @martinadamek The vendor folder is actually less than 5 MBs. The problem is the base image itself is > 300 MB.
    – ninesalt
    Nov 10 at 13:06










  • Then maybe try installing alpine version of php and install apache manually.
    – Martin Adámek
    Nov 10 at 13:07










  • @martinadamek That's what I'm trying to do (my last edit).
    – ninesalt
    Nov 10 at 14:13













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have the following dockerfile:



FROM php:7.2-apache
LABEL name "medico-app"
COPY composer.json composer.lock ./
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
RUN apt-get update && apt-get install -y --no-install-recommends git zip && composer install
COPY . /var/www/html
EXPOSE 80


When this image is built, it has a size of ~500 Mbs. I'm trying to compress this image to < 100 Mb so that I can use it on Zeit Now. According to what I'm reading in the docker documentation, multi-stage builds sometimes help in making images smaller. My current idea is to split the dockerfile into two stages, one where I would install the dependencies with compose and the other where I'd just have php and apache. I can't seem to get it right though. Any suggestions?



This is what I have so far:



# first stage
FROM composer:latest
COPY composer.json composer.lock ./
RUN composer install


For the second stage, I tried this



FROM httpd:2.4-alpine
LABEL name "medico-app"
COPY --from=0 /app/vendor ./vendor
COPY . /usr/local/apache2/htdocs/
EXPOSE 80


However when I run the container now, the php files aren't served, I just see them as text. Im probably missing something here with PHP/Apache.



EDIT:



I also tried this for the second stage but I can't get it to work:



FROM php:7.2-alpine
LABEL name "medico-app"
RUN apk --no-cache update && apk --no-cache add apache2 openrc
COPY --from=0 /app/vendor ./vendor
COPY . /var/www/
EXPOSE 80


Now when I open my localhost I don't see the PHP files that I should see. I just see the default it works page.










share|improve this question















I have the following dockerfile:



FROM php:7.2-apache
LABEL name "medico-app"
COPY composer.json composer.lock ./
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
RUN apt-get update && apt-get install -y --no-install-recommends git zip && composer install
COPY . /var/www/html
EXPOSE 80


When this image is built, it has a size of ~500 Mbs. I'm trying to compress this image to < 100 Mb so that I can use it on Zeit Now. According to what I'm reading in the docker documentation, multi-stage builds sometimes help in making images smaller. My current idea is to split the dockerfile into two stages, one where I would install the dependencies with compose and the other where I'd just have php and apache. I can't seem to get it right though. Any suggestions?



This is what I have so far:



# first stage
FROM composer:latest
COPY composer.json composer.lock ./
RUN composer install


For the second stage, I tried this



FROM httpd:2.4-alpine
LABEL name "medico-app"
COPY --from=0 /app/vendor ./vendor
COPY . /usr/local/apache2/htdocs/
EXPOSE 80


However when I run the container now, the php files aren't served, I just see them as text. Im probably missing something here with PHP/Apache.



EDIT:



I also tried this for the second stage but I can't get it to work:



FROM php:7.2-alpine
LABEL name "medico-app"
RUN apk --no-cache update && apk --no-cache add apache2 openrc
COPY --from=0 /app/vendor ./vendor
COPY . /var/www/
EXPOSE 80


Now when I open my localhost I don't see the PHP files that I should see. I just see the default it works page.







php apache docker dockerfile






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 14:18

























asked Nov 10 at 12:49









ninesalt

1,24011128




1,24011128












  • You could purge apt lists after installing your dependencies to save some space: rm -rf /var/lib/apt/lists/*. But your problem will be more likely huge vendor folder.
    – Martin Adámek
    Nov 10 at 12:56










  • @martinadamek The vendor folder is actually less than 5 MBs. The problem is the base image itself is > 300 MB.
    – ninesalt
    Nov 10 at 13:06










  • Then maybe try installing alpine version of php and install apache manually.
    – Martin Adámek
    Nov 10 at 13:07










  • @martinadamek That's what I'm trying to do (my last edit).
    – ninesalt
    Nov 10 at 14:13


















  • You could purge apt lists after installing your dependencies to save some space: rm -rf /var/lib/apt/lists/*. But your problem will be more likely huge vendor folder.
    – Martin Adámek
    Nov 10 at 12:56










  • @martinadamek The vendor folder is actually less than 5 MBs. The problem is the base image itself is > 300 MB.
    – ninesalt
    Nov 10 at 13:06










  • Then maybe try installing alpine version of php and install apache manually.
    – Martin Adámek
    Nov 10 at 13:07










  • @martinadamek That's what I'm trying to do (my last edit).
    – ninesalt
    Nov 10 at 14:13
















You could purge apt lists after installing your dependencies to save some space: rm -rf /var/lib/apt/lists/*. But your problem will be more likely huge vendor folder.
– Martin Adámek
Nov 10 at 12:56




You could purge apt lists after installing your dependencies to save some space: rm -rf /var/lib/apt/lists/*. But your problem will be more likely huge vendor folder.
– Martin Adámek
Nov 10 at 12:56












@martinadamek The vendor folder is actually less than 5 MBs. The problem is the base image itself is > 300 MB.
– ninesalt
Nov 10 at 13:06




@martinadamek The vendor folder is actually less than 5 MBs. The problem is the base image itself is > 300 MB.
– ninesalt
Nov 10 at 13:06












Then maybe try installing alpine version of php and install apache manually.
– Martin Adámek
Nov 10 at 13:07




Then maybe try installing alpine version of php and install apache manually.
– Martin Adámek
Nov 10 at 13:07












@martinadamek That's what I'm trying to do (my last edit).
– ninesalt
Nov 10 at 14:13




@martinadamek That's what I'm trying to do (my last edit).
– ninesalt
Nov 10 at 14:13












1 Answer
1






active

oldest

votes

















up vote
0
down vote













General tips for making docker images smaller:




  1. Use a minimal base image such as the alpine versions. In this case you can use something like php:7.2-alpine and install apache using apk.

  2. When using apt-get follow the best practices. In particular add && rm -rf /var/lib/apt/lists/*

  3. Try minifiying the code being added to the image using something like gulp minify.






share|improve this answer





















  • I'm trying to do your first suggestions. I installed apache in the php:7.2 image however I can't figure out how to run it correctly and where to put my files.
    – ninesalt
    Nov 10 at 13:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239114%2fhow-can-i-make-this-docker-image-smaller%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













General tips for making docker images smaller:




  1. Use a minimal base image such as the alpine versions. In this case you can use something like php:7.2-alpine and install apache using apk.

  2. When using apt-get follow the best practices. In particular add && rm -rf /var/lib/apt/lists/*

  3. Try minifiying the code being added to the image using something like gulp minify.






share|improve this answer





















  • I'm trying to do your first suggestions. I installed apache in the php:7.2 image however I can't figure out how to run it correctly and where to put my files.
    – ninesalt
    Nov 10 at 13:57















up vote
0
down vote













General tips for making docker images smaller:




  1. Use a minimal base image such as the alpine versions. In this case you can use something like php:7.2-alpine and install apache using apk.

  2. When using apt-get follow the best practices. In particular add && rm -rf /var/lib/apt/lists/*

  3. Try minifiying the code being added to the image using something like gulp minify.






share|improve this answer





















  • I'm trying to do your first suggestions. I installed apache in the php:7.2 image however I can't figure out how to run it correctly and where to put my files.
    – ninesalt
    Nov 10 at 13:57













up vote
0
down vote










up vote
0
down vote









General tips for making docker images smaller:




  1. Use a minimal base image such as the alpine versions. In this case you can use something like php:7.2-alpine and install apache using apk.

  2. When using apt-get follow the best practices. In particular add && rm -rf /var/lib/apt/lists/*

  3. Try minifiying the code being added to the image using something like gulp minify.






share|improve this answer












General tips for making docker images smaller:




  1. Use a minimal base image such as the alpine versions. In this case you can use something like php:7.2-alpine and install apache using apk.

  2. When using apt-get follow the best practices. In particular add && rm -rf /var/lib/apt/lists/*

  3. Try minifiying the code being added to the image using something like gulp minify.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 13:47









yamenk

10.9k31125




10.9k31125












  • I'm trying to do your first suggestions. I installed apache in the php:7.2 image however I can't figure out how to run it correctly and where to put my files.
    – ninesalt
    Nov 10 at 13:57


















  • I'm trying to do your first suggestions. I installed apache in the php:7.2 image however I can't figure out how to run it correctly and where to put my files.
    – ninesalt
    Nov 10 at 13:57
















I'm trying to do your first suggestions. I installed apache in the php:7.2 image however I can't figure out how to run it correctly and where to put my files.
– ninesalt
Nov 10 at 13:57




I'm trying to do your first suggestions. I installed apache in the php:7.2 image however I can't figure out how to run it correctly and where to put my files.
– ninesalt
Nov 10 at 13:57


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239114%2fhow-can-i-make-this-docker-image-smaller%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma