Prerequisites
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)
2️⃣ Verify if the images exists on your host
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.
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.
- 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
| git clone https://github.com/abishekvashok/cmatrix.git
|
- Switch to the repo dir and check the contents
9️⃣ Similarly, install the required packages required to build the cmatrix screensave
- Prepare compilation, will fail, missing autoconf
1
| apt-get install -y autoconf
|
- Prepare compilation, will fail, missing automake
1
| apt-get install -y automake
|
- Prepare compilation, will succeed, confirm with echo $?
- Verify it the install was successful
- Prepare configure, will fail, missing compiler
1
| ./configure LDFLAGS="-static"
|
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"
|
🔟 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
|
- Check the size of the new image and inspect the layers
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 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
|
1
| docker build -t sakharamshinde/cmatrix .
|
1
| docker run --rm -it sakharamshinde/cmatrix
|
- Check the size of the image and inspect the layers
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 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
|
1
| docker build -t sakharamshinde/cmatrix .
|
1
| docker run --rm -it sakharamshinde/cmatrix
|
- Check the size of the image and inspect the layers
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 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
|
1
| docker build -t sakharamshinde/cmatrixms .
|
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 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
|
1
| docker build -t sakharamshinde/cmatrixms .
|
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 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
|
1
| docker build -t sakharamshinde/cmatrixms .
|
1
| docker run --rm -it sakharamshinde/cmatrixms --help
|