DEV Community

David MacEachern
David MacEachern

Posted on

How to fix Github Docker Containers Built with Actions

Following on from having set up the Github Container Registry in my previous post I look at how I can combine Github Actions with the Registry to automate the build step of my application.

tl;dr

  • You can use Github Actions to build containers based on Dockerfiles in your repository.
  • Have a Google for the answer but don't hesitate to run your problematic containers interactively.
  • You can build and interact with your Docker images locally docker build . then docker run -it $IMAGE_ID /bin/bash

Problem

I have a Dockerfile I am building to package up my application to be deployed somewhere, I do so using a Github Action upon pushing changes
to Github.

Github Actions have many example templates to base workflows on if you already have a project you can navigate to: "https://github.com/" + $YOUR_USERNAME + "/" + $REPOSITORY_NAME + "/actions/new

When I create a Dockerfile, I develop locally and commit changes to the repository. Upon a push to Github my Action Workflow is triggered, this is continuous integration (CI) at it's best, but when we need to look inside a container things can get a little tricky.

What follows are some notes about how you might fix your CI container build.

Lessons learnt

When I attempt to build this Dockerfile, which looks like this:

FROM rustlang/rust:nightly-slim AS build
WORKDIR /src/rust-web-template
COPY . .
RUN cd ./collector && cargo build --release && cd ..
FROM ubuntu:18.04
COPY --from=build /src/rust-web-template/target/release/collector /usr/local/bin/collector
CMD ["usr/local/bin/client"]
Enter fullscreen mode Exit fullscreen mode

Here are some issues I ran into:

  • pkg-config is required for locating dependencies such as SSL within the container's linux.
It looks like you're compiling on Linux and also targeting Linux. Currently this
requires the `pkg-config` utility to find OpenSSL but unfortunately `pkg-config`
could not be found. If you have OpenSSL installed you can likely fix this by
installing `pkg-config`.
Enter fullscreen mode Exit fullscreen mode
  • Docker container builds steps that require approval must have a yes flag passed to them apt-get install -y pkg-config.
Do you want to continue? [Y/n] Abort.
The command '/bin/sh -c apt-get update &&     apt-get install pkg-config' returned a non-zero code: 1
Enter fullscreen mode Exit fullscreen mode
  • We need to run apt-get update to ensure our container knows where to get packages we want to install.
E: Package 'pkg-config' has no installation candidate
Enter fullscreen mode Exit fullscreen mode
  • Dependencies such as Openssl need to be installed as well to be available when compiling an application that depends on it.
run pkg_config fail: "`\"pkg-config\" \"--libs\" \"--cflags\" \"openssl\"` did not exit successfully: exit code: 1\n--- stderr\nPackage openssl was not found in the pkg-config search path.\nPerhaps you should add the directory containing `openssl.pc\'\nto the PKG_CONFIG_PATH environment variable\nNo package \'openssl\' found\n"
Enter fullscreen mode Exit fullscreen mode

How can we debug a Docker container without Googling?

So our Dockerfile now looks like this:

FROM rustlang/rust:nightly-slim AS build
WORKDIR /src/rust-web-template
COPY . ./collector
RUN apt-get update -y && \
    apt-get install -y pkg-config \
    libssl-dev
RUN cd ./collector && cargo build --release && cd ..
FROM ubuntu:18.04
COPY --from=build /src/rust-web-template/target/release/collector /usr/local/bin/collector
CMD ["usr/local/bin/client"]
Enter fullscreen mode Exit fullscreen mode

I received the following error, which tells me that a directory path was not correct.

 COPY failed: stat /var/lib/docker/overlay2/640cf4644f3f681a1b78f9507d80e70f3cae1a189a65dfd6a1f26f7313748161/merged/src/rust-web-template/target/release/collector: no such file or directory
Enter fullscreen mode Exit fullscreen mode

To fix this I comment out the code, as we need to build the container and then interact with it.

FROM rustlang/rust:nightly-slim AS build
WORKDIR /src/rust-web-template
COPY . ./collector
RUN apt-get update -y && \
    apt-get install -y pkg-config \
    libssl-dev
RUN cd ./collector && cargo build --release && cd ..
+ #FROM ubuntu:18.04
+ #COPY --from=build /src/rust-web-template/target/release/collector /usr/local/bin/collector
+ #CMD ["usr/local/bin/client"]
Enter fullscreen mode Exit fullscreen mode

Next we build it by running $ docker build -t debugimage ., and then we can run the container interactively to explore it docker run -it --name debugimage:latest /bin/bash

Once inside we can run our bash commands to investigate.

t@d0e323387e63:/src/rust-web-template# ls
collector
root@d0e323387e63:/src/rust-web-template# cd collector/
root@d0e323387e63:/src/rust-web-template/collector# ls
Cargo.lock  Cargo.toml  Dockerfile  src  target
root@d0e323387e63:/src/rust-web-template/collector# cd tar
bash: cd: tar: No such file or directory
root@d0e323387e63:/src/rust-web-template/collector# cd target
root@d0e323387e63:/src/rust-web-template/collector/target# ls
CACHEDIR.TAG  release
root@d0e323387e63:/src/rust-web-template/collector/target# cd release/
root@d0e323387e63:/src/rust-web-template/collector/target/release# pwd
/src/rust-web-template/collector/target/release
root@d0e323387e63:/src/rust-web-template/collector/target/release# ls
build  collector  collector.d  deps  examples  incremental
root@d0e323387e63:/src/rust-web-template/collector/target/release# pwd
/src/rust-web-template/collector/target/release
root@d0e323387e63:/src/rust-web-template/collector/target/release#
Enter fullscreen mode Exit fullscreen mode

Then update the path in the Docker container, and uncomment out lines that were not working.

- COPY --from=build /src/rust-web-template/target/release/collector /usr/local/bin/collector
+ COPY --from=build /src/rust-web-template/collector/target/release/collector /usr/local/bin/collector
Enter fullscreen mode Exit fullscreen mode

And then we can $ exit our interactive container session.

Upon building the image again the path problem shouldn't cause the build to fail!

The working code for this can be found here.

Wrapping up

So that's a couple of common problems building linux containers, hopefully it won't happen again!

Do you have better ways to resolve build problems?

Feel free to share any thoughts or findings of your own down below 🙏🏻.

Top comments (0)