Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Dockerfile to reduce amount of config needed in docker-compose #723

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ WORKDIR /app
COPY ui/ ./

RUN rm -rf node_modules; \
yarn install --frozen-lockfile --non-interactive; \
yarn build
yarn install --frozen-lockfile --non-interactive; \
yarn build
Comment on lines -10 to +11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My editor kept formatting this - happy to revert if it is undesired.


# Stage 2: Go Builder
FROM --platform=$TARGETPLATFORM golang:1.22-alpine as go-builder
Expand All @@ -32,35 +32,28 @@ ARG USER="dagu"
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Create user and set permissions
RUN apk update; \
apk add --no-cache sudo tzdata; \
addgroup -g ${USER_GID} ${USER}; \
adduser ${USER} -h /home/${USER} -u ${USER_UID} -G ${USER} -D -s /bin/ash; \
echo ${USER} ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/${USER}; \
chmod 0440 /etc/sudoers.d/${USER}; \
mkdir -p .config/dagu/dags; \
chown -R ${USER}:${USER} /home/${USER};

COPY --from=go-builder /app/bin/dagu /usr/local/bin/
COPY ./entrypoint.sh /entrypoint.sh

# Create user and set permissions
RUN apk update && \
apk add --no-cache su-exec shadow tzdata && \
addgroup -g ${USER_GID} ${USER} && \
adduser ${USER} -h /config -u ${USER_UID} -G ${USER} -D -s /bin/ash && \
chown -R ${USER}:${USER} /config && \
chmod +x /entrypoint.sh;
Comment on lines -37 to +44
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of sudo we rely on su-exec and allowing the user to run scripts in /etc/custom-init.d

Ideally sudo would not be added to docker containers, since it is easy to get root if you need it anyway (through docker exec)


USER ${USER}
WORKDIR /home/${USER}
WORKDIR /config

# Add the hello_world.yaml file
COPY --chown=${USER}:${USER} <<EOF .config/dagu/dags/hello_world.yaml
schedule: "* * * * *"
steps:
- name: hello world
command: sh
script: |
echo "Hello, world!"
EOF
Comment on lines -50 to -58
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could never work as when a user binds over /home/dagu/.config/dagu it will mount over this file at this location.

If there is a desire to have an automatically populated hello-world dag, it would have to happen during runtime on the first boot.


ENV DAGU_HOST=0.0.0.0
ENV DAGU_PORT=8080
ENV DAGU_TZ="Etc/UTC"
ENV PUID=${USER_UID}
ENV PGID=${USER_GID}
ENV DOCKER_GID=-1

EXPOSE 8080

ENTRYPOINT ["/entrypoint.sh"]
CMD ["dagu", "start-all"]
62 changes: 21 additions & 41 deletions docs/source/docker-compose.rst
Original file line number Diff line number Diff line change
@@ -1,48 +1,28 @@
.. _Using Docker Compose:
Using Docker Compose
===================================

To automate DAG executions based on cron expressions, it is necessary to run both the ui server and scheduler process. Here is an example `docker-compose.yml` setup for running Dagu using Docker Compose.
Here is an example `docker-compose.yml` setup for running Dagu using Docker Compose.

.. code-block:: yaml
version: "3.9"
services:
# init container updates permission
init:
image: "ghcr.io/dagu-org/dagu:latest"
user: root
volumes:
- dagu_config:/home/dagu/.config/dagu
- dagu_data:/home/dagu/.local/share
command: chown -R dagu /home/dagu/.config/dagu/ /home/dagu/.local/share
# ui server process
server:
image: "ghcr.io/dagu-org/dagu:latest"
environment:
- DAGU_PORT=8080
- DAGU_TZ=Asia/Tokyo
restart: unless-stopped
ports:
- "8080:8080"
volumes:
- dagu_config:/home/dagu/.config/dagu
- dagu_data:/home/dagu/.local/share
command: dagu server
depends_on:
- init
# scheduler process
scheduler:
image: "ghcr.io/dagu-org/dagu:latest"
environment:
- DAGU_TZ=Asia/Tokyo
restart: unless-stopped
volumes:
- dagu_config:/home/dagu/.config/dagu
- dagu_data:/home/dagu/.local/share
command: dagu scheduler
depends_on:
- init
volumes:
dagu_config: {}
dagu_data: {}
services:
dagu:
image: "ghcr.io/dagu-org/dagu:latest"
container_name: dagu
hostname: dagu
ports:
- "8080:8080"
environment:
- DAGU_PORT=8080 # optional. default is 8080
- DAGU_TZ=Asia/Tokyo
- DAGU_BASE_PATH=/dagu # optional. default is /
- PUID=1000 # optional. default is 1000
- PGID=1000 # optional. default is 1000
- DOCKER_GID=999 # optional. default is -1 and it will be ignored
volumes:
- dagu_config:/config
- /var/run/docker.sock:/var/run/docker.sock # optional. required for docker in docker
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure if this was worth including here. It maybe the comment should mention the socat alternative in the docs?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the great suggestion! I believe this would be a valuable addition to include here. I think mentioning socat would be nice, but I also like the current focus on the docker-compose configuration. So, let's leave it as it is for now.

