How to fix Github Docker Containers Built with Actions
Tags: docker, actions, github
Published: 2020-11-12 13:11:39

---
title: How to fix Github Docker Containers Built with Actions
published: true
tags: docker, actions, github
---

Following on from having set up the Github Container Registry [in my previous post](https://dev.to/davidmaceachern/using-github-container-registry-15m0) 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)](https://en.wikipedia.org/wiki/Continuous_integration) 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](https://github.com/davidmaceachern/rust-web-template/blob/9a9cce4215c5b875563bd2e14c6f73ad76829e5d/collector/Dockerfile), which looks like this:

```Dockerfile
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"]
```

Here are some issues I ran into:

- `pkg-config` is required for locating dependencies such as SSL within the container's linux.

```Bash
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`.
```

- Docker container builds steps that require approval must have a yes flag passed to them `apt-get install -y pkg-config`.

```bash
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
```

- We need to run `apt-get update` to ensure our container knows where to get packages we want to install.

```bash
E: Package 'pkg-config' has no installation candidate
```

- Dependencies such as Openssl need to be installed as well to be available when compiling an application that depends on it.

```bash
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"
```

## How can we debug a Docker container without Googling?

So our Dockerfile now looks like this:

```Dockerfile
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"]
```

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

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

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

```Diff
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"]
```

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.

```Bash
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#
```

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

```Diff
- 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
```

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](https://github.com/davidmaceachern/rust-web-template/blob/3b6d6566ebd0a5b1e445d3736ef6d7198360dabf/collector/Dockerfile).

## 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 🙏🏻.