1. Build a container using Docker for cmatrix screensaver - Alpine
Lab assignment for creating a container using containerd and docker.
1. Build a container using Docker for cmatrix screensaver - Alpine
Prerequisites
- Docker
Assignment
- Lets build a container using the github repository created for running a Matrix screensaver.
- Languages: C
- Pull a base docker image ‘alpine’ - Ultrasmall and minimal image based on alpine linux (size: ~8MB)
1
docker pull alpine
- Verify if the images exists on your host
1
docker images
- Create a Dockerfile using the apline images
1
2
3
4
5
cat <<EOF> ./Dockerfile
FROM alpine
LABEL org.opencontainers.image.authors="Sakharam Shinde"
EOF
- Build a docker image using the Dockerfile (Same as base image with just additional label)
1
docker build -t sakharamshinde/cmatrix .
- Verify if the new images is created with the defined tag.
1
docker images
- Create a container using the new image and ssh into the container
1
docker run --rm -it sakharamshinde/cmatrix sh
- Once we are into the shell of the container, execute below steps to get the cmatrix running inside the container.
- Verify the Hostname
1
hostname
- Clone the Repository,
this will fail
1
git clone https://github.com/abishekvashok/cmatrix.git
- Since git is not installed in the base image, we will install the git tool.
1
apk update
1
apk add git
- Clone the repo
1
git clone https://github.com/abishekvashok/cmatrix.git
- Switch to the repo dir and check the contents
1
cd cmatrix
1
ls -l
- Similarly, install the required packages required to build the cmatrix screensave
- Prepare compilation, will fail, missing autoconf
1
autoreconf -i
- Install autoconf
1
apk add autoconf
- Prepare compilation, will fail, missing automake
1
autoreconf -i
- Install automake
1
apk add automake
- Prepare compilation, will succeed, confirm with echo $?
1
autoreconf -i
- Verify it the install was successful
1
echo $?
- Prepare configure, will fail, missing compiler
1
./configure LDFLAGS="-static"
- Install compiler
1
apk add alpine-sdk
- Prepare configure, will fail, missing dependencies
1
./configure LDFLAGS="-static"
- Install dependencies and create missing directories
1
apk add ncurses-dev ncurses-static
1
mkdir -p /usr/lib/kbd/consolefonts /usr/share/consolefonts
- Prepare configure, will succeed
1
./configure LDFLAGS="-static"
- Compile and view result
1
make
- Check the created binary
1
ls -l ./cmatrix
- Run cmatrix
1
./cmatrix
- Build a docker image for the cmatrix app using all the command used above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
cat <<EOF> ./Dockerfile
FROM alpine
LABEL org.opencontainers.image.authors="Sakharam Shinde"
RUN apk update
RUN apk add git
RUN git clone https://github.com/abishekvashok/cmatrix.git .
RUN cd cmatrix
RUN apk add autoconf
RUN apk add automake
RUN autoreconf -i
RUN apk add alpine-sdk
RUN apk add ncurses-dev ncurses-static
RUN mkdir -p /usr/lib/kbd/consolefonts /usr/share/consolefonts
RUN ./configure LDFLAGS="-static"
RUN make
CMD ["./cmatrix"]
EOF
- Build a new image with the updated Dockerfile
1
docker build -t sakharamshinde/cmatrix .
- Run the new image, this will fail
1
docker run --rm -it sakharamshinde/cmatrix
- Specify the working directory for the Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
cat <<EOF> ./Dockerfile
FROM alpine
LABEL org.opencontainers.image.authors="Sakharam Shinde"
WORKDIR /cmatrix
RUN apk update
RUN apk add git
RUN git clone https://github.com/abishekvashok/cmatrix.git .
RUN apk add autoconf
RUN apk add automake
RUN autoreconf -i
RUN apk add alpine-sdk
RUN apk add ncurses-dev ncurses-static
RUN mkdir -p /usr/lib/kbd/consolefonts /usr/share/consolefonts
RUN ./configure LDFLAGS="-static"
RUN make
CMD ["./cmatrix"]
EOF
- Build a new image with the updated Dockerfile
1
docker build -t sakharamshinde/cmatrix .
- Run the new image, this will succeed
1
docker run --rm -it sakharamshinde/cmatrix
- Check the size of the new image and inspect the layers
1
docker images
1
docker history sakharamshinde/cmatrix
- To reduce the number of layers, we will combine common commands into one
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
cat <<EOF> ./Dockerfile
FROM alpine
LABEL org.opencontainers.image.authors="Sakharam Shinde"
WORKDIR /cmatrix
RUN apk update --no-cache && \
apk add git autoconf automake alpine-sdk ncurses-dev ncurses-static
RUN git clone https://github.com/abishekvashok/cmatrix.git .
RUN autoreconf -i
RUN mkdir -p /usr/lib/kbd/consolefonts /usr/share/consolefonts
RUN ./configure LDFLAGS="-static"
RUN make
CMD ["./cmatrix"]
EOF
- Build the image
1
docker build -t sakharamshinde/cmatrix .
- Run the new image
1
docker run --rm -it sakharamshinde/cmatrix
- Check the size of the image and inspect the layers
1
docker images
1
docker history sakharamshinde/cmatrix
- To reduce the size further, we can combine all the RUN commands
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cat <<EOF> ./Dockerfile
FROM alpine
LABEL org.opencontainers.image.authors="Sakharam Shinde"
WORKDIR /cmatrix
RUN apk update --no-cache && \
apk add git autoconf automake alpine-sdk ncurses-dev ncurses-static && \
git clone https://github.com/abishekvashok/cmatrix.git . && \
autoreconf -i && \
mkdir -p /usr/lib/kbd/consolefonts /usr/share/consolefonts && \
./configure LDFLAGS="-static" && \
make
CMD ["./cmatrix"]
EOF
- Build the image
1
docker build -t sakharamshinde/cmatrix .
- Run the new image
1
docker run --rm -it sakharamshinde/cmatrix
- Check the size of the image and inspect the layers
1
docker images
1
docker history sakharamshinde/cmatrix
- Build the image using multi-stage docker build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cat <<EOF> ./Dockerfile
# Build Container Image
FROM alpine AS cmatrixbuilder
WORKDIR /cmatrix
RUN apk update --no-cache && \
apk add git autoconf automake alpine-sdk ncurses-dev ncurses-static && \
git clone https://github.com/abishekvashok/cmatrix.git . && \
autoreconf -i && \
mkdir -p /usr/lib/kbd/consolefonts /usr/share/consolefonts && \
./configure LDFLAGS="-static" && \
make
# cmatrix Container Image
FROM alpine
LABEL org.opencontainers.image.authors="Sakharam Shinde" \
RUN apk update --no-cache && \
apk add ncurses-terminfo-base
COPY --from=cmatrixbuilder /cmatrix/cmatrix /cmatrix
CMD ["./cmatrix"]
EOF
- Build the image
1
docker build -t sakharamshinde/cmatrixms .
- Run the new image
1
docker run --rm -it sakharamshinde/cmatrixms
- Check the user running the container
1
docker run --rm -it sakharamshinde/cmatrixms whoami
- Run the container as a user instead of root
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
cat <<EOF> ./Dockerfile
# Build Container Image
FROM alpine AS cmatrixbuilder
WORKDIR /cmatrix
RUN apk update --no-cache && \
apk add git autoconf automake alpine-sdk ncurses-dev ncurses-static && \
git clone https://github.com/abishekvashok/cmatrix.git . && \
autoreconf -i && \
mkdir -p /usr/lib/kbd/consolefonts /usr/share/consolefonts && \
./configure LDFLAGS="-static" && \
make
# cmatrix Container Image
FROM alpine
LABEL org.opencontainers.image.authors="Sakharam Shinde" \
RUN apk update --no-cache && \
apk add ncurses-terminfo-base && \
adduser -g "Sakharam" -s /usr/sbin/nologin -D -H sakharam
COPY --from=cmatrixbuilder /cmatrix/cmatrix /cmatrix
USER sakharam
CMD ["./cmatrix"]
EOF
- Build the image
1
docker build -t sakharamshinde/cmatrixms .
- Run the new image
1
docker run --rm -it sakharamshinde/cmatrixms
- Run the container with entrpoint and commands
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
cat <<EOF> ./Dockerfile
# Build Container Image
FROM alpine AS cmatrixbuilder
WORKDIR /cmatrix
RUN apk update --no-cache && \
apk add git autoconf automake alpine-sdk ncurses-dev ncurses-static && \
git clone https://github.com/abishekvashok/cmatrix.git . && \
autoreconf -i && \
mkdir -p /usr/lib/kbd/consolefonts /usr/share/consolefonts && \
./configure LDFLAGS="-static" && \
make
# cmatrix Container Image
FROM alpine
LABEL org.opencontainers.image.authors="Sakharam Shinde" \
RUN apk update --no-cache && \
apk add ncurses-terminfo-base && \
adduser -g "Sakharam" -s /usr/sbin/nologin -D -H sakharam
COPY --from=cmatrixbuilder /cmatrix/cmatrix /cmatrix
USER sakharam
ENTRYPOINT ["./cmatrix"]
CMD ["-b"]
EOF
- Build the image
1
docker build -t sakharamshinde/cmatrixms .
- Run the new image
1
docker run --rm -it sakharamshinde/cmatrixms --help
This post is licensed under CC BY 4.0 by the author.