๐ณ Sample Dockerfile - 1
Copy
# ๐ ๏ธ 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
Copy
# ๐ ๏ธ 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.
