Ripper346
Ripper346

Reputation: 752

Docker multi-stage builds with images that doesn't coexist

I am trying o understand Docker multi-stage builds. I have Docker toolbox 18.03 so I am able to run them. My first attempt after looking at the docs was to build an image with Python 3.7 and Php-apache. So I wrote this Dockerfile:

FROM python:3.7.0-stretch

FROM php:7-apache-stretch

RUN docker-php-ext-install mbstring mysqli pdo pdo_mysql
RUN a2enmod rewrite

# Enable SSL
RUN a2enmod ssl
RUN mkdir /etc/apache2/ssl
ADD server.crt /etc/apache2/ssl/
ADD server.key /etc/apache2/ssl/
ADD default-ssl.conf /etc/apache2/sites-available/
RUN a2ensite default-ssl
EXPOSE 443

RUN echo 'log_errors = On' >> /usr/local/etc/php/php.ini

Then I run docker build . and it built it successfully:

Sending build context to Docker daemon  12.29kB
Step 1/12 : FROM python:3.7.0-stretch
 ---> 825141134528
Step 2/12 : FROM php:7-apache-stretch
 ---> 5e5a59788e34
Step 3/12 : RUN docker-php-ext-install mbstring mysqli pdo pdo_mysql
 ---> Using cache
 ---> bf6b095ac6d3
Step 4/12 : RUN a2enmod rewrite
 ---> Using cache
 ---> d3443d0e8d97
Step 5/12 : RUN a2enmod ssl
 ---> Running in edd0a15406db
Considering dependency setenvif for ssl:
Module setenvif already enabled
Considering dependency mime for ssl:
Module mime already enabled
Considering dependency socache_shmcb for ssl:
Enabling module socache_shmcb.
Enabling module ssl.
See /usr/share/doc/apache2/README.Debian.gz on how to configure SSL and create self-signed certificates.
To activate the new configuration, you need to run:
  service apache2 restart
Removing intermediate container edd0a15406db
 ---> a301c3a3ca78
Step 6/12 : RUN mkdir /etc/apache2/ssl
 ---> Running in 59621555d753
Removing intermediate container 59621555d753
 ---> db74733784c3
Step 7/12 : ADD server.crt /etc/apache2/ssl/
 ---> db3715192748
Step 8/12 : ADD server.key /etc/apache2/ssl/
 ---> f6206d6b2d5e
Step 9/12 : ADD default-ssl.conf /etc/apache2/sites-available/
 ---> 0fa5dd62c854
Step 10/12 : RUN a2ensite default-ssl
 ---> Running in 7ab353bca552
Enabling site default-ssl.
To activate the new configuration, you need to run:
  service apache2 reload
Removing intermediate container 7ab353bca552
 ---> 53336ee4133d
Step 11/12 : EXPOSE 443
 ---> Running in 0530749e96b3
Removing intermediate container 0530749e96b3
 ---> 53b56723dd81
Step 12/12 : RUN echo 'log_errors = On' >> /usr/local/etc/php/php.ini
 ---> Running in 8bd7c50715c3
Removing intermediate container 8bd7c50715c3
 ---> d456abb9ee67
Successfully built d456abb9ee67

Then I created the container for it and run. It started and remained up, so I tried to enter into the bash of the container and there is the surprise, php and apache work fine, python doesn't even exist. What am I missing in the Dockerfile?

Upvotes: 0

Views: 1146

Answers (2)

Sugatur Deekshith S N
Sugatur Deekshith S N

Reputation: 511

I took Different example , it might help you to understand multistage Dockerfile

Requirement :- java code should be built using mvn and then deploy .war file to tomcat

Dockerfile

FROM maven as maven
RUN mkdir /usr/src/mymaven
WORKDIR /usr/src/mymaven
COPY . .
RUN mvn install -DskipTests

FROM tomcat 
WORKDIR webapps 
COPY --from=maven /usr/src/mymaven/target/java-tomcat-maven-example.war .
RUN rm -rf ROOT && mv java-tomcat-maven-example.war ROOT.war

in the above Dockerfile i am copying my java code from local to maven image and then building the mvn project using "mvn install -DskipTests" after that in second stage (Tomcat) copying .war file generated previous image to tomcat image. so by this I was able to reduce my image size.

if i wouldn't use multistage then I need to take jdk or ubuntu image then on top that need to install maven and tomcat , then the image size would have been doubled.

for simple hello world web java project you can refer https://github.com/DeekshithSN/Java_app

Hope it helps you

Upvotes: 0

gCoh
gCoh

Reputation: 3089

The last image in multistage build is the target image. In order to combine contents, you need to use copy --from original to target image.

It would be easier and safer installing python on the target image than copying it from the first one (without using multistage build)

Normally multistage builds are used for creating prod ready images, without compile time dependencies (compile on first image, copy executables to the target Image)

Upvotes: 1

Related Questions