AEM in docker

 Hello Everyone,

In this post we will see how we can run our aem server from docker. Before that why we really need docker, whenever we have a new team member, they will struggle for project setup. Now imagine we have a set up file we can share with any new member who is joining our team. They simply can start their work without wasting time on set up or we can directly start giving KT, so using docker we can achieve this we can create a docker image and we can share the image with team members.

What is docker image ?

A Docker image is a lightweight, standalone, and executable package that contains everything needed to run an application, including the application code, libraries, dependencies. Everything in one image.

How to create docker image ?

1st get docker installed on your machine/system docker toolbox is deprecated so use docker desktop software. 

You might face some challenges while installing and starting docker.

Install docker desktop

Enable hyper-v

Step-By-Step: Enabling Hyper-V for Use on Windows 11 (microsoft.com)

After docker is installed correctly create a Dockerfile. 

what is dockerfile?

Umm some instructions for creating docker image. It's a simple text file which will have command to build a docker image. Dockerfile name can be anything.

Now where to create a dockerfile, I am setting up docker image for author and publish environment.

Let's set up for aem author instance first. Below is my project structure, I have kept my aem jar, license and dockerfile in the same directory where my project is created.


my dockerfile looks like below

# Use an official AdoptOpenJDK runtime as a parent image
FROM adoptopenjdk/openjdk11:alpine-slim
# Set environment variables
ENV CQ_RUNMODE "author"
ENV CQ_JVM_OPTS "-Xmx2048m"
# Set the working directory
WORKDIR /usr/src/app
# Copy AEM quickstart and license file to the container
COPY cq-quickstart-6.5.0.jar ./
COPY license.properties ./
# Set up volume to share crx-quickstart directory with other containers
VOLUME /usr/src/app/crx-quickstart
# Copy the UI content and apps packages to the container
COPY ui.content/target/sampleproject.ui.content-0.0.1-SNAPSHOT.zip ./crx-quickstart/install/sampleproject.ui.content.zip
COPY ui.apps/target/sampleproject.ui.apps-0.0.1-SNAPSHOT.zip ./crx-quickstart/install/sampleproject.ui.apps.zip
# Expose AEM author in port 4504
EXPOSE 4504
# Copy license file to the container
COPY license.properties ./crx-quickstart/license.properties
# Start AEM in author mode
ENTRYPOINT ["java", "-jar", "/usr/src/app/cq-quickstart-6.5.0.jar", "-Dsling.run.modes=author", "-p", "4504"]


In aem-publish folder also I have one dockefile which will be used to build aem publish instance docker image.  
aem-publish dockerfile

# Use an official AdoptOpenJDK runtime as a parent image>
FROM adoptopenjdk/openjdk11:alpine-slim
# Set environment variables
ENV CQ_RUNMODE "publish"
ENV CQ_JVM_OPTS "-Xmx2048m"
# Set the working directory
WORKDIR /usr/src/app
# Copy AEM quickstart and license file to the container
COPY cq-quickstart-6.5.0.jar ./
COPY license.properties ./
# Set up volume to share crx-quickstart directory with other containers
VOLUME /usr/src/app/crx-quickstart
# Copy the UI content and apps packages to the container
COPY ui.content/target/sampleproject.ui.content-0.0.1-SNAPSHOT.zip ./crx-quickstart/install/sampleproject.ui.content.zip
COPY ui.apps/target/sampleproject.ui.apps-0.0.1-SNAPSHOT.zip ./crx-quickstart/install/sampleproject.ui.apps.zip
# Copy AEM configuration files to the container
COPY .m2/settings.xml /root/.m2/settings.xml
# Expose AEM publish port
EXPOSE 4503
# Copy license file to the container
COPY license.properties ./crx-quickstart/license.properties
# Start AEM in publish mode
ENTRYPOINT ["java", "-jar", "/usr/src/app/cq-quickstart-6.5.0.jar", "-Dsling.run.modes=publish", "-p", "4503"]


Here I am assuming your dockerfile, aem jar and license file are in same directory.
docker installation done   check version once using docker --version command
docker files done  
Now let's create docker image for author instance open command prompt and execute the below command.
don't use caps letter it will give error if we use caps letter in docker image.

docker build -t sampleimage1 C:\user\code\sampleproject

C:\user\code\sampleproject  -->dockerfile path

sampleimage1 -->image name we are going to create

Now create a Volume : docker volume is the directory outside of container where data will be stored. volumes can be used to share data between containers or to store data that needs to persist between container restarts.

docker volume create sampleproject_volume1




after creating image and volume start the aem author instance using this image

docker run -p 4504:4504 aem-author or we can volume mount the packages we want to share withing container for doing that run the below command.

docker run -d --name sampleimage1_container -p 4504:4504 -v sampleproject_volume1:/usr/src/app/crx-quickstart/install sampleimage1

--name my_aem_author  : container will get created with name my_aem_author
-p 4504:4504 : It maps port 4504 of the host machine to port 4504 of the container. This is used to access the AEM Author instance running inside the container.
-v sampleproject_volume:/usr/src/app/crx-quickstart/install : it will create a new volume with name sampleproject_volume and mounts it to the "/usr/src/app/crx-quickstart/install" directory inside the container
sampleimage : sampleimage is the image for which container will get created.
Now go to browser and hit the http://localhost:4504/aem/start.html it will open login page, login with admin/admin credential

Below snapshot images for docker image and docker volume 


So finally we have a docker image with project content after we push this docker image to docker hub our team member can pull and start aem with the already installed packages. 
Same we can do publish instance as well

Command line for creating docker image use command

C:\Users\username>docker build -t aem-publish C:\pathToFolder\aem-in-docker\aem-publisher

My dockerfile is located at C:\pathToFolder\aem-in-docker\aem-publisher

Image name will be aem-publish

Check if image is created or not using docker images command

aem-publish is created now run the image to start aem publish instance

Once image is created we can run docker run command anywhere

C:\>docker run -p 4503:4503 aem-publish

it will look like below

in docker you images will look like below

Few points to remember while working with docker.

You can only stop containers, but not images.
To stop the container:
Get the container id using docker ps
Stop it using docker container stop <id>
Or in docker desktop go container tab and get the container id
In screenshot yellow highlighted are container id 
                                       

One image can have multiple containers like whenever we stop running image and again start that image it will create a new container for starting the image.

In below screen shot we can see one image i.e. aem-author is having 4 container it is because I stopped aem instance 3 times and 4th one is running here all 4 containers are running for single image that is aem-author

To remove docker image you need to remove container where the image is used so to remove container use command docker rm <container_id> now you can remove the image use command docker rmi <image_name>

docker ps : will return the running container.


To list all containers, both running add –a :
see below



KEEP EDUCATING YOURSELF!!!!

Comments

Popular posts from this blog

Sling API vs JCR API

Creating or finding .M2 folder