Cart

    Sorry, we could not find any results for your search querry.

    Containerizing your web application

    In this era of digital transformation, it is becoming increasingly important to quickly and efficiently deploy and maintain your applications. Containerization is a technology that can help with this. By containerizing your application, you make use of a 'container' that contains all the necessary components to run your application. This allows your application to be easily executed on any system that supports containerization, such as our Kubernetes platform.

    In this guide, we will demonstrate how to containerize your application using Docker and then use it on a Kubernetes cluster, using a dynamic website (with Apache, MariaDB, and PHP) as an example.

    • For the steps in this guide, you will need Docker (engine or desktop). Refer to our Docker installation guides for Linux, Windows, and MacOS.
    • If you prefer to keep your image private and not host it on a public registry, we recommend using a private container image registry such as a private Docker Registry. See our Docker Registry guide for Kubernetes.

    Containerizing Your Application

     

    Step 1

    Create and open a file named Dockerfile (without an extension) for your application, for example, on Linux:

    nano Dockerfile

    You are free to place the Dockerfile in a directory of your choice.


     

    Step 2

    In this file, you will provide information on how the container image should be built. The Dockerfile should include the following steps, as explained in the code:

    • Specify the base operating system on which the installation will be performed, such as AlmaLinux or Ubuntu.
    • Install the necessary software and dependencies, such as Apache and PHP.
    • Copy your website files to the appropriate location inside the container.
    • Make any configuration adjustments to the software used, such as configuring Apache to host the website.

    Note that you should modify the information below to match your own configuration.

    # The base operating system on which the image is built.
    # Available operating systems can be found at https://hub.docker.com/search?image_filter=official&q=&type=image
    # Specific version numbers like Ubuntu:22.04 are allowed
    FROM almalinux:9
    
    # Install the necessary software and dependencies
    RUN dnf -y update && \
        dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm && \
        dnf -y install https://rpms.remirepo.net/enterprise/remi-release-9.rpm && \
        dnf -y module install php:remi-8.2 && \
        dnf -y install httpd php-common php-cli php-fpm && \
        dnf -y install mariadb-server mariadb 
    
    # Copy the website files and database to the container
    COPY . /var/www/html/
    COPY mydatabase.sql /tmp/
    
    # Start mariadb
    CMD mariadb start
    
    # Create the database and user (modify these details as needed)
    CMD mysql -e "CREATE DATABASE mydatabase; \
                  GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost' IDENTIFIED BY 'mypassword';" 
    # Import the database dump
    CMD mysql mydatabase < /tmp/mydatabase.sql
    
    # Configure Apache to host the website
    RUN sed -i 's/Listen 80/Listen 8080/' /etc/httpd/conf/httpd.conf && \ 
        sed -i 's/#ServerName www.example.com:80/ServerName localhost:8080/' /etc/httpd/conf/httpd.conf && \
        echo "IncludeOptional /etc/httpd/conf.d/*.conf" >> /etc/httpd/conf/httpd.conf
    
    # Set the correct permissions for the website files
    RUN chown -R apache:apache /var/www/html 
    
    # Start the Apache service
    CMD httpd start 
    
    EXPOSE 8080 CMD 
    
    ["/usr/sbin/httpd", "-D", "FOREGROUND"] 

     

    Step 3

    Build the container image based on the created Dockerfile using the 'docker build' command. Replace 'my-website' with another name of your choice for your application and 'latest' with a version number.

    docker build -t my-website:latest .

    The dot at the end of the command indicates that the Dockerfile should be used in the current directory. If the Dockerfile is located in a different directory, you can use the following syntax:

    docker build -t my-website:latest -f /path/to/Dockerfile .
     

    The 'docker build' command creates the container image on the local Docker daemon, not in the current directory. This means that you won't see the container image in the directory where you executed the command, but it will be stored in the internal image cache of the Docker daemon. You can view these images with the following command:

    docker images

    This will display a list of all the local container images currently present in the internal image cache of the Docker daemon. The newly created container image should appear there with the name and tag you specified in the 'docker build' command.

    This guide mainly focuses on using the containerized application on a Kubernetes platform. However, if you want to share or store the container image for other purposes, you can export it to a tar file using the 'docker save' command and then import it at another location using the 'docker load' command.

    docker save -o <save image to path> <image name>
    docker load -i <path to image tar file>
    

     

    Step 4

    Use the Docker 'push' command to send the container image to a registry so that you can use it on a Kubernetes platform.

    In many cases, you may prefer to keep your container image private and push it to your own container image registry (such as a private Docker registry). To push a containerized application to your own registry, the following three steps are required:

    • Connect to your own registry (replace example.com with the hostname of your server):
      docker login example.com
      
    • Tag your container image with the registry URL (replace my-image with the name of your containerized application from step 3):
      docker tag my-image example.com/my-image
    • Push your container image to the registry:
      docker push example.com/my-image 

    Alternatively, push your container to the public Docker registry if you want your container image to be publicly accessible:

    docker push my-website:latest

    Replace my-website:latest with the name and tag of your application created in step 3.

    Great! You have created your container and it is available. But how do you deploy it? We explain that in our guide 'Deploying Your Containerized Image in a Kubernetes Cluster'.

    Need help?

    Receive personal support from our supporters

    Contact us