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

#99/chore/update docker compose yaml #102

Merged
merged 15 commits into from May 1, 2024

Conversation

YoshikiKubotani
Copy link
Collaborator

@YoshikiKubotani YoshikiKubotani commented Apr 27, 2024

Issue URL

close #99, close #82

Change overview

  • Add .env files to use dynamic interpolation in docker-compose.yaml (See also Note for reviewers)
  • Add envs.env.sample for users to use environment variables inside containers (See also Note for reviewers)
  • Add top-level name element in docker-compose.yaml to set the project name [link]
  • Update README.md to add optional steps and additional FAQ.

How to test

  1. (Optional) Remove running container
~/environments/cpu$ docker compose down
[+] Running 2/2
 ✔ Container cpu-ascender-core-1  Removed                                                                                                                               0.4s 
 ✔ Network cpu-ascender_default   Removed
  1. Specify an arbitrary name to PROJECT_NAME_ENV in environments/cpu/.env
# If you need to change the default name of the project, edit the following.
PROJECT_NAME_ENV=test
  1. Specify environment variables to environments/envs.env
# Specify the environment variables you want to use inside the container here.
ENV_1=hogehoge
ENV_2=1
  1. Recreate the docker image without using cache and confirm it finished with no errors
~/environments/cpu$ docker compose build --no-cache
[+] Building 258.8s (15/15) FINISHED                                                                                                                          docker:default
 => [core internal] load build definition from Dockerfile                                                                                                               0.1s
 => => transferring dockerfile: 2.21kB                                                                                                                                  0.0s
 => [core internal] load .dockerignore                                                                                                                                  0.1s
 => => transferring context: 132B                                                                                                                                       0.0s
 => [core internal] load metadata for docker.io/library/ubuntu:20.04                                                                                                    2.1s
 => CACHED [core  1/10] FROM docker.io/library/ubuntu:20.04@sha256:21ae67bf44d1d0a53ecdce48742c766e44aea4d16e18a3b88a3888eddaf782b5                                     0.0s
 => [core internal] load build context                                                                                                                                  0.0s
 => => transferring context: 98B                                                                                                                                        0.0s
 => [core  2/10] RUN apt update && apt install --no-install-recommends -y software-properties-common                                                                  170.1s
 => [core  3/10] RUN add-apt-repository ppa:deadsnakes/ppa                                                                                                              6.2s 
 => [core  4/10] RUN apt update && apt install --no-install-recommends -y     git curl make ssh openssh-client     python3.8 python3-pip python-is-python3             60.4s 
 => [core  5/10] RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1     && update-alternatives --set python3 /usr/bin/python3.8     && p  17.1s 
 => [core  6/10] RUN groupadd -g 1000 challengers     && useradd -ms /bin/sh -u 1000 -g 1000 challenger                                                                 0.6s 
 => [core  7/10] WORKDIR /home/challenger/test                                                                                                                          0.1s 
 => [core  8/10] COPY --chown=1000:1000 pyproject.toml poetry.lock poetry.toml .                                                                                        0.1s 
 => [core  9/10] RUN test false = "true" && poetry install || echo "skip to run poetry install."                                                                        0.4s 
 => [core 10/10] RUN test false = "true" && mv /home/challenger/test/.venv ${HOME}/.venv || echo "skip to move .venv."                                                  0.5s 
 => [core] exporting to image                                                                                                                                           0.9s 
 => => exporting layers                                                                                                                                                 0.9s
 => => writing image sha256:c0acb14d5d731a6f4d52567d361f6e9590037e5174d9e7b746f5f5cf7372d7da                                                                            0.0s
 => => naming to docker.io/library/cpu-test-core
  1. Boot up the container and confirm it finished with no errors
~/environments/cpu$ docker compose up -d
[+] Running 2/2
 ✔ Network cpu-test_default   Created                                                                                                                                   0.0s 
 ✔ Container cpu-test-core-1  Started
  1. Verify the project root name in the container matches the name set in step 2.
~/environments/cpu$ docker compose exec core bash

~/test$ pwd
/home/challenger/test
  1. Verify the environment variables set in step 3 are accessible within the running container.
~/test$ printenv ENV_1
hogehoge
~/test$ printenv ENV_2
1

Note for reviewers

Note

Although it is not mentioned in Docker's official documentation, it appears that .env files and files specified in env_files serve different purposes in Docker.

In the docker-compose.yaml file, environment variables specified in the shell can be dynamically loaded and used. Additionally, if a .env file exists in the same directory level as the docker-compose.yaml file, the environment variables defined in that file are also loaded. [link]
This .env file must be named exactly ".env" and placed in the same location as the docker-compose.yaml.

On the other hand, env_files are used to set environment variables inside containers. [link]
Files specified in env_files do not have special constraints regarding their names or locations.

Therefore, for added changes in this pull request, the .env file is used to set the names for directories and containers bound in the docker-compose.yaml, while env_files are used to set environment variables inside the containers.
See also the answer thread to this question on stack overflow.

@YoshikiKubotani YoshikiKubotani marked this pull request as ready for review April 27, 2024 10:53
@YoshikiKubotani YoshikiKubotani changed the base branch from develop to #95/chore/check-new-linter-ruff April 27, 2024 11:22
@YoshikiKubotani YoshikiKubotani changed the base branch from #95/chore/check-new-linter-ruff to develop April 27, 2024 11:23
Copy link
Contributor

@gatheluck gatheluck left a comment

Choose a reason for hiding this comment

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

@YoshikiKubotani
Thank you for your PR! I added comments. Could you take a look?

Copy link
Contributor

@gatheluck gatheluck left a comment

Choose a reason for hiding this comment

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

Thank you for your update! LGTM!!

@YoshikiKubotani YoshikiKubotani merged commit 1b27b5d into develop May 1, 2024
2 checks passed
@gatheluck gatheluck linked an issue May 2, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants