Docker

Docker tutorial – Build Docker image for your Database application

Docker as we know, is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud. In this post, we are going to take look at how to build a Docker image for Database application.

Step #1. Setup Docker

  1. From the docker site, install the latest version of the docker for your platform. Docker is available in two editions: Community Edition (CE) and Enterprise Edition (EE). Docker Community Edition (CE) is ideal for developers and small teams looking to get started with Docker and experimenting with container-based apps. Docker Enterprise Edition (EE) is designed for enterprise development and IT teams who build, ship, and run business-critical applications in production at scale.
  2. Once the installation of docker is over, check the installation by running following command docker run hello-world:

    Validate Docker Installation
    Image – Validate Docker Installation
  3. Run docker –version to check the version of the docker you’re running.

    Check Docker version
    Image – Check Docker version

OK, now we have got the docker setup, the next step is to define the docker container.

Step #2. Create Dockerfile for our container

    1. Dockerfile will define what goes on in the environment inside the container. Access to resources like networking interfaces and disk drives is virtualized inside this environment, which is isolated from the rest of the system, so you have to map ports to the outside world, and be specific about what files you want to “copy in” to that environment. So that you can expect that the build of your app defined in this Dockerfile will behave exactly the same wherever it runs.
    2. Common Dockerfile instructions start with RUN, ENV, FROM, MAINTAINER, ADD, and CMD, among others.
      • FROM – Specifies the base image that the Dockerfile will use to build a new image. As an example, we are going to use phusion/baseimage as our base image (this is minimal Ubuntu-based image)
      • MAINTAINER – Specifies the Dockerfile Author Name and his/her email.
      • RUN – Runs any UNIX command to build the image.
      • ENV – Sets the environment variables. For this post, JAVA_HOME is the variable that is set.
      • CMD – Provides the facility to run commands at the start of the container. This can be overridden upon executing the docker run command.
      • ADD – This instruction copies the new files, directories into the Docker container file system at a specified destination.
      • EXPOSE – This instruction exposes a specified port to the host machine.
    3. To begin with, create a new folder and then create a file in it named “Dockerfile” with the following content. Here I have used Postgres as an example.
      # Dockerfile
      FROM postgres:9.5
      MAINTAINER  Author Name [email protected]
    4. Once we have the baseimage set, the next step is to do initialization steps like creation of db,users etc.,
      #init.sql to create database
      COPY init.sql /docker-entrypoint-initdb.d/
      # Adjust PostgreSQL configuration to accept remote connections 
      RUN echo "host all  all    0.0.0.0/0  md5" >> /var/lib/postgresql/data/pg_hba.conf
      RUN echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf
    5.  Below is sample init.sql that would have statements to create the database like the one below
      CREATE DATABASE testdb;
    6. Then coming back to Dockerfile, add expose port command as final statement
      #expose port
      EXPOSE 5372
      
    7. Our Docker file would now be looking like the one below
FROM postgres:9.5
#init.sql to create database
COPY init.sql /docker-entrypoint-initdb.d/


# Adjust PostgreSQL configuration to accept remote connections 
RUN echo "host all  all    0.0.0.0/0  md5" >> /var/lib/postgresql/data/pg_hba.conf

RUN echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf

# expose port
EXPOSE 5432

Step #3. Build Docker Image

Now that we have completed Dockerfile, next step is to build Docker image by docker build command 

docker build -f DBDockerfile -t postgresdb .

Here -t specifies the name of the image and -f specifies the name of the Dockerfile. Image tag I have left it empty.

Docker build complete
Image – Docker build complete

Congrats! You’ve successfully built a container for your Database application.

Step #4. Test Docker Image

To run the Docker image, execute the following commands

docker run -p 5372:5372 postgresdb

Here -p specifies the port container: host mapping.

Congrats! You’ve successfully built and run container for your Database application.

Step #6. Docker Compose Configuration

If you’re looking for running multi-container applications using Docker Compose tool then the configuration is as easy, there would be YAML file to configure your application’s services/networks/volumes, etc., Then, with a single command, you can create and start all the services from the compose configuration.

Here are the key steps :

  1. Define Dockerfile for your container(s).
  2. Define docker-compose.yml for the services that make up your application services.
  3. Run docker-compose up and Compose starts and runs your entire app.

Sample docker-compose.yml for Angular UI application would look like this:

version: '3'
services: 
  postgresdb:
   build:
   context: .
   dockerfile: DBDockerfile
    volumes:
           - 'postgresdb:/var/lib/postgresql/data'
    environment:
           POSTGRES_USER: postgres
           POSTGRES_PASSWORD: postgres
           POSTGRES_DB: testdb
    ports:
           - '5432:5432'
    healthcheck:
            test:
                - CMD-SHELL
                - 'pg_isready -U postgres'
            interval: 10s
            timeout: 5s
            retries: 5
    networks:
     - samplenet
networks:
    samplenet: null
volumes:
    postgresdb: {}

For database services, volumes need to be mounted so that data can be persisted. Also, you can have healthcheck section to check the health of the database at regular intervals.

To scale services using Docker compose refer here. There is much more to the Docker platform than what was covered here, but now you would have got a good idea of the basics of building containers for an application.

Like this post? Don’t forget to share it!

Additional Resources:

Summary
Docker tutorial - Build Docker image for your Database application
Article Name
Docker tutorial - Build Docker image for your Database application
Description
In this post, we are going to take look at how to build Docker image for Database application.
Author
Publisher Name
upnxtblog
Publisher Logo

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

school Previous post TOP 84 Coursera Specializations to boost your career
school Next post TOP 25 Udemy Machine Learning courses (Level – Intermediate)