How to run AmazonLinux 2023 on a local Docker host?
systemctl command doesn't work inside of amazonlinux:latest container
systemd - Error running systemctl to start service in Amazon Linux 2 - Stack Overflow
docker - amazonlinux:2 systemctl Failed to get D-Bus connection - Stack Overflow
Suggesting to avoid systemd service units in a docker image.
Instead use cronttab script with @boot directive/selector.
In addition dbus is centrally managed by kernel and not allowed at container level.
If Docker service is up then you probably have dbus active and running.
You can add capabilities to the root user running in the container. Read more here.
As last resort try to disable SELinux in your docker image.
I was running into the same issue trying to run systemctl from within the Amazon Linux 2 docker image
Dockerfile:
FROM amazonlinux:latest
# update and install httpd 2.4.53, php 7.4.28 with php extensions
RUN yum update -y; yum clean all
RUN yum install -y httpd amazon-linux-extras
RUN amazon-linux-extras enable php7.4
RUN yum clean metadata
RUN yum install -y php php-{pear,cli,cgi,common,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip}
# update website files
WORKDIR /var/www/html
COPY phpinfo.php /var/www/html
RUN chown -R apache:apache /var/www
CMD ["/usr/sbin/httpd","-DFOREGROUND"]
EXPOSE 80
EXPOSE 443
$ docker build -t azl1 $ docker run -d -p 8080:80 --name azl1_web azl1
pointing a browser to the IP:8080/phpinfo.php brought up the normal phpinfo page as expected pointing to a successful php 7.4.28 installation.
Amazon Linux v 2.0 does support systemd and comes installed by default:
cat /etc/os-release
NAME="Amazon Linux"
VERSION="2.0 (2017.12)"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2.0"
PRETTY_NAME="Amazon Linux 2.0 (2017.12) LTS Release Candidate"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2.0"
HOME_URL="https://amazonlinux.com/"
rpm -qa | grep -i systemd
systemd-libs-219-42.amzn2.4.x86_64
systemd-219-42.amzn2.4.x86_64
systemd-sysv-219-42.amzn2.4.x86_64`
sadly that only amazon linux v2 support systemd. Amazon linux v1.0 does not https://aws.amazon.com/amazon-linux-2/
Using Amazon Linux 2 I tried to replicate the issue. Obviously, I don't have all the dependencies to run your exact docker-compose.yml, thus I used the docker-compose.yml from here for my verification. The file setups wordpress with mysql .
Steps I took were following (executed as ec2-user in home folder):
1. Install docker
sudo yum update -y
sudo yum install -y docker
sudo systemctl enable docker
sudo systemctl start docker
2. Install docker-compose
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/bin/docker-compose
sudo chmod +x /usr/bin/docker-compose
3. Create docker-compose.yml
mkdir myapp
Create file ./myapp/docker-compose.yml:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
4. Create docker_boot.service
The file is different then yours, as there were few potential issues in your file:
- not using absolute paths
ec2-usermay have no permissions to run docker
Create file ./myapp/docker_boot.service:
[Unit]
Description=docker boot
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/home/ec2-user/myapp
ExecStart=/usr/bin/docker-compose -f /home/ec2-user/myapp/docker-compose.yml up -d --remove-orphans
[Install]
WantedBy=multi-user.target
5. Copy docker_boot.service to systemd
sudo cp -v ./myapp/docker_boot.service /etc/systemd/system
6. Enable and start docker_boot.service
sudo systemctl enable docker_boot.service
sudo systemctl start docker_boot.service
Note: First start may take some time, as it will pull all docker images required. Alternatively start docker-compose manually first to avoid this.
7. Check status of the docker_boot.service
sudo systemctl status docker_boot.service
8. Check if the wordpress is up
curl -L localhost:8000
9. Reboot
Check if the docker_boot.service is running after instance reboot by logging in into the instance and using sudo systemctl status docker_boot.service and/or curl -L localhost:8000.
To have a service launch at launch you would run the following command sudo systemctl enable docker.
For having it then launch your docker compose up -d command you'd need to create a new service for your specific action, and then enable it with the contents similar to the below.
[Unit]
After=docker.service
Description=Docker compose
[Service]
ExecStart=docker-compose up -d
[Install]
WantedBy=multi-user.target
More information for this is available in this post.
Any tips / links to help get docker and AMI v2 (Amazon Linux 2 LTS Candidate AMI 2017.12.0 ). I'm looking to leverage Amazon Linux 2 and docker. Anyone have a pointer to get docker daemon to start automatically. I've walked through a few tutorials and when I type docker version, the daemon isn't running. I've added the ec2-user to the docker group. Rebooted...
ec2-user@ip-10-0-0-90 ~$ systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Drop-In: /etc/systemd/system/docker.service.d └─override.conf Active: failed (Result: start-limit) since Sun 2018-01-28 18:24:10 UTC; 27min ago Docs: https://docs.docker.com Process: 3927 ExecStart=/usr/bin/docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock (code=exited, status=1/FAILURE) Main PID: 3927 (code=exited, status=1/FAILURE)
Jan 28 18:24:10 ip-10-0-0-90.ec2.internal systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE Jan 28 18:24:10 ip-10-0-0-90.ec2.internal systemd[1]: Failed to start Docker Application Container Engine. Jan 28 18:24:10 ip-10-0-0-90.ec2.internal systemd[1]: Unit docker.service entered failed state. Jan 28 18:24:10 ip-10-0-0-90.ec2.internal systemd[1]: docker.service failed. Jan 28 18:24:10 ip-10-0-0-90.ec2.internal systemd[1]: docker.service holdoff time over, scheduling restart. Jan 28 18:24:10 ip-10-0-0-90.ec2.internal systemd[1]: start request repeated too quickly for docker.service Jan 28 18:24:10 ip-10-0-0-90.ec2.internal systemd[1]: Failed to start Docker Application Container Engine. Jan 28 18:24:10 ip-10-0-0-90.ec2.internal systemd[1]: Unit docker.service entered failed state. Jan 28 18:24:10 ip-10-0-0-90.ec2.internal systemd[1]: docker.service failed.