Building Custom Docker File: Examples for Various Application Stacks

Utsav Desai
8 min readMar 26, 2023

--

Dockerfile for NGINX

here’s a simple Dockerfile for NGINX:

# Use an official NGINX image as a parent image
FROM nginx:latest

# Copy the custom configuration file to the container
COPY nginx.conf /etc/nginx/nginx.conf

# Expose port 80 for NGINX to listen on
EXPOSE 80

# Start NGINX when the container launches
CMD ["nginx", "-g", "daemon off;"]

In this Dockerfile, we are using the official NGINX image as the parent image, copying our custom nginx.conf configuration file to the container, exposing port 80 for NGINX to listen on, and specifying the command to start NGINX when the container launches.

To build this Dockerfile, navigate to the directory containing the Dockerfile and run the following command:

docker build -t mynginx .

This will create a Docker image named mynginx based on the instructions in the Dockerfile. You can then run a container from this image using the docker run command:

docker run -p 80:80 mynginx

This will start a container from the mynginx image and map port 80 on the host machine to port 80 in the container, allowing you to access the NGINX web server in the container from your web browser.

Dockerfile for ReactJS and NodeJS

here’s a simple Dockerfile for React.js and Node.js:

# Use an official Node.js runtime as a parent image
FROM node:14-alpine

# Set the working directory to /app
WORKDIR /app

# Copy the package.json and package-lock.json files to the container
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code to the container
COPY . .

# Expose port 3000 for the application to listen on
EXPOSE 3000

# Start the application when the container launches
CMD ["npm", "start"]

In this Dockerfile, we are using the official Node.js 14 Alpine image as the parent image, setting the working directory to /app, copying the package.json and package-lock.json files to the container and installing dependencies using npm install. We then copy the rest of the application code to the container, expose port 3000 for the application to listen on, and specify the command to start the application when the container launches.

To build this Dockerfile, navigate to the directory containing the Dockerfile and run the following command:

docker build -t mynodeapp .

This will create a Docker image named mynodeapp based on the instructions in the Dockerfile. You can then run a container from this image using the docker run command:

docker run -p 3000:3000 mynodeapp

This will start a container from the mynodeapp image and map port 3000 on the host machine to port 3000 in the container, allowing you to access the Node.js application in the container from your web browser.

Dockerfile for Flutter App

here’s a simple Dockerfile for a Flutter app:

# Use an official Flutter image as a parent image
FROM cirrusci/flutter:stable

# Set the working directory to /app
WORKDIR /app

# Copy the pubspec.yaml and pubspec.lock files to the container
COPY pubspec.yaml pubspec.lock ./

# Install dependencies
RUN flutter pub get

# Copy the rest of the application code to the container
COPY . .

# Build the application for release
RUN flutter build apk --release

# Expose port 80 for the application to listen on (optional)
EXPOSE 80

# Start the application when the container launches
CMD ["flutter", "run", "--release"]

In this Dockerfile, we are using the official Flutter image from Cirrus CI as the parent image, setting the working directory to /app, copying the pubspec.yaml and pubspec.lock files to the container and installing dependencies using flutter pub get. We then copy the rest of the application code to the container and build the application for release using flutter build apk --release. Optionally, we can expose port 80 for the application to listen on, and specify the command to start the application when the container launches using flutter run --release.

To build this Dockerfile, navigate to the directory containing the Dockerfile and run the following command:

docker build -t myflutterapp .

This will create a Docker image named myflutterapp based on the instructions in the Dockerfile. You can then run a container from this image using the docker run command:

docker run -p 80:80 myflutterapp

This will start a container from the myflutterapp image and map port 80 on the host machine to port 80 in the container, allowing you to access the Flutter app in the container from your web browser.

Dockerfile for MongoDB App

here’s a simple Dockerfile for a MongoDB app:

# Use an official MongoDB image as a parent image
FROM mongo:latest

# Create the directory for the MongoDB data files
RUN mkdir -p /data/db

# Expose port 27017 for MongoDB to listen on
EXPOSE 27017

# Start MongoDB when the container launches
CMD ["mongodb"]

In this Dockerfile, we are using the official MongoDB image as the parent image, creating the directory for the MongoDB data files using mkdir -p /data/db, exposing port 27017 for MongoDB to listen on and specifying the command to start MongoDB when the container launches using mongodb.

To build this Dockerfile, navigate to the directory containing the Dockerfile and run the following command:

docker build -t mymongodbapp .

This will create a Docker image named mymongodbapp based on the instructions in the Dockerfile. You can then run a container from this image using the docker run command:

