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

Implemented Docker and Docker Compose support #63

Closed
wants to merge 16 commits into from
Closed
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.github
.gitignore
codecov.yaml
LICENSE
README.md
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing EOF newline! 😱

23 changes: 23 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Docker

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: build
run: docker build . -t modern-cpp

- name: test
run: docker run modern-cpp sh -c "./build/test/GreeterTests"
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM gcc:9.2
leozz37 marked this conversation as resolved.
Show resolved Hide resolved

ENV DEBIAN_FRONTEND noninteractive

# Installing Dependencies
RUN apt-get update && apt-get install -y \
wget \
&& rm -rf /var/lib/apt/lists/*

# Installing CMake
RUN wget "https://github.com/Kitware/CMake/releases/download/v3.18.3/cmake-3.18.3-Linux-x86_64.sh" \
&& chmod a+x cmake-3.18.3-Linux-x86_64.sh \
&& ./cmake-3.18.3-Linux-x86_64.sh --prefix=/usr/local --skip-license \
&& rm cmake-3.18.3-Linux-x86_64.sh

# Copying files
COPY . /app
WORKDIR /app
Comment on lines +16 to +18
Copy link
Owner

Choose a reason for hiding this comment

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

We should probably explicitly specify which files and directories to copy to avoid copying unwanted files or previous build artefacts to the container.

Copy link
Author

Choose a reason for hiding this comment

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

Implemented .dockerignore file.

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for the updates! Tbh I still prefer explicitly copying the necessary directories and files as opposed to blacklisting.

  • The .dockerignore does not contain any build directories or other non-relevant files that users might create before running docker manually
  • Adding the .dockerignore creates yet another root level file to the starter and must be maintained
  • Explicitly listing files ensures that only relevant files get copied and the CI will complain if we forgot something

Alternatively, we could copy only files tracked by git for a clean container state.


# Building
RUN cmake -Hall -Bbuild \
&& cmake --build build
TheLartians marked this conversation as resolved.
Show resolved Hide resolved

# Running
CMD [ "./build/standalone/Greeter" ]
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This template is the result of learnings from many previous projects and should
- Installable target with automatic versioning information and header generation via [PackageProject.cmake](https://github.com/TheLartians/PackageProject.cmake)
- Automatic [documentation](https://thelartians.github.io/ModernCppStarter) and deployment with [Doxygen](https://www.doxygen.nl) and [GitHub Pages](https://pages.github.com)
- Support for [sanitizer tools, and more](#additional-tools)
- Built-in [Docker support](#docker)

## Usage

Expand Down Expand Up @@ -138,6 +139,41 @@ Additional arguments can be passed to the analyzers by setting the `CLANG_TIDY_A

Ccache can be enabled by configuring with `-DUSE_CCACHE=<ON | OFF>`.

## Docker

The project supports [Docker](https://www.docker.com) out-of-the-box, which allows building and running the project inside a container.
The container builds all targets, so you can run any of them passing a command after `sh -c`.

### Docker Workflow

```bash
# build
docker build . -t modern-cpp-starter
# run tests
docker run modern-cpp-starter sh -c "./build/test/GreeterTests"
# run standalone
docker run modern-cpp-starter
```

### Docker Compose Workflow

```bash
# build
docker-compose up --build
# run tests
docker-compose run application sh -c "./build/test/GreeterTests"
# run standalone
docker-compose run application
```

### Removing Docker Support

Docker support is optional, if you don't need it simply remove the following files from the starter:

- `.github/workflows/docker.yml`
- `Dockerfile`
- `docker-compose.yml`

## FAQ

> Can I use this for header-only libraries?
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "3.8"

services:
application:
build: .
image: greeter
container_name: greeter