Creating Dockerfiles, Dockerignore, and Docker Compose

A Dockerfile is a simple text document that contains all the commands that a user could call on the command line to assemble an image. Let's show you how to create one.

One of the best ways to automate Docker to build images automatically is by using a Dockerfile. A Dockerfile is a simple text document that contains all the commands that a user could call on the command line to assemble an image.

In this tutorial, we shall show you a step-by-step process of creating a Dockerfile for nodeJS application, creating Dockerignore, and docker-compose.

Creating a Dockerfile, Dockerignore, and Docker Compose

1. Dockerfile

We shall create the Dockerfile inside the source directory.

vim Dockerfile

First of all, define from what image we want to start the build. Here I have added my alpine Linux docker image, which includes nodejs10 and NPM. Follow our Docker image and docker hub guide to create docker images and pull them to the docker hub.

FROM ddarshana/alpinenode10

The MAINTAINER command shows the author of the generated images.

MAINTAINER darshana (D.Darshana@fosslinux.com)

The RUN command is used to execute any commands. Here we install a curl package on Alpine Linux.

RUN apk add --update curl && rm -rf /var/cache/apk/*

Create a new Directory called App, which is going to hold the application code inside the image.

RUN mkdir /app

The WORKDIR directive is used to set where the command defined with CMD is to be executed.

WORKDIR /app

Copy package.json file.

COPY package.json .

Install nodeJS application and dependencies.

RUN npm install

Bundle app source.

COPY . .

Download the environment variable file. In our case, we have a separate location to store global configurations.

RUN curl -O https://demofiles.fosslinux/nodejs/.env

Run Application, and it will start on the default 3000 port.

CMD ["npm", "start"]

Here is our full Dockerfile.

FROM ddarshana/alpinenode10

MAINTAINER darshana (D.Darshana@fosslinux.com)

# Install "curl" package
RUN apk add --update curl && rm -rf /var/cache/apk/*

# Create app directory
RUN mkdir /app

WORKDIR /app

# copy package.json

COPY package.json .

# Install app dependencies
RUN npm install

# Bundle app source
COPY . .

# Download environment variable file from our network location

RUN curl -O https://demofiles.fosslinux/nodejs/.env

# Run APP
CMD ["npm", "start"]

Save and exit the file. Here is my NodeJs App Code Directory.

Nodejs APP Code
Nodejs APP Code

2. Creating a Dockerignore file

Using the “dockerignore” file prevents our local modules and other unwanted files from being copied onto the Docker image and the possibility of overwriting modules installed within your image. We highly recommend you to use it along with Dockerfiles.

The Dockerignore file should be in the same directory as the Dockerfile.

vim .dockerignore

Here we added the following contents:

.env
Dockerfile
node_modules
test
.vscode
.eslintrc.js

Save and exit the file.

Build a Docker image

docker build . -t fosslinuxdemo/nodejsapp

Build Image
Build Image

It should take some time to complete the process. Please be patient.

Image Created
Image Created

List Docker images

Run the following the command to list images:

docker images

List Images
List Images

Run Docker image

docker run -itd -p 3000:3000 fosslinuxdemo/nodejsapp

-itd:- executes the container in the background

-p:- flag redirects a public port to a private port inside the container

4. Docker Compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, we shall use a YAML file to configure the application services. After that, with a single command, we shall create and start all the services from our configuration.

Install Compose

Before installing the composer, you need first to install Docker.

Run this command to download the current stable release of Docker Compose:

curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Apply executable permissions to the binary:

chmod +x /usr/local/bin/docker-compose

Test the installation:

docker-compose --version

Docker Composer Install
Docker Composer Install

5. Installing WordPress and MySQL using Docker Compose

Create a folder for your project:

mkdir /opt/fosslinuxdemo

Go to the created directory:

cd /opt/fosslinuxdemo

Here we shall create data directories for our MySQL container and WordPress container.

mkdir data
mkdir html

We will mount the above directories to our containers.

Create a composer file:

vim docker-compose.yml

Here is our docker-compose file. Copy the following contents to your file.

version: '3.7'
services:
db:
container_name: fosslinux-mysql
restart: always
image: mysql:5.7
volumes:
- /opt/fosslinuxdemo/data:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
ports:
- "3306:3306"
networks:
- fosslinux_net

wp:
container_name: fosslinux-wp
restart: always
volumes:
- "/opt/fosslinuxdemo/html:/var/www/html"
depends_on:
- db
image: wordpress
ports:
- "80:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- fosslinux_net
networks:
fosslinux_net:

Create file and Directories
Create file and Directories

Note the version number. More details of versioning @ composer file version.

version: '3.7'
services:
db:

A service in Compose is a running container, and service definitions specify information about how each container will run. Our “db” service definition has various options.

container_name: fosslinux-mysql
  • Specifies a name for the container
restart: always
  • Defines the container restart policy
image: mysql:5.7
volumes:https://hub.docker.com/_/mysql
- /opt/fosslinuxdemo/data:/var/lib/mysql

Here we mount volume “/opt/fosslinuxdemo/data ” to the “/var/lib/mysql “directory on the container. Generally, it is the standard data directory for MySQL on most distributions.

command: --default-authentication-plugin=mysql_native_password

This option specifies a command to override the default CMD instruction for the image.

environment:

Define environment variables. Here we set MySQL variables.

 ports:
- "3306:3306"
  • Port mapping.
networks:
- fosslinux_net
  • Defines a network for the container
depends_on:
- db

This represents our containers will start in order of dependency, with the “wp” container beginning after the “db” container.

Now we can create our environment.

 docker-compose up -d

-d flag, which will run containers in the background.

Creating Environment
Creating Environment

List docker containers:

docker ps -a

Created Containers
Created Containers

Check logs of WordPress Container:

docker logs -f fosslinux-wp

Now go to the web browser and type your IP to get the WordPress installation window.

WordPress Installation
WordPress Installation

Proceed to click on, continue, and complete the WordPress installation process.

That’s all about working with Dockerfiles and Docker Compose. I hope you enjoyed the article.

Darshana
Hey! I'm Darshana, a Linux / DevOps Engineer and also a contributor to FOSS Linux. I enjoy working on various kind of Linux distributions and cloud technologies. During my free time, I love to swim and hike across nature trails. Linux is my love and I'm here to share all my learnings with all of you! Hope you enjoyed reading my article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,521FansLike
364FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to setup FTP Server on CentOS

You are allowed to use any one of the FTP servers as you like. However, in this tutorial, we will be installing and using vsftpd, short for Very Secure FTP Daemon. It is a fast, stable, and secure FTP server that will help you transfer files to and from a remote system in a jiffy.

CentOS Vs. Ubuntu Server : Everything You Need to Know

Choosing the perfect Linux distribution to set up your server can be confusing since Linux provides a limitless number of options. The main reason behind these many distributions is because Linux is an opensource platform. Anybody with the required skills can contribute to the development or build and release their distribution. Currently, there are more than 600 Linux Desktop and Server distributions in the market.

Top 10 Reasons to use Xfce as your Desktop Environment

There are many choices for desktop environments for Linux based operating systems. Mainly, you can install any DE of your choice on most of the Linux based distributions, even if they are not offered as a package officially. In our recent articles, we discussed the best of KDE and Cinnamon. In this article, we wish to present to you the top reasons why you should consider Xfce as your desktop environment.

The 10 Best Linux Network Monitoring Tools

Having total control over your network is essential to prevent programs from overusing your network resources and slowing down the overall performance. This is why you should install a network monitoring tool on your system, giving you a visual overview of everything that's happening on your network. Networking Tools are like swiss-knife for the system administrators for troubleshooting system issues.

How to install CouchPotato on Ubuntu

Downloading movies and copying them over to your home server can get frustrating, especially if you are doing it daily! What if you have an option to download videos automatically, quickly, and above all with excellent quality. CouchPotato allows you to download movies easily once they are available and released automatically.

Top 20 Rsync Examples in Linux

The Rsync (remote sync) command is a Linux/Unix utility used to synchronize and copy files and directories either locally or remotely. Most Linux geeks use Rsync to mirror, backup or migrate data across folders, across disks and networks. One notable feature with the Rsync command is that it uses the "delta transfer algorithm."

MUST READ

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

5 Things to do when your Linux system GUI freezes

All operating systems freeze at some point and, Linux distributions are no exceptions. In this post, we are going to look at five methods you can apply to your frozen system before thinking of plugging out the power cable or holding down the power button to power off the PC.

6 cool and fun Linux commands you don’t want to miss

The real purpose of this article is to help Linux newbies get comfortable and confident with the Linux command-line.  While knowledge and comfort of the Linux GUI allow great power, it is the mastery of the command line, or CLI, affords the Linux user unlimited power and certifies them as a Linux power user well on their way to becoming an expert.

Top 5 reasons to switch from Windows to Linux right now

Do you love Windows or Linux? Being on a Linux website, it's not hard to take a guess, but what are the strong reasons to switch to Linux? Linux is a 100% free OS. Is that reason alone enough? Let's find out! Never have there been more compelling reasons to make the switch. Here are just five great reasons for users to create that magical move from the wayward world of Windows to the lauded land of Linux.

Pop!_OS 20.04 Review: Professional Linux Distribution Ever Made

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.