skip to Main Content

I have created a simple tech stack using Docker. PHP 7.2 on CentOS.

Below is the docker file

FROM centos:7

# Install Apache
RUN yum -y update
RUN yum -y install httpd httpd-tools

# Install EPEL Repo
RUN rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 
 && rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

# Install PHP
RUN yum -y install php72w php72w-bcmath php72w-cli php72w-common php72w-gd php72w-intl php72w-ldap php72w-mbstring 
    php72w-mysql php72w-pear php72w-soap php72w-xml php72w-xmlrpc

# Update Apache Configuration
RUN sed -E -i -e '/<Directory "/var/www/html">/,/</Directory>/s/AllowOverride None/AllowOverride All/' /etc/httpd/conf/httpd.conf
RUN sed -E -i -e 's/DirectoryIndex (.*)$/DirectoryIndex index.php 1/g' /etc/httpd/conf/httpd.conf

EXPOSE 80

# Start Apache
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]

Below is the docker-compose.yml

version: '3.2'
  services:
   centos-php-apache:
     build:
       context: ./
     ports:
      - "8080:80"
     volumes:
      - ./code:/var/www/html

All is now running fine. After few days, I decided to update the following –

(i) Update “dockerfile” by adding another php-module or php-extension

(ii) Update the docker compose.yml by adding another service (say) “mariadb”.

I would like to do this update, without effecting or deleting any files from the previous setup. In fact, this is a common scenario, where developers may need additional extensions or service in the future – without redoing everything right from the beginning.

Should I directly edit the "dockerfile" and add the php extension and edit the docker-compose.yml file and add the service as usual and then run the docker-compose up command. Of course before running the "up" command, I will first bring it down using the docker-compose down command.

Can anyone throw some light, on how I can accomplish this.

2

Answers


  1. It would be best if you used docker-compose up --build centos-php-apache instead of docker-compose up . And you do not want to worry about data. They are persistent with your volume map. Also, make sure to use another volume map for your mariadb data.

    Login or Signup to reply.
  2. I am adding a dummy setup, together with the contents of the files:

    tree
    .
    ├── docker-compose.yml
    ├── mariadb
    │   └── Dockerfile
    └── web-app
        └── Dockerfile
    
    ------------------------
    
    cat docker-compose.yml
    version: "3"
    services:
      webapp:
        build:
          context: ./web-app
          dockerfile: Dockerfile
      mariadb:
        build:
          context: ./mariadb
          dockerfile: Dockerfile
        environment:
          - MYSQL_ROOT_PASSWORD=supersecret
    
    ------------
    
    cat mariadb/Dockerfile 
    FROM mariadb
    RUN echo "hello from mariadb"
    
    ------------
    
    cat web-app/Dockerfile 
    FROM nginx
    RUN echo "Hello"
    RUN echo "from web-app"
    # ^^ that's an example for a change that you want to perform(adding a new module) ^^
    
    

    Assuming that you just performed the change in the web-app/Dockerfile, you have a couple of options:

    Option A:

    You rebuild one specific image, while the current containers are not affected:

    docker-compose build webapp
    
    Building webapp
    Step 1/3 : FROM nginx
     ---> 2622e6cca7eb
    Step 2/3 : RUN echo "Hello"
     ---> Using cache
     ---> ad39a2920d75
    Step 3/3 : RUN echo "from web-app"
     ---> Running in ea62a9e81110
    from web-app
    Removing intermediate container ea62a9e81110
     ---> 9fc79785a4c4
    

    When you are comfortable with the image that you built, redeploy the containers that suffered image changes(note that if you run docker-compose up, only the web-app is redeployed):

    docker-compose up -d  
    
    stack-example_mariadb_1 is up-to-date
    Recreating stack-example_webapp_1 ... done
    

    Option B:

    If your confident with the changes that you performed on the Dockerfile, go for the one-liner with the --build option:

    docker-compose up -d --build 
    
    Building webapp
    Step 1/4 : FROM nginx
     ---> 2622e6cca7eb
    Step 2/4 : RUN echo "Hello"
     ---> Using cache
     ---> ad39a2920d75
    Step 3/4 : RUN echo "from web-appi"
     ---> Running in 8cbd41c34d35
    from web-appi
    Removing intermediate container 8cbd41c34d35
     ---> ed7bc1950d4c
    Step 4/4 : RUN echo "change 2"
     ---> Running in f52be965609f
    change 2
    Removing intermediate container f52be965609f
     ---> da600ed3f0ed
    Successfully built da600ed3f0ed
    Successfully tagged stack-example_webapp:latest
    Building mariadb
    Step 1/2 : FROM mariadb
     ---> 8075b7694a2d
    Step 2/2 : RUN echo "hello from mariadb"
     ---> Using cache
     ---> 89f75af407fe
    Successfully built 89f75af407fe
    Successfully tagged stack-example_mariadb:latest
    Recreating stack-example_webapp_1 ... 
    Recreating stack-example_webapp_1 ... done
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search