Explain how to make a minimal image with the alpine image and run the c++ program

  • 2020-11-25 07:24:14
  • OfStack

demand

Work if we want to make mirror, 1 kind is directly pull official images, such as we want to run a c + + program we could directly pull1 gcc, or ubuntu mirror is ok, but there is a problem, we just want to run a c + + program is to run a ubuntu system, which is very resource-intensive, so went to the Internet search found early docker alpine mirror is used to do base image, so alpile image was used to make a mirror

dockerfile


FROM alpine:3.7

MAINTAINER Rethink 
# update Alpine The source of the software is the domestic (Tsinghua University) site, because the pull from the default official source is too slow... 
RUN echo "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main/" > /etc/apk/repositories

RUN apk update \
    && apk upgrade \
    && apk add --no-cache bash \
    bash-doc \
    bash-completion \
    && rm -rf /var/cache/apk/* \
    && /bin/bash
COPY a.out /bin
CMD ["./a.out"]

Question:

a.out is an executable I compiled on the host, but the newly generated image won't boot, which surprises me

Improvement:

/ a.out doesn't work because a.out is compiled on my own ubuntu system, and my mirror image is an alpine image, so it's easy to fix this file

Method 1:

Download the cross-compilation tool on the ubuntu host, use the cross-compilation tool to compile our source code and generate executable programs

Method 2:

We can steal a lazy use of docker pull1 installed apline-ES46en image, above the compilation of our source code, the generated executable program into the apline image can be

Method 3:

We can compile with source code in the alpine image

Methods 3 dockerfile


FROM alpine:3.7

MAINTAINER Rethink 
# update Alpine The source of the software is the domestic (Tsinghua University) site, because the pull from the default official source is too slow... 
RUN echo "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main/" > /etc/apk/repositories

RUN apk update \
    && apk upgrade \
    && apk add --no-cache bash \
    bash-doc \
    bash-completion \
    && rm -rf /var/cache/apk/* \
    && /bin/bash

RUN apk add gcc

RUN mkdir /chun

COPY hello.c /chun

WORKDIR /chun/

RUN gcc hello.c

CMD ["./a.out"]


Related articles: