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,414FansLike
377FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

Mastering the SQLite Database in Python

SQLite is a relational database management system based on the SQL language; it is a serverless, Zero-configuration database engine. It is one of the...

Basics of Working with the SQLite Database in Python

A database is one of the most useful and popular files for storing data; they can be used to store any kind of data, including text, numbers, images, binary data, files, etc. SQLite is a relational database management system based on the SQL language. It is a C library, and it provides an API to work with other programming languages, including Python. It does not require a separate server process to be run as needed in large database engines like MySQL and Postgresql.

5 Ways to Check the Linux Version

When most people talk of Linux, they are always referring to a Linux distribution. However, this is not the case. Linux itself is a kernel which acts as a bridge between user applications and the hardware. When we talk of a Linux distribution, we refer to an operating system developed from the Linux kernel. A distribution comes with a package manager, pre-installed applications, a Desktop Environment, and several more features.

Getting Started with Linux Operating System

The Linux operating system brings forth a vibrant mix of features and security, making it the best alternative to macOS or Windows operating systems. In this post, we will give you a master guide on Getting started with Linux systems - taking you from a complete beginner to a level where you can begin testing the various Linux distributions available with much ease.

How to Create a Comprehensive Mail Server on Ubuntu

Postal is a free and open-source mail server used to send and receive emails. It comes loaded with tons of excellent features and functionalities, making it extremely popular among large organizations as well as in enterprise settings.

The 10 Best Linux Performance Monitoring Tools

Do you want to monitor the performance of your Linux system? Are you looking for some powerful performance monitoring tools to help you out? If you agree, it's your day as we have put together a detailed list of the ten best Linux performance monitoring tools.

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
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.

What is the difference between Linux and Unix?

When discussing Linux and Unix with average users, it's not uncommon that they will sometimes mistakenly interchange the terms Linux and Unix.  The two are not the same.  Though they share similarities in their overall structures and toolkits, they are decidedly not the same.

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.

10 ways to use the Chown command with examples

Any file in a Linux system belongs to a particular owner(s) and a group. It is one of the many features that maintain security in Linux systems. For example; If a file belongs to say UserA, then UserB cannot manipulate the data depending on any additional permissions set unless the user has permission to do that.

6 Best Linux Distributions for Beginners

Making the shift to Linux is not as complicated as some people make it out to be. There is a variety of Linux distributions available that cater to the needs of people who are planning on making the jump to Linux from any other operating system.