volumes:
dagu_config: {}
6 changes: 5 additions & 1 deletion docs/source/executors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ See the Docker's API documentation for all available options.
Use Host's Docker Environment
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you are running `dagu` using a container, you need the setup below.
If you are running `dagu` using a container, there are two options to use the host's Docker environment.

1. Mount the Docker socket to the container and pass through the host's docker group id. See the example in :ref:`Using Docker Compose <Using Docker Compose>`

Or

1. Run a `socat` container with the command below.

Expand Down
65 changes: 65 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env /bin/sh

echo "Starting entrypoint.sh"

echo "
PUID=${PUID}
PGID=${PGID}
DOCKER_GID=${DOCKER_GID}
TZ=${DAGU_TZ}
"

# Check if both DOCKER_GID is not -1. This indicates the desire for a docker group
if [ "$DOCKER_GID" != "-1" ]; then
if ! getent group docker >/dev/null; then
echo "Creating docker group with GID ${DOCKER_GID}"
addgroup -g ${DOCKER_GID} docker
usermod -a -G docker dagu
fi

echo "Changing docker group GID to ${DOCKER_GID}"
groupmod -o -g "$DOCKER_GID" docker
fi

groupmod -o -g "$PGID" dagu
usermod -o -u "$PUID" dagu

mkdir -p /config

chown $PUID:$PGID -R /config

# If DAGU_HOME is not set, try to guess if the legacy /home directory is being
# used. If so set the HOME to /home/dagu. Otherwise force the /config directory
# as DAGU_HOME
if [ -z "$DAGU_HOME" ]; then
if [ -d /home/dagu/.config/dagu ]; then
echo "WARNING: Using legacy /home/dagu directory. Please consider moving to /config"
usermod -d /home/dagu dagu
chown $PUID:$PGID -R /home/dagu
else
Comment on lines +35 to +39
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check isn't the best, but it should cover most cases.

We mirror what the "init" part of the original docker-compose stack did by chowning the directory.

# For ease of use set DAGU_HOME to /config so all data is located in a
# single directory
export DAGU_HOME=/config
fi
fi

# Run all scripts in /etc/custom-init.d. It assumes that all scripts are
# executable
if [ -d /etc/custom-init.d ]; then
for f in /etc/custom-init.d/*; do
if [ -x "$f" ]; then
echo "Running $f"
$f
fi
done
fi

# If DOCKER_GID is not -1 set RUN_GID to DOCKER_GID otherwise set to PGID
if [ "$DOCKER_GID" != "-1" ]; then
RUN_GID=$DOCKER_GID
else
RUN_GID=$PGID
fi

# Run the command as the dagu user and optionally the docker group
su-exec $PUID:$RUN_GID "$@"
54 changes: 17 additions & 37 deletions examples/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,39 +1,19 @@
version: "3.9"
services:
# init container updates permission
init:
image: "ghcr.io/dagu-org/dagu:latest"
user: root
volumes:
- dagu_config:/home/dagu/.config/dagu
- dagu_data:/home/dagu/.local/share
command: chown -R dagu /home/dagu/.config/dagu/ /home/dagu/.local/share
# ui server process
server:
image: "ghcr.io/dagu-org/dagu:latest"
environment:
- DAGU_PORT=8080
- DAGU_TZ=Asia/Tokyo
restart: unless-stopped
ports:
- "8080:8080"
volumes:
- dagu_config:/home/dagu/.config/dagu
- dagu_data:/home/dagu/.local/share
depends_on:
- init
# scheduler process
scheduler:
image: "ghcr.io/dagu-org/dagu:latest"
environment:
- DAGU_TZ=Asia/Tokyo
restart: unless-stopped
volumes:
- dagu_config:/home/dagu/.config/dagu
- dagu_data:/home/dagu/.local/share
command: dagu scheduler
depends_on:
- init
dagu:
image: "ghcr.io/dagu-org/dagu:latest"
container_name: dagu
hostname: dagu
ports:
- "8080:8080"
environment:
- DAGU_PORT=8080 # optional. default is 8080
- DAGU_TZ=Asia/Tokyo
- DAGU_BASE_PATH=/dagu # optional. default is /
- PUID=1000 # optional. default is 1000
- PGID=1000 # optional. default is 1000
- DOCKER_GID=999 # optional. default is -1 and it will be ignored
volumes:
- dagu_config:/config
- /var/run/docker.sock:/var/run/docker.sock # optional. required for docker in docker
volumes:
dagu_config: {}
dagu_data: {}
dagu_config: {}
Loading