DEV Community

InterSystems Developer for InterSystems

Posted on • Originally published at community.intersystems.com

Introduction to Docker - Part2 (Docker Compose, Docker File, Docker Volume)

Hi Community,

In my previous article, we learned topics listed below:

  1. What is Docker?
  2. Some of the Docker benefits
  3. How does Docker work?
  4. Docker Image
  5. Docker Container
  6. Docker Image repository
  7. InterSystems's Docker image repository
  8. Docker Installation
  9. Docker Basic Commands
    1. Running IRIS community edition by using Docker
    2. Docker Desktop GUI

In this article, we will cover the following topics:

  1. Use of Docker Compose file (a YAML file)
  2. Use of Docker file (employed to build a Docker image)
  3. Use of Docker volume

So let's begin.

1. Use of Docker Compose file (

a YAML file)

Docker Compose is a tool developed to help define and share multi-container applications. With Compose, we can create a YAML file to define the services, and with a single command, we can spin everything up or tear it all down.

A big advantage of using Compose is the ability to define your application stack in a file and keep it at the root of your project repo. You can also enable somebody else to contribute to your project with ease. They would only need to clone your repo and start the compose app. 

In my previous article, we used the command mentioned below to create and start the container with the InterSystems community image:

docker run -d -p 52773:52773 intersystemsdc/iris-community 

At this point, let's modify this command and add a container name, map more ports, and set the restart option:

docker run -d -p 52773:52773 -p 53773:53773 -p 1972:1972 --name iris --restart=always intersystemsdc/iris-community

Image description

Let me break down the abovementioned command for you:

#docker run command to create and start the container
docker run 
#-d -an option used to start container in deattach mode
-d 
#-p -an option is used to map the ports
-p 52773:52773 
-p 53773:53773 
-p 1972:1972 
#name of the container
--name iris 
#set the restart option to always
--restart=always 
#base image
intersystemsdc/iris-community

