Skip to main content

๐Ÿณ Sample Dockerfile - 1

# ๐Ÿ› ๏ธ Use an official base image (e.g., Ubuntu 20.04)
FROM ubuntu:20.04

# ๐ŸŒ Set environment variables
ENV MY_ENV_VAR=value

# ๐Ÿ“ฆ Install system packages and dependencies
RUN apt-get update && \
    apt-get install -y package1 package2 && \
    apt-get clean

# ๐Ÿ“‚ Copy files or directories from the host to the image
COPY ./local-files /app/

# ๐Ÿข Set the working directory for subsequent commands
WORKDIR /var/www/html

# ๐ŸŒ Expose a port on the container
EXPOSE 80

# ๐Ÿƒ Define a default command to run when the container starts
CMD ["./start.sh"]

# ๐Ÿš€ Start nginx when the container runs
CMD ["nginx", "-g", "daemon off;"]

# ๐Ÿ—๏ธ Define an entry point for the container
ENTRYPOINT ["/entrypoint.sh"]

# ๐Ÿฉบ Define a healthcheck command
HEALTHCHECK CMD curl -f http://localhost/ || exit 1

# ๐Ÿท๏ธ Add metadata to the image
LABEL version="1.0" maintainer="Your Name <your.email@example.com>"

# ๐Ÿ’พ Specify a volume that can be mounted by the container
VOLUME ["/data"]

# ๐Ÿ‘ค Create a new user with specific user and group IDs
RUN groupadd -r mygroup && \
    useradd -r -g mygroup myuser

# ๐Ÿšซ Switch to a non-root user
USER myuser

# ๐ŸŒ Copy files from a remote URL to the image
ADD http://example.com/file.tar.gz /tmp/

# ๐Ÿงน Remove temporary files and clean up
RUN rm -rf /tmp/*

# ๐Ÿ’ป Execute a shell command during build
SHELL ["/bin/bash", "-c"]

# โš™๏ธ Set a build-time ARG (can be passed during the build)
ARG MY_ARG=value

# ๐Ÿ“‚ Create a mount point for a named volume
VOLUME /myvolume

# ๐Ÿ“ฃ Set the shell form of ENTRYPOINT
ENTRYPOINT echo "Hello, World!"

# ๐Ÿ—๏ธ Use multi-stage builds
FROM builder AS intermediate
RUN build-something

# ๐Ÿ“ค Copy artifacts from the intermediate stage
COPY --from=intermediate /app/artifacts /final/

# ๐Ÿ‘ฅ Define the default user for the image
USER nobody:nogroup

๐Ÿณ Sample Dockerfile - 2

# ๐Ÿ› ๏ธ Use Amazon Linux 2 base image
FROM amazonlinux:2

# ๐Ÿ“ฆ Update repositories and install PHP from Amazon Linux Extras
RUN amazon-linux-extras enable php8.2 && \
    yum install -y php php-cli php-common php-json php-mbstring php-pdo php-mysqlnd php-opcache php-xml php-gd php-curl php-zip

# ๐Ÿ“ฆ Install Apache and other dependencies
RUN yum update -y && \
    yum install -y httpd curl zip unzip \
        libpng-devel zlib-devel libxml2-devel libzip-devel libonig-devel && \
    yum clean all

# ๐ŸŽผ Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# ๐Ÿ“ฆ Enable PHP and install PHP, Apache, and necessary development tools for Kafka
RUN amazon-linux-extras enable php8.2 && \
    yum install -y php php-cli php-common php-json php-mbstring php-pdo php-mysqlnd php-opcache php-xml php-gd php-curl php-zip httpd librdkafka-devel php-pear php-devel gcc make && \
    pecl install rdkafka && \
    echo "extension=rdkafka.so" > /etc/php.d/50-rdkafka.ini

# ๐Ÿ“ฆ Install other dependencies for Kafka
RUN yum install -y curl zip unzip libpng-devel zlib-devel libxml2-devel libzip-devel libonig-devel && \
    yum clean all

# ๐Ÿ‘ฅ Create Apache user and group
RUN groupadd -r www-data && useradd -r -g www-data www-data

# โš™๏ธ Enable Apache modules
RUN sed -i 's/^#LoadModule rewrite_module/LoadModule rewrite_module/' /etc/httpd/conf.modules.d/00-base.conf && \
    sed -i 's/^#LoadModule negotiation_module/LoadModule negotiation_module/' /etc/httpd/conf.modules.d/00-base.conf

# ๐Ÿ“ Redirect Apache logs to stdout and stderr
# RUN ln -sf /dev/stdout /var/log/httpd/access_log && \
#     ln -sf /dev/stderr /var/log/httpd/error_log

# ๐Ÿ”„ Update laravel-kafka-logger version
RUN composer require hhxsv5/laravel-kafka-logger:^1.0

# ๐Ÿ“‚ Copy your custom httpd.conf file to the container
COPY httpd.conf /etc/httpd/conf/httpd.conf

# ๐Ÿข Set working directory
WORKDIR /var/www/html

# ๐Ÿ“‚ Copy the application files to the working directory
COPY . /var/www/html/

# ๐Ÿ“‚ Copy env
#COPY .env.example .env

# ๐Ÿ”’ Set permissions for the PID file and directory
RUN chown -R www-data:www-data /var/www/html /run/httpd

# ๐Ÿงน Remove existing Apache PID file if it exists
RUN rm -f /run/httpd/httpd.pid

# โŒ Check for and kill existing Apache processes
RUN pkill httpd || true

# ๐Ÿ”’ Set permissions for the log directory and error log file
RUN chown -R www-data:www-data /var/log/httpd
RUN touch /var/log/httpd/error_log
RUN chown www-data:www-data /var/log/httpd/error_log

# ๐Ÿ“ฆ Install all PHP dependencies
RUN composer install

# ๐Ÿ”‘ Key generate command
RUN php artisan key:generate --no-interaction

# ๐Ÿ”‘ List of credentials
#RUN php artisan app:creds --list

# ๐Ÿ”’ Set permissions for the Laravel storage folder
RUN chmod -R 777 /var/www/html/storage
RUN chmod -R 777 /var/www/html/bootstrap/cache

# ๐Ÿ‘ฅ Set the user to run as Apache user
USER www-data

# ๐ŸŒ Expose the port Apache is reachable on
EXPOSE 8001

# ๐Ÿš€ Start Apache service
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
These Dockerfiles are examples of how to create Docker images for different environments, using various features and best practices. They demonstrate the use of environment variables, multi-stage builds, security practices by switching to non-root users, and exposing necessary ports for web services.