Skip to content

Using systemd in a docker container

Using systemd in a docker container

The Amazon Linux 2 AMI on AWS comes with the systemd service preinstalled but the official docker image on Docker Hub does not.  For development reasons, this is quite annoying because I often create shell scripts that invoke systemctl or apachectl to start and stop services and need to test them quickly and easily.  Using docker to test such scripts is by far the fastest way to quickly iterate through the trial-and-error process that we all love and enjoy as software engineers.  So here are the steps to create and run a docker container that has systemd installed.

Step 1: Create docker container

Create the following Dockerfile to build an Amazon Linux container that includes the systemd components.

FROM amazonlinux
RUN yum install systemd -y
CMD [“/sbin/init”]

Build the docker container using the “docker build” command.

docker build -t mybuild .

After building your new container, you should see the image listed if you run “docker images“.

REPOSITORY TAG IMAGE ID CREATED SIZE
mybuild latest 15048f5febb8 14 seconds ago 627MB

Step 2: Run docker container

Use the following command to run your new container in privileged mode.

docker run –name amazonlinux2 -dit –privileged mybuild /bin/bash -c “/sbin/init”

And the following command to connect to the running docker container.

docker exec -it amazonlinux2 /bin/bash

 

Published inDocker

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *