Skip to content

Commit

Permalink
Add docker compose environment (#66)
Browse files Browse the repository at this point in the history
* Add very basic Docker environment

That will do for now

* Add latest changes

* Update Docker environment

- Remove build.md which was included by mistake.
- Improve dev.sh script.
- Update .gitignore to exclude artifacts folder.
- Create .dockerignore file.
- Replace get_version.sh script with inline command.
- Reduce image size by using alpine as base image.

---------

Signed-off-by: Álex Ruiz <[email protected]>
  • Loading branch information
AlexRuiz7 committed Mar 8, 2024
1 parent 8efc6e1 commit e3172df
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 0 deletions.
71 changes: 71 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Frontend development environments

Install [Docker Desktop][docker-desktop] as per its instructions, available for Windows, Mac
and Linux (Ubuntu, Debian & Fedora).
This ensures that the development experience between Linux, Mac and Windows is as
similar as possible.

> IMPORTANT: be methodic during the installation of Docker Desktop, and proceed
> step by step as described in their documentation. Make sure that your system
> meets the system requirements before installing Docker Desktop, and read any
> post-installation note, specially on Linux: [Differences between
> Docker Desktop for Linux and Docker Engine][docker-variant].
## Pre-requisites

1. Assign resources to [Docker Desktop][docker-desktop]. The requirements for the
environments are:

- 8 GB of RAM (minimum)
- 4 cores

The more resources the better ☺

2. Clone the [wazuh-indexer][wi-repo].

3. Set up user permissions

The Docker volumes will be created by the internal Docker user, making them
read-only. To prevent this, a new group named `docker-desktop` and GUID 100999
needs to be created, then added to your user and the source code folder:

```bash
sudo groupadd -g 100999 docker-desktop
sudo useradd -u 100999 -g 100999 -M docker-desktop
sudo chown -R docker-desktop:docker-desktop $WZD_HOME
sudo usermod -aG docker-desktop $USER
```

## Understanding Docker contexts

Before we begin starting Docker containers, we need to understand the
differences between Docker Engine and Docker Desktop, more precisely, that the
use different contexts.

Carefully read these two sections of the Docker documentation:

- [Differences between Docker Desktop for Linux and Docker Engine][docker-variant].
- [Switch between Docker Desktop and Docker Engine][docker-context].

Docker Desktop will change to its context automatically at start, so be sure
that any existing Docker container using the default context is **stopped**
before starting Docker Desktop and any of the environments in this folder.

## Starting up the environments

Use the sh script to up the environment.

Example:

```bash
Usage: ./dev.sh {up|down|stop} [security]
```

Once the `wazuh-indexer` container is up, attach a shell to it and run `./gradlew run`
to start the application.


[docker-desktop]: https://docs.docker.com/get-docker
[docker-variant]: https://docs.docker.com/desktop/install/linux-install/#differences-between-docker-desktop-for-linux-and-docker-engine
[docker-context]: https://docs.docker.com/desktop/install/linux-install/#context
[wi-repo]: https://github.com/wazuh/wazuh-indexer
58 changes: 58 additions & 0 deletions docker/dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

# Attaches the project as a volume to a JDK 17 container
# Requires Docker
# Script usage: bash ./dev.sh

set -e

# ====
# Checks that the script is run from the intended location
# ====
function check_project_root_folder () {
if [[ "$0" != "./dev.sh" && "$0" != "dev.sh" ]]; then
echo "Run the script from its location"
usage
exit 1
fi
# Change working directory to the root of the repository
cd ..
}

# ====
# Displays usage
# ====
function usage() {
echo "Usage: ./dev.sh {up|down|stop}"
}

# ====
# Main function
# ====
function main() {
check_project_root_folder "$@"
compose_file=docker/dev.yml
compose_cmd="docker compose -f $compose_file"
REPO_PATH=$(pwd)
VERSION=$(cat VERSION)
export REPO_PATH
export VERSION

case $1 in
up)
$compose_cmd up -d
;;
down)
$compose_cmd down
;;
stop)
$compose_cmd stop
;;
*)
usage
exit 1
;;
esac
}

main "$@"
26 changes: 26 additions & 0 deletions docker/dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: "3.9"

services:

wazuh-indexer:
image: wazuh-indexer-dev:${VERSION}
container_name: wazuh-indexer-dev-${VERSION}
build:
context: ./..
dockerfile: ${REPO_PATH}/docker/images/wi-dev.Dockerfile
ports:
# OpenSearch REST API
- 9200:9200
# Cross-cluster search
# - 9250:9250
# Node communication and transport
# - 9300:9300
# Performance Analyzer
# - 9600:9600
expose:
- 9200
volumes:
- ${REPO_PATH}:/home/wazuh-indexer/app
entrypoint: ['tail', '-f', '/dev/null']
user: "1000:1000"
working_dir: /home/wazuh-indexer/app
67 changes: 67 additions & 0 deletions docker/images/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
artifacts/

# intellij files
.idea/
*.iml
*.ipr
*.iws
build-idea/
out/

# include shared intellij config
!.idea/inspectionProfiles/Project_Default.xml
!.idea/runConfigurations/Debug_OpenSearch.xml
!.idea/vcs.xml

# These files are generated in the main tree by annotation processors
benchmarks/src/main/generated/*
benchmarks/bin/*
benchmarks/build-eclipse-default/*
server/bin/*
server/build-eclipse-default/*
test/framework/build-eclipse-default/*

# eclipse files
.project
.classpath
.settings
build-eclipse/

# netbeans files
nb-configuration.xml
nbactions.xml

# gradle stuff
.gradle/
build/

# vscode stuff
.vscode/

# testing stuff
**/.local*
.vagrant/
/logs/

# osx stuff
.DS_Store

# default folders in which the create_bwc_index.py expects to find old es versions in
/backwards
/dev-tools/backwards

# needed in case docs build is run...maybe we can configure doc build to generate files under build?
html_docs

# random old stuff that we should look at the necessity of...
/tmp/
eclipse-build

# projects using testfixtures
testfixtures_shared/

# These are generated from .ci/jobs.t
.ci/jobs/

# build files generated
doc-tools/missing-doclet/bin/
16 changes: 16 additions & 0 deletions docker/images/wi-dev.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM gradle:jdk17-alpine AS builder
USER gradle
WORKDIR /home/wazuh-indexer/app
COPY --chown=gradle:gradle . /home/wazuh-indexer/app
RUN gradle clean


FROM eclipse-temurin:17-jdk-alpine
RUN addgroup -g 1000 wazuh-indexer && \
adduser -u 1000 -G wazuh-indexer -D -h /home/wazuh-indexer wazuh-indexer && \
chmod 0775 /home/wazuh-indexer && \
chown -R 1000:0 /home/wazuh-indexer
USER wazuh-indexer
COPY --from=builder --chown=1000:0 /home/wazuh-indexer/app /home/wazuh-indexer/app
WORKDIR /home/wazuh-indexer/app
EXPOSE 9200 9300

0 comments on commit e3172df

Please sign in to comment.