Skip to content

Commit

Permalink
Make "up" go Brrrrrrrr
Browse files Browse the repository at this point in the history
data seed command synchronizes storage directory

Remove storage volume (reuse olympia volume)

Remove data_olympia (use anonymous volume)

Remove sources from development DOCKER_TARGET

Invert docker-compose.ci to docker-compose.dev (update defaults in setup script to support prod mode in ci)
  • Loading branch information
KevinMind committed Oct 30, 2024
1 parent 3a6416d commit ce48139
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 95 deletions.
5 changes: 1 addition & 4 deletions .github/actions/build-docker/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ inputs:
version:
required: true
description: The image version to tag with
target:
required: true
description: The stage to target in the build
push:
required: false
description: Push the image?
Expand Down Expand Up @@ -57,7 +54,7 @@ runs:
- name: Create .env and version.json files
shell: bash
run: |
echo "DOCKER_TARGET=${{ inputs.target }}" >> $GITHUB_ENV
echo "DOCKER_TARGET=production" >> $GITHUB_ENV
echo "DOCKER_VERSION=${{ steps.meta.outputs.version }}" >> $GITHUB_ENV
echo "DOCKER_COMMIT=${{ steps.context.outputs.git_sha }}" >> $GITHUB_ENV
echo "DOCKER_BUILD=${{ steps.context.outputs.git_build_url }}" >> $GITHUB_ENV
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/run-docker/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ inputs:
compose_file:
description: 'The docker-compose file to use'
required: false
default: 'docker-compose.yml:docker-compose.ci.yml'
default: 'docker-compose.yml'
logs:
description: 'Show logs'
required: false
Expand Down
7 changes: 0 additions & 7 deletions .github/workflows/_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,28 @@ jobs:
-
name: Needs Locale Compilation
services: ''
compose_file: docker-compose.yml:docker-compose.ci.yml
run: |
make compile_locales
make test_needs_locales_compilation
-
name: Static Assets
services: ''
compose_file: docker-compose.yml:docker-compose.ci.yml
run: make test_static_assets
-
name: Internal Routes
services: ''
compose_file: docker-compose.yml:docker-compose.ci.yml
run: make test_internal_routes_allowed
-
name: Elastic Search
services: ''
compose_file: docker-compose.yml:docker-compose.ci.yml
run: make test_es_tests
-
name: Codestyle
services: web
compose_file: docker-compose.yml:docker-compose.ci.yml
run: make lint-codestyle
-
name: Manage Check
services: web nginx
compose_file: docker-compose.yml:docker-compose.ci.yml
run: make check
steps:
- uses: actions/checkout@v4
Expand All @@ -78,5 +72,4 @@ jobs:
version: ${{ inputs.version }}
digest: ${{ inputs.digest }}
services: ${{ matrix.services }}
compose_file: ${{ matrix.compose_file }}
run: ${{ matrix.run }}
2 changes: 1 addition & 1 deletion .github/workflows/_test_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ jobs:
services: ''
digest: ${{ inputs.digest }}
version: ${{ inputs.version }}
compose_file: docker-compose.yml
compose_file: docker-compose.yml:docker-compose.dev.yml
run: |
split="--splits ${{ needs.test_config.outputs.splits }}"
group="--group ${{ matrix.group }}"
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ jobs:
registry: ${{ steps.docker_hub.outputs.registry }}
image: ${{ steps.docker_hub.outputs.image }}
version: ci-${{ needs.context.outputs.docker_version }}
target: development
push: true

test_make_docker_configuration:
Expand Down Expand Up @@ -179,7 +178,7 @@ jobs:
with:
digest: ${{ needs.build.outputs.digest }}
version: ${{ needs.build.outputs.version }}
compose_file: docker-compose.yml
compose_file: docker-compose.yml:docker-compose.dev.yml
run: |
make docs
Expand Down Expand Up @@ -230,7 +229,7 @@ jobs:
with:
digest: ${{ needs.build.outputs.digest }}
version: ${{ needs.build.outputs.version }}
compose_file: docker-compose.yml
compose_file: docker-compose.yml:docker-compose.dev.yml
run: make extract_locales

- name: Push Locales
Expand Down Expand Up @@ -297,7 +296,6 @@ jobs:
registry: ${{ steps.docker_hub.outputs.registry }}
image: ${{ steps.docker_hub.outputs.image }}
version: ${{ needs.context.outputs.docker_version }}
target: production
push: true

