Search code examples
dockerdockerfilesystemdaptubuntu-server

How to handle a Docker RUN apt install command that wants to install systemd service


What is the proper way to go about using an Aptitude package that wants to install a systemd service on Linux within a Docker container?

Here is the output:

krzysztof@pop-devx:~/docker2_edgeiq$ docker compose build
[+] Building 3.8s (21/22)                                                                                                                                             
 => [internal] load build definition from Dockerfile                                                                                                             0.0s
 => => transferring dockerfile: 604B                                                                                                                             0.0s
 => [internal] load .dockerignore                                                                                                                                0.0s
 => => transferring context: 2B                                                                                                                                  0.0s
 => resolve image config for docker.io/docker/dockerfile:1                                                                                                       0.2s
 => CACHED docker-image://docker.io/docker/dockerfile:1@sha256:39b85bbfa7536a5feceb7372a0817649ecb2724562a38360f4d6a7782a409b14                                  0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                                                                                 0.2s
 => [internal] load build context                                                                                                                                0.0s
 => => transferring context: 186B                                                                                                                                0.0s
 => [ 1/16] FROM docker.io/library/ubuntu:latest@sha256:9a0bdde4188b896a372804be2384015e90e3f84906b750c1a53539b585fbbe7f                                         0.0s
 => CACHED [ 2/16] WORKDIR /edgeiq                                                                                                                               0.0s
 => CACHED [ 3/16] RUN apt-get update                                                                                                                            0.0s
 => CACHED [ 4/16] RUN apt-get install -y sudo                                                                                                                   0.0s
 => CACHED [ 5/16] RUN sudo apt-get upgrade -y                                                                                                                   0.0s
 => CACHED [ 6/16] RUN sudo apt-get install -y ca-certificates                                                                                                   0.0s
 => CACHED [ 7/16] RUN sudo apt-get install -y zip                                                                                                               0.0s
 => CACHED [ 8/16] RUN sudo su && echo "deb [trusted=yes] https://apt.fury.io/edgeiq/ /" > /etc/apt/sources.list.d/edgeiq.list                                   0.0s
 => CACHED [ 9/16] RUN sudo apt-get update                                                                                                                       0.0s
 => CACHED [10/16] RUN sudo apt-get install edgeiq-edgectl                                                                                                       0.0s
 => CACHED [11/16] COPY edgeiq_bootstrap.json .                                                                                                                  0.0s
 => CACHED [12/16] COPY edgeiq_identifier.json .                                                                                                                 0.0s
 => CACHED [13/16] RUN pwd                                                                                                                                       0.0s
 => CACHED [14/16] RUN ls -lash                                                                                                                                  0.0s
 => ERROR [15/16] RUN sudo edgectl install -b ./edgeiq_bootstrap.json                                                                                            3.1s
------
 > [15/16] RUN sudo edgectl install -b ./edgeiq_bootstrap.json:
#0 0.272 Installing edge to '/opt/edge/'...
#0 1.051 Downloading edge from https://api.edgeiq.io/api/v1/platform/releases/latest/edge-linux-amd64-latest...
#0 3.058 Issue installing Edge: Issue enabling edge.service: sh: 1: systemctl: not found
#0 3.058 
#0 3.058 exit status 127
#0 3.058 
------
failed to solve: process "/bin/sh -c sudo edgectl install -b ./edgeiq_bootstrap.json" did not complete successfully: exit code: 1
krzysztof@pop-devx:~/docker2_edgeiq$

The Dockerfile is the following:

# syntax=docker/dockerfile:1
FROM ubuntu:latest
WORKDIR /edgeiq
RUN apt-get update
RUN apt-get install -y sudo
RUN sudo apt-get upgrade -y
#RUN sudo apt-get install -y systemd
RUN sudo apt-get install -y ca-certificates
RUN sudo apt-get install -y zip
RUN sudo su && echo "deb [trusted=yes] https://apt.fury.io/edgeiq/ /" > /etc/apt/sources.list.d/edgeiq.list
RUN sudo apt-get update
RUN sudo apt-get install edgeiq-edgectl
COPY edgeiq_bootstrap.json .
COPY edgeiq_identifier.json .
RUN pwd
RUN ls -lash
RUN sudo edgectl install -b ./edgeiq_bootstrap.json
COPY . .

Someone must have though of this because there is a lot of packages in apt that install as a service. What is the best way to do this?


Solution

  • You can tell edgectl not to start the service by providing the --no-start option:

    RUN edgectl install -b ./edgeiq_bootstrap.json --no-start
    

    You will of course need to figure out how to start the service without using systemd. It looks like a systemd unit file is available in /opt/edge/init/systemd/edge.service; you should be able to inspect that and extract the appropriate command from the ExecStart directive.


    Unrelated to your question, but you can drop all those sudo calls in your Dockerfile. The fact that you're able to install sudo in the first place means you're already running as root. You could simplify things down to:

    # syntax=docker/dockerfile:1
    FROM ubuntu:latest
    WORKDIR /edgeiq
    
    RUN apt-get update
    RUN apt-get upgrade -y
    RUN apt-get install -y ca-certificates sudo zip
    RUN echo "deb [trusted=yes] https://apt.fury.io/edgeiq/ /" > /etc/apt/sources.list.d/edgeiq.list
    RUN apt-get update && apt-get -y install edgeiq-edgectl
    COPY edgeiq_bootstrap.json .
    COPY edgeiq_identifier.json .
    RUN pwd; ls -lash
    RUN edgectl install -b ./edgeiq_bootstrap.json --no-start