Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Copy operational files into final docker images #455

Merged
merged 6 commits into from
Jan 13, 2022

Conversation

AetherUnbound
Copy link
Contributor

Description

Presently, we are unable to launch the ingestion server in production using only the published image. The current setup assumes that the operational files will be mounted into the containers when they are spun up, which may not be the case for all services running in production.

This PR adds steps in each Dockerfile to copy all of the necessary operational files into the image so they are available for the container on its own. Images can now be spun up on their own and run successfully without any attached volumes. This ensures that the images that are pulled from our container registry can be launched directly without requiring a copy of the code locally. It also means that our final images are immutable (at least in production), so we won't have any unexpected changes at runtime even if the repository code were to change.

I also added a just build recipe for assistance with this work.

Testing Instructions

  1. Comment out all of the code volume mounts (e.g. ./api:/api) in the docker-compose.yml
  2. Run just build
  3. Run just up
  4. Observe that the stack starts successfully even though the local code is not attached as a volume to the containers

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@AetherUnbound AetherUnbound requested a review from a team as a code owner January 4, 2022 20:58
@AetherUnbound AetherUnbound added 💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix 🟥 priority: critical Must be addressed ASAP labels Jan 4, 2022
Copy link
Member

@zackkrida zackkrida left a comment

Choose a reason for hiding this comment

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

Very nicely commented, thank you!

@dhruvkb
Copy link
Member

dhruvkb commented Jan 5, 2022

So when we attach the same directory as a volume during dev, the copied code is overwritten?

@AetherUnbound
Copy link
Contributor Author

So when we attach the same directory as a volume during dev, the copied code is overwritten?

Yes! The dev version will have a "live" version of the code, while the prod version will have a static set available within the image. More information can be found here, and the Docker docs (say that five times fast) actually describe exactly the scenario that's happening here:

If you bind-mount into a non-empty directory on the container, the directory’s existing contents are obscured by the bind mount. This can be beneficial, such as when you want to test a new version of your application without building a new image.

One other thing I forgot to mention - I prefixed the image names with openverse-. When the images get built, they were previously under the repository name api, analytics, etc. I have dozens of images on my machine, and so api becomes quite ambiguous 😅 this shouldn't affect the target tags or the artifacts that are built during CI/CD (as evidenced by the successful tests).

api/Dockerfile Outdated
Comment on lines 68 to 70
# Copy the operational code into the final image
COPY run.sh manage.py /api/
COPY catalog/ /api/catalog/
Copy link
Member

Choose a reason for hiding this comment

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

Could we write

COPY . /api

like we did for analytics?

Copy link
Member

Choose a reason for hiding this comment

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

^ This would make the COPY statement

  • same in all Dockerfiles
  • identical to the volumes rules from the Docker-Compose file

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 only concern with doing this is that it copies all files in each directory, which includes a number of files which are irrelevant for production use (including all of the tests). I've generally been steered away from that, but it might not be too much extra space. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Would a .dockerignore file help with that? We could reduce a lot of cruft from going into the context.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a great question! I'd be curious if the .dockerignore would also pertain to bind mounts 🤔 I'll try it out - agreed that it'd be better in general!

Copy link
Member

Choose a reason for hiding this comment

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

I also submitted #442 which would make the analytics/ code structure similar to ingestion_server/. If you could maybe rebase this PR on top of that one, the same exclusion strategy would work for both codebases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can do! This is also a good reminder to review that PR 😄

Copy link
Member

@dhruvkb dhruvkb left a comment

Choose a reason for hiding this comment

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

The Docker docs (...Docker docs Docker docs Docker docs Docker docs) were very clear about it, thanks for the reference. LGTM!

@@ -65,7 +65,7 @@ services:

web:
build: ./api/
image: api
image: openverse-api
Copy link
Member

Choose a reason for hiding this comment

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

This is really good, thanks!

Copy link
Member

@krysal krysal left a comment

Choose a reason for hiding this comment

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

Tried it and works well. The prefix for images makes sense too 👍

@AetherUnbound AetherUnbound force-pushed the hotfix/copy-files-into-image branch from 0de6e06 to d726ad7 Compare January 5, 2022 23:18
@AetherUnbound AetherUnbound changed the base branch from main to nl_fixes January 5, 2022 23:18
@AetherUnbound
Copy link
Contributor Author

I've updated this to point at #442 so we can unify the approach. Tested with just up, just api-test, just ing-testlocal and just up (with commented out local code bind mounts) - all looked great!

Copy link
Member

@dhruvkb dhruvkb left a comment

Choose a reason for hiding this comment

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

This is awesome!

Base automatically changed from nl_fixes to main January 13, 2022 07:08
@AetherUnbound AetherUnbound force-pushed the hotfix/copy-files-into-image branch from d726ad7 to fe023ea Compare January 13, 2022 18:05
@AetherUnbound AetherUnbound merged commit f4d8fa1 into main Jan 13, 2022
@AetherUnbound AetherUnbound deleted the hotfix/copy-files-into-image branch January 13, 2022 18:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix 🟥 priority: critical Must be addressed ASAP
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants