Help us understand the problem. What is going on with this article?

Docker内でC++コードをビルドする

はじめに

ベストプラクティスはわからないけど、DockerでC++のビルドを行えるDockerfileを作成した。ホスト環境をビルド環境で汚さない目的のためにDockerを使用している。Dockerコンテナにアタッチしてコードをビルドするのは一般的ではないかもしれない・・。ホスト側のソースコードをbind mountして、Dockerの環境でビルドを行うような使い方を想定している。

本題

コードをビルドするためのコマンド

# ホスト
docker build --tag env_c .
docker run -it --rm env_c /bin/bash
# Docker
make

構成

  • Dockerfile
  • CMakeLists.txt
  • main.cpp

Dockerfile

FROM ubuntu:18.04

RUN apt update \
  && apt install --yes \
  build-essential \
  cmake \
  vim \
  && rm -rf /var/lib/apt/lists/*

COPY . /workspace/helloworld
RUN mkdir /workspace/helloworld/build \
  && cd /workspace/helloworld/build \
  && cmake .. \
  && make

WORKDIR /workspace/helloworld/build

CMakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(helloworld_cpp)

add_executable(${PROJECT_NAME}
  src/main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC include)

main.cpp

#include <stdio.h>

int main() {
    printf("helloworld\n");
    return 0;
}

ファイル置き場

https://github.com/toppy-luna/sandbox/tree/master/C%2B%2B/helloworld

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away