docker run -p 27017:27017 mymongodbapp

This will start a container from the mymongodbapp image and map port 27017 on the host machine to port 27017 in the container, allowing you to access the MongoDB app in the container.

Dockerfile for FireBase App

Here’s a simple Dockerfile for a Firebase app:

# Use an official Node.js runtime as a parent image
FROM node:14-alpine

# Set the working directory to /app
WORKDIR /app

# Copy the package.json and package-lock.json files to the container
COPY package*.json ./

# Install dependencies
RUN npm install -g firebase-tools
RUN npm install

# Copy the rest of the application code to the container
COPY . .

# Expose port 5000 for the application to listen on (optional)
EXPOSE 5000

# Start the application when the container launches
CMD ["firebase", "emulators:start", "--import=./firebase-seed-data"]

In this Dockerfile, we are using the official Node.js 14 Alpine image as the parent image, setting the working directory to /app, copying the package.json and package-lock.json files to the container and installing the Firebase tools and dependencies using npm install. We then copy the rest of the application code to the container and specify the command to start the Firebase emulators when the container launches using firebase emulators:start.

You’ll notice that we’re also specifying the --import=./firebase-seed-data option to the firebase emulators:start command. This is because Firebase emulators require some initial data to be imported into the emulator before they can be used. The firebase-seed-data directory contains the necessary data to get the emulators up and running.

To build this Dockerfile, navigate to the directory containing the Dockerfile and run the following command:

docker build -t myfirebaseapp .

This will create a Docker image named myfirebaseapp based on the instructions in the Dockerfile. You can then run a container from this image using the docker run command:

docker run -p 5000:5000 myfirebaseapp

This will start a container from the myfirebaseapp image and map port 5000 on the host machine to port 5000 in the container, allowing you to access the Firebase app in the container from your web browser.

Dockerfile for Mysql App

here’s a simple Dockerfile for a MySQL app:

# Use an official MySQL image as a parent image
FROM mysql:latest

# Set environment variables for the MySQL root user
ENV MYSQL_ROOT_PASSWORD mysecretpassword
ENV MYSQL_DATABASE mydatabase
ENV MYSQL_USER myuser
ENV MYSQL_PASSWORD myuserpassword

# Expose port 3306 for MySQL to listen on
EXPOSE 3306

# Copy the SQL dump file to the container
COPY dump.sql /docker-entrypoint-initdb.d/

# Start MySQL when the container launches
CMD ["mysqld"]

In this Dockerfile, we are using the official MySQL image as the parent image, setting environment variables for the MySQL root user, specifying the database name, username and password using ENV, exposing port 3306 for MySQL to listen on, copying the SQL dump file to the container and specifying the command to start MySQL when the container launches using mysqld.

To build this Dockerfile, navigate to the directory containing the Dockerfile and run the following command:

docker build -t mysqlapp .

This will create a Docker image named mysqlapp based on the instructions in the Dockerfile. You can then run a container from this image using the docker run command:

docker run -p 3306:3306 mysqlapp

This will start a container from the mysqlapp image and map port 3306 on the host machine to port 3306 in the container, allowing you to access the MySQL app in the container.

Dockerfile for PHP App

here’s a simple Dockerfile for a PHP app:

# Use an official PHP runtime as a parent image
FROM php:7.4-apache

# Set the working directory to /var/www/html
WORKDIR /var/www/html

# Copy the application code to the container
COPY . .

# Install PHP extensions required by the application
RUN docker-php-ext-install mysqli pdo pdo_mysql

# Expose port 80 for Apache to listen on
EXPOSE 80

# Start Apache when the container launches
CMD ["apache2-foreground"]

In this Dockerfile, we are using the official PHP 7.4 Apache image as the parent image, setting the working directory to /var/www/html, copying the application code to the container, installing the required PHP extensions using docker-php-ext-install and specifying the command to start Apache when the container launches using apache2-foreground.

To build this Dockerfile, navigate to the directory containing the Dockerfile and run the following command:

docker build -t phpapp .

This will create a Docker image named phpapp based on the instructions in the Dockerfile. You can then run a container from this image using the docker run command:

docker run -p 80:80 phpapp

This will start a container from the phpapp image and map port 80 on the host machine to port 80 in the container, allowing you to access the PHP app in the container.

--

--

Utsav Desai
Utsav Desai

Written by Utsav Desai

Utsav Desai is a technology enthusiast with an interest in DevOps, App Development, and Web Development.

No responses yet