Create the Compose file {#create-the-compose-file}

In the root folder, create a file named docker-compose.yml a and write the abovementioned commands as stated below:

#specify docker-compose version
version: '3.6'
#services/container details
services:
  #Name of the container
  iris:
    #Base Image
    image: intersystemsdc/iris-community      
    #set restart option
    restart: always
    #port mapping
    ports:
      - 55036:1972
      - 55037:52773
      - 53773:53773

The mapping of the docker run command and a docker-compose file is illustrated below:

Image description
A Docker-compose file snapshot is demonstrated beneath:

Image description

In order to run the docker-compose file code, we will use the docker-compose up command:

docker-compose up -d
  • -d or --detach:  an option that runs the command in the background and returns control to the terminal.  

Image description

The container is started. Let us run "docker ps" command to list down running containers

Image description

As you can see, we are getting the same result with the docker-compose file.

 

Create and Start multiple containers

With the help of docker-compose not only can we run multiple containers but also organize and add more commands to it.

E.g., in the following docker-compose file we are running the MongoDB container along with the iris container:

#specify docker-compose version
version: '3.6'
#services/container details
services:
  #Name of the container
  iris:
    #Base Image
    image: intersystemsdc/iris-community      
    #set restart option
    restart: always
    #port mapping
    ports:
      - 55036:1972
      - 55037:52773
      - 53773:53773
  
  #start MongoDB container
  mongodb:
    image: mongo 
    ports:
    - 27017:27017 

Let us run the docker-compose up command

Image description

Both MongoDB and iris containers have been created and started now.

2. The Docker file

Docker can automatically build images by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. 

So, our first question is simple. what is a Dockerfile? It is what Docker uses to build the image itself. The Dockerfile is essentially the build instructions of how to create the image.

The advantage of a Dockerfile over simply storing the binary image is that the automatic builds will ensure you have the latest version available. It is a good thing from a security perspective because you want to ensure you are not installing any vulnerable software.


Common Docker file Commands

Below you can find some of the most commonly used docker commands. Please note all of the docker commands must be in Capital letters.

FROM 

The first one is the FROM command, which tells what image was based on. This is the multi-layered approach that makes Docker so efficient and powerful. In this instance, the iris-community Docker image was used, which references a Dockerfile once more to automate the build process.

FROM intersystemsdc/iris-community

WORKDIR

This command is used to set up a working directory in which we will copy files.

E.g the undermentioned command will set /opt/irisbuild as a working directory:  

WORKDIR /opt/irisbuild

 

COPY

The COPY command is as simple as it sounds.  It can copy files to the container.  

Usually, we copy custom configuration files, applications source files, data files, etc.

#coping Installer.cls to a root of workdir. Don't miss the dot, here.
COPY Installer.cls .

#copy source files from src folder to src folder in workdir in the docker container.
COPY src src

#copy source files from data/fhir folder to fhirdata folder in the docker container.
COPY data/fhir fhirdata

 

ENV

This sets the environment variables, which can be used in the Dockerfile and any scripts it calls.

The ENV instruction defines environment variable <key> to the value <value>

ENV USER_ID "SYSTEM"
ENV USER_PASSWORD "MANAGER"

<

RUN

The RUN command is used to execute commands during the image-building process.

#here we give the rights to irisowner user and group which are run IRIS.
RUN chown ${ISC_PACKAGE_MGRUSER}:${ISC_PACKAGE_IRISGROUP} /opt/irisapp   
#start IRIS and run script in iris.script file 
RUN iris start IRIS \    
    && iris session IRIS < /tmp/iris.script

 

USER 

By default, containers run as root, which gives them complete control of the host system. As container technology matures, more secure default options may become available. For now, requiring root is dangerous for others and may not be available in all environments. Your image should use the USER instruction to specify a non-root user for containers to run as. If your software does not create its own user, you can create a user and group in the Dockerfile.

#here we switch user to a root to create a folder and copy files in docker.
USER root 

WORKDIR /opt/irisapp

#switching user from root to irisowner, to copy files
USER irisowner 

COPY src src

For more details, please read Docker official documentation

3. Docker Volume

A Docker volume is an independent file system entirely managed by Docker. It exists as a standard file or directory on the host where data persist.

The purpose of using Docker volumes is to keep data outside the container to make it available for backing up or sharing.

Docker volumes depend on Docker’s file system and are the preferred method of persisting data for Docker containers and services. When a container is started, Docker loads the read-only image layer, adds a read-write layer on top of the image stack, and mounts volumes onto the container filesystem.

we use -v or --volume flag to allow ourselves to mount local files into the container.

Volumes are the preferred mechanism for persisting data generated and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker. Volumes have several advantages over bind mounts:

  • Volumes are easier to back up or migrate than bind mounts.
  • You can manage volumes using Docker CLI commands or the Docker API.
  • Volumes work on both Linux and Windows containers.
  • Volumes can be more safely shared among multiple containers.
  • Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
  • New volumes can have their content pre-populated by a container.
  • Volumes on Docker Desktop have much higher performance than bind mounts from Mac and Windows hosts.

In addition, volumes are often a better option than persisting data in a writable layer of the container because a volume does not increase the size of the containers while using it. Also, the contents of the volume exist outside of the lifecycle of a given container.

Volumes on the Docker host

We can mention volumes under the services section in the docker-compose file.

#specify docker-compose version
version: '3.6'
#services/container details
services:
  #Name of the container
  iris:
    #Base Image
    image: intersystemsdc/iris-community      
    #set restart option
    restart: always
    #port mapping
    ports:
      - 55036:1972
      - 55037:52773
      - 53773:53773
    #create a volume
    volumes:
      - ./:/irisdev/app  

we have created irisdev/app volume.

 

Summary

Docker is a powerful tool that allows developers and IT teams to create, deploy, and run applications in a containerized environment. By providing portability, consistency, scalability, resource efficiency, and security, Docker makes it easy to deploy applications across different environments and infrastructures. With the growing popularity of containerization, Docker is becoming an essential tool for modern software development and deployment.

In this article, we learned how to use Docker compose (a YAML file that specifies the configuration options for each container in the application), Docker file (employed to build a Docker image) and Docker volume (a persistent data storage mechanism operated to share data between Docker containers and the host machine.)

 

Thank you for reading!

Top comments (0)