push_gar:
Expand Down Expand Up @@ -329,5 +327,4 @@ jobs:
registry: ${{ steps.docker_gar.outputs.registry }}
image: ${{ steps.docker_gar.outputs.image }}
version: ${{ needs.context.outputs.docker_version }}
target: production
push: true
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ tmp/*
# End of .gitignore. Please keep this in sync with the top section of .dockerignore

# do not ignore the following files
!docker-compose.development.yml
!docker-compose.dev.yml
!docker-compose.private.yml
!private/README.md
37 changes: 14 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ EOF
ARG DOCKER_TARGET
ENV DOCKER_TARGET=${DOCKER_TARGET}

# Add our custom mime types (required for for ts/json/md files)
COPY docker/etc/mime.types /etc/mime.types


# Define production dependencies as a single layer
# let's the rest of the stages inherit prod dependencies
# and makes copying the /deps dir to the final layer easy.
Expand All @@ -115,7 +119,10 @@ ${PIP_COMMAND} install --progress-bar=off --no-deps --exists-action=w -r require
npm ci ${NPM_ARGS} --include=prod
EOF

FROM base AS pip_development
# In the local development image, we stop with dependencies
# Everything else is about compiling production assets which is not needed
# in the typical development workflow.
FROM base AS development

RUN \
# Files required to install pip dependencies
Expand All @@ -127,11 +134,7 @@ RUN \
# Mounts for caching dependencies
--mount=type=cache,target=${PIP_CACHE_DIR},uid=${OLYMPIA_UID},gid=${OLYMPIA_UID} \
--mount=type=cache,target=${NPM_CACHE_DIR},uid=${OLYMPIA_UID},gid=${OLYMPIA_UID} \
<<EOF
${PIP_COMMAND} install --progress-bar=off --no-deps --exists-action=w -r requirements/prod.txt
${PIP_COMMAND} install --progress-bar=off --no-deps --exists-action=w -r requirements/dev.txt
npm install ${NPM_ARGS} --no-save
EOF
make update_deps

FROM base AS locales
ARG LOCALE_DIR=${HOME}/locale
Expand Down Expand Up @@ -168,35 +171,23 @@ echo "from olympia.lib.settings_base import *" > settings_local.py
DJANGO_SETTINGS_MODULE="settings_local" make -f Makefile-docker update_assets
EOF

FROM base AS sources
FROM base AS production

ARG DOCKER_BUILD DOCKER_COMMIT DOCKER_VERSION

ENV DOCKER_BUILD=${DOCKER_BUILD}
ENV DOCKER_COMMIT=${DOCKER_COMMIT}
ENV DOCKER_VERSION=${DOCKER_VERSION}

# Add our custom mime types (required for for ts/json/md files)
COPY docker/etc/mime.types /etc/mime.types
# Copy the rest of the source files from the host
COPY --chown=olympia:olympia . ${HOME}
# Copy compiled locales from builder
COPY --from=locales --chown=olympia:olympia ${HOME}/locale ${HOME}/locale
# Copy assets from assets
COPY --from=assets --chown=olympia:olympia ${HOME}/site-static ${HOME}/site-static
COPY --from=assets --chown=olympia:olympia ${HOME}/static-build ${HOME}/static-build

# Set shell back to sh until we can prove we can use bash at runtime
SHELL ["/bin/sh", "-c"]

FROM sources AS development

# Copy dependencies from `pip_development`
COPY --from=pip_development --chown=olympia:olympia /deps /deps

FROM sources AS production

# Copy compiled locales from builder
COPY --from=locales --chown=olympia:olympia ${HOME}/locale ${HOME}/locale
# Copy dependencies from `pip_production`
COPY --from=pip_production --chown=olympia:olympia /deps /deps


# Set shell back to sh until we can prove we can use bash at runtime
SHELL ["/bin/sh", "-c"]
5 changes: 5 additions & 0 deletions Makefile-docker
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ update_assets:
# Collect static files: This MUST be run last or files will be missing
$(PYTHON_COMMAND) manage.py collectstatic --noinput

.PHONY: update_deps
update_deps:
$(PIP_COMMAND) install --progress-bar=off --no-deps --exists-action=w -r requirements/prod.txt
$(PIP_COMMAND) install --progress-bar=off --no-deps --exists-action=w -r requirements/dev.txt
npm install $(NPM_ARGS) --no-save

# TOOD: remove this after we migrate addons-frontned to not depend on it.
.PHONY: setup-ui-tests
Expand Down
13 changes: 0 additions & 13 deletions docker-compose.ci.yml

This file was deleted.

17 changes: 17 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
worker:
environment:
- HOST_UID
volumes:
- ./:/data/olympia
# Don't mount generated files. They only exist in the container
# and would otherwiser be deleted by mounbting data_olympia
- /data/olympia/static-build
- /data/olympia/site-static
- ./package.json:/deps/package.json
- ./package-lock.json:/deps/package-lock.json

web:
extends:
service: worker

27 changes: 3 additions & 24 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ x-env-mapping: &env
- HISTIGNORE=ls:exit:"cd .."
- HISTCONTROL=erasedups
- CIRCLECI
- HOST_UID
- HOST_UID=9500
- DEBUG
- DATA_BACKUP_SKIP

Expand Down Expand Up @@ -51,14 +51,6 @@ services:
"--",
"celery -A olympia.amo.celery:app worker -E -c 2 --loglevel=INFO",
]
volumes:
- data_olympia:/data/olympia
# Don't mount generated files. They only exist in the container
# and would otherwiser be deleted by mounbting data_olympia
- /data/olympia/static-build
- /data/olympia/site-static
- ./package.json:/deps/package.json
- ./package-lock.json:/deps/package-lock.json
extra_hosts:
- "olympia.test:127.0.0.1"
restart: on-failure:5
Expand Down Expand Up @@ -141,13 +133,9 @@ services:
- "discovery.type=single-node"
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
mem_limit: 2g
volumes:
- data_elasticsearch:/usr/share/elasticsearch/data

redis:
image: redis:6.2
volumes:
- data_redis:/data

rabbitmq:
image: rabbitmq:3.12
Expand All @@ -158,8 +146,6 @@ services:
- RABBITMQ_DEFAULT_USER=olympia
- RABBITMQ_DEFAULT_PASS=olympia
- RABBITMQ_DEFAULT_VHOST=olympia
volumes:
- data_rabbitmq:/var/lib/rabbitmq

autograph:
image: mozilla/autograph:3.3.2
Expand Down Expand Up @@ -189,17 +175,10 @@ networks:
default:

volumes:
data_redis:
data_elasticsearch:
data_mysqld:
# Keep this value in sync with Makefile-os
# External volumes must be manually created/destroyed
name: addons-server_data_mysqld
external: true
data_rabbitmq:
data_olympia:
driver: local
driver_opts:
type: none
o: bind
device: ${PWD}


11 changes: 11 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ if [[ -n "${HOST_UID:-}" ]]; then
echo "${OLYMPIA_USER} UID: ${OLYMPIA_UID} -> ${HOST_UID}"
fi

# TODO: this doesn't work but we have to check if the image we built is production
# but we are for some reason trying to run it using development dependencies.
# this is pretty typical for CI... we could consider just building ci images though as a simpler solution. It's just nice to test the image you already pushed cause muh CI/CD...
if [[ "${DOCKER_VERSION:-}" != 'local' && "${DOCKER_TARGET:-}" == 'production' ]]; then
echo "Running remote image. Re-installing with development dependencies..."
rm -rf /deps
mkdir -p /deps
chown -R ${get_olympia_uid}:${get_olympia_gid} /deps
su -s /bin/bash $OLYMPIA_USER -c "make update_deps"
fi

cat <<EOF | su -s /bin/bash $OLYMPIA_USER
echo "Running command as ${OLYMPIA_USER} $(get_olympia_uid):$(get_olympia_gid)"
set -xue
Expand Down
11 changes: 5 additions & 6 deletions docs/topics/development/building_and_running_services.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The Dockerfile for the **addons-server** project uses a multi-stage build to opt

4. **Environment Variables for OLYMPIA_USER**:
- **Development Setup**: The `HOST_UID` environment variable is set to the host user ID, ensuring that the container runs with the correct permissions.
- **CI Setup**: In CI environments, such as defined in `docker-compose.ci.yml`, the user ID is reset to the default 9500, and the Olympia mount is removed. This makes the container a closed system, mimicking production behavior closely.
- **CI Setup**: In CI environments, the user ID is reset to the default 9500, and the Olympia mount is removed. This makes the container a closed system, mimicking production behavior closely.

### Best Practices for the Dockerfile

Expand Down Expand Up @@ -146,7 +146,7 @@ We use docker compose under the hood to orchestrate container both locally and i
The `docker-compose.yml` file defines the services, volumes, and networks required for the project.

Our docker compose project is split into a root [docker-compose.yml](../../../docker-compose.yml) file and additional files for specific environments,
such as [docker-compose.ci.yml](../../../docker-compose.ci.yml) for CI environments.
such as [docker-compose.dev.yml](../../../docker-compose.dev.yml) for local environments.

### Healthchecks

Expand All @@ -155,15 +155,14 @@ The health checks ensure the django wsgi server and celery worker node are runni

### Environment specific compose files

- **Local Development**: The `docker-compose.yml` file is used for local development. It defines services like `web`, `db`, `redis`, and `elasticsearch`.
- **CI Environment**: The `docker-compose.ci.yml` file is used for CI environments. It overrides the HOST_UID as well as removing volumes to make the container more production like.
- **Private**: This file includes the customs service that is not open source and should therefore not be included by default.
- **Base**: The `docker-compose.yml` file is used for local development. It defines services like `web`, `db`, `redis`, and `elasticsearch`.
- **Dev Environment**: The `docker-compose.dev.yml` file is used for local development. It overrides the HOST_UID as well as removing volumes to make the container more production like.
- **Override**: This file allows modifying the default configuration without changing the main `docker-compose.yml` file. This file is larglely obsolete and should not be used.

To mount with a specific set of docker compose files you can add the COMPOSE_FILE argument to make up. This will persist your setting to .env.

```sh
make up COMPOSE_FILE=docker-compose.yml:docker-compose.ci.yml
make up COMPOSE_FILE=docker-compose.yml:docker-compose.dev.yml
```

Files should be separated with a colon.
Expand Down
Loading

0 comments on commit ce48139

Please sign in to comment.