Post

1. Build a container using Docker for cmatrix screensaver - Ubuntu

Lab assignment for creating a container using containerd and docker.

1. Build a container using Docker for cmatrix screensaver - Ubuntu

Prerequisites

  • Docker

Assignment

  • Lets build a container using the github repository created for running a Matrix screensaver.
  • Languages: C

1️⃣ Pull a base docker image ‘alpine’ - Ultrasmall and minimal image based on alpine linux (size: ~8MB)

1
docker pull ubuntu

2️⃣ Verify if the images exists on your host

1
docker images

3️⃣ Create a Dockerfile using the apline images

1
2
3
4
5
cat <<EOF> ./Dockerfile
FROM ubuntu

LABEL org.opencontainers.image.authors="Sakharam Shinde"
EOF

4️⃣ Build a docker image using the Dockerfile (Same as base image with just additional label)

1
docker build -t sakharamshinde/cmatrix .

5️⃣ Verify if the new images is created with the defined tag.

1
docker images

6️⃣ Create a container using the new image and ssh into the container

1
docker run --rm -it sakharamshinde/cmatrix sh

7️⃣ 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

8️⃣ Since git is not installed in the base image, we will install the git tool.

1
apt-get update
1
apt-get install -y 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 

9️⃣ Similarly, install the required packages required to build the cmatrix screensave

  • Prepare compilation, will fail, missing autoconf
1
autoreconf -i
  • Install autoconf
1
apt-get install -y autoconf
  • Prepare compilation, will fail, missing automake
1
autoreconf -i
  • Install automake
1
apt-get install -y 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
apt-get install -y alpine-sdk
  • Prepare configure, will fail, missing dependencies
1
./configure LDFLAGS="-static"
  • Install dependencies and create missing directories
1
apt-get install -y 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 apt-get update

RUN apt-get install -y git 

RUN git clone https://github.com/abishekvashok/cmatrix.git .

RUN cd cmatrix

RUN apt-get install -y autoconf

RUN apt-get install -y automake

RUN autoreconf -i

RUN apt-get install -y alpine-sdk

RUN apt-get install -y 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 apt-get update

RUN apt-get install -y git 

RUN git clone https://github.com/abishekvashok/cmatrix.git .

RUN apt-get install -y autoconf

RUN apt-get install -y automake

RUN autoreconf -i

RUN apt-get install -y alpine-sdk

RUN apt-get install -y 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
  1. Check the size of the new image and inspect the layers
1
docker images
1
docker history sakharamshinde/cmatrix
  1. 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 apt-get update --no-cache && \
    apt-get install -y 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
  1. Check the size of the image and inspect the layers
1
docker images
1
docker history sakharamshinde/cmatrix
  1. 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 apt-get update --no-cache && \
apt-get install -y 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
  1. Check the size of the image and inspect the layers
1
docker images
1
docker history sakharamshinde/cmatrix
  1. 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 apt-get update --no-cache && \
    apt-get install -y 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 apt-get update --no-cache && \
    apt-get install -y 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
  1. Check the user running the container
1
docker run --rm -it sakharamshinde/cmatrixms whoami
  1. 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 apt-get update --no-cache && \
    apt-get install -y 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 apt-get update --no-cache && \
    apt-get install -y 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
  1. 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 apt-get update --no-cache && \
    apt-get install -y 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 apt-get update --no-cache && \
    apt-get install -y 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.