Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start Nginx server within alpine:latest image using rc-service command

I am trying to create my own Nginx image, using apline:latest image, after fixing a lot of errors, thanks to the internet, I managed to do it and everything works fine, but the problem is when I run the following command :

rc-service nginx status
 * status: stopped

and when I try to start the service this is what it gives me as shown below :

rc-service nginx start
 * WARNING: nginx is already starting

even though the service is stopped the output of the second command tells it is already started?!

so I opened the localhost of my docker-machine to verify whether the service is on or off, and the nginx html page appears successfully.

I tried to run rc-service nginx reload and this is the result:

rc-service nginx reload
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/blkio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpu/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuacct/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuset/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/devices/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/freezer/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/hugetlb/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/memory/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_cls/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_prio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/perf_event/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/pids/tasks: Read-only file system
 * nginx: cannot `reload' as it has not been started

here is the output of nginx -t :

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

and here is the output of less /var/log/nginx/error.log as shown below there is no error :

less: can't open '/var/log/nginx/error.log': No such file or directory

this is my dockerfile :

From alpine:latest

COPY nginx.conf ./tmp
COPY index.html ./tmp
COPY run.bash ./tmp
COPY run2.bash ./tmp

RUN apk update && \
    apk add nginx && \
    adduser -D -g 'www' www && \
    mkdir /www && \
    chown -R www:www /var/lib/nginx && \
    chown -R www:www /www && \
    mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig && \
    apk add openrc --no-cache && \
    sh tmp/run.bash

cmd sh tmp/run2.bash

run.bash :

mv tmp/nginx.conf /etc/nginx/nginx.conf
mv tmp/index.html  /www/index.html

run2.bash :

mkdir /run/openrc
touch /run/openrc/softlevel
mkdir -p /run/nginx
nginx
sh

and this is the guide that I followed :

https://wiki.alpinelinux.org/wiki/Nginx

I want to know why rc-service nginx reload doesn't work even that my nginx service is running perfectly on my docker machine, and also why rc-service nginx status tells that the nginx service is stopped even that it is not ?

and thanks in advance.

By the way when I run this command nginx -s reload, it works without any errors.

like image 321
Holy semicolon Avatar asked Dec 22 '22 16:12

Holy semicolon


1 Answers

After debugging and lots of trial and error, I found a perfect solution at least for me, David Maze answer is very helpful, but for testing purpose I need my shell to be accessible, because when u run nginx -g "daemon off;" you can't access your container shell unless you stop the command process.

whatever when I start my container using the following command :

docker run -it -p 80:80 -p 443:443 alpine:latest

we access the container shell, and we need to download nginx server , and openrc to be able to use rc-service command line.

/ # apk update
/ # apk add nginx openrc

#ANSWER 1

now we'll test If there is an error in nginx server by using the following command :

/ # nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/run/nginx/nginx.pid" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

as you can see from the output that we get, It tells you that a missing file or directory shall be created, so let's create that directory :

/ # ls -la run/
total 8
drwxr-xr-x    2 root     root          4096 Dec 16 10:31 .
drwxr-xr-x    1 root     root          4096 Jan 16 08:12 ..

/ # mkdir /run/nginx/

and we give that directory that we created some permissions :

/ # chown -R nginx:nginx /run/nginx/
/ # chmod 775 /run/nginx/
/ # ls -la /run/
total 12
drwxr-xr-x    1 root     root          4096 Jan 16 08:15 .
drwxr-xr-x    1 root     root          4096 Jan 16 08:12 ..
drwxrwxr-x    2 nginx    nginx         4096 Jan 16 08:15 nginx

now we are good with nginx :

/ # nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Let's test our nginx service is it started or not with rc-service command :

/ # rc-service nginx status
 * You are attempting to run an openrc service on a
 * system which openrc did not boot.
 * You may be inside a chroot or you may have used
 * another initialization system to boot this system.
 * In this situation, you will get unpredictable results!
 * If you really want to do this, issue the following command:
 * touch /run/openrc/softlevel

So from the above output we see that we have two problems, openrc did not boot, and there is a missing file softlevel :

/ # ls -la /run/
total 12
drwxr-xr-x    1 root     root          4096 Jan 16 08:15 .
drwxr-xr-x    1 root     root          4096 Jan 16 08:12 ..
drwxrwxr-x    2 nginx    nginx         4096 Jan 16 08:22 nginx

Let's start by booting our system with openrc simply by typing it itself :

/ # openrc
 * Caching service dependencies ...
Service `hwdrivers' needs non existent service `dev'

/ # ls -la /run/
total 16
drwxr-xr-x    1 root     root          4096 Jan 16 08:29 .
drwxr-xr-x    1 root     root          4096 Jan 16 08:12 ..
drwxrwxr-x    2 nginx    nginx         4096 Jan 16 08:22 nginx
drwxr-xr-x   14 root     root          4096 Jan 16 08:29 openrc

/ # ls -la /run/openrc/
total 64
drwxr-xr-x   14 root     root          4096 Jan 16 08:29 .
drwxr-xr-x    1 root     root          4096 Jan 16 08:29 ..
drwxr-xr-x    2 root     root          4096 Jan 16 08:29 daemons
-rw-r--r--    1 root     root            11 Jan 16 08:29 depconfig
-rw-r--r--    1 root     root          2895 Jan 16 08:29 deptree
drwxr-xr-x    2 root     root          4096 Jan 16 08:29 exclusive
drwxr-xr-x    2 root     root          4096 Jan 16 08:29 failed
drwxr-xr-x    2 root     root          4096 Jan 16 08:29 hotplugged
drwxr-xr-x    2 root     root          4096 Jan 16 08:29 inactive
drwxr-xr-x    2 root     root          4096 Jan 16 08:29 options
drwxr-xr-x    2 root     root          4096 Jan 16 08:29 scheduled
drwxr-xr-x    2 root     root          4096 Jan 16 08:29 started
drwxr-xr-x    2 root     root          4096 Jan 16 08:29 starting
drwxr-xr-x    2 root     root          4096 Jan 16 08:29 stopping
drwxr-xr-x    2 root     root          4096 Jan 16 08:29 tmp
drwxr-xr-x    2 root     root          4096 Jan 16 08:29 wasinactive

now we create the missing file :

/ # touch /run/openrc/softlevel

now our rc-service command works perfectly :

/ # rc-service nginx status
 * status: stopped

let's start our server :

 / # rc-service nginx start
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/blkio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpu/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuacct/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuset/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/devices/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/freezer/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/hugetlb/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/memory/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_cls/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_prio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/perf_event/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/pids/tasks: Read-only file system
 * Starting nginx ...          [ ok ]

check if it is started or not :

/ # rc-service nginx status
 * status: started

#ANSWER 2

Or you can simply call those two command lines instead :

/ # openrc
 * Caching service dependencies ...
Service `hwdrivers' needs non existent service `dev'    [ ok ]

/ # touch /run/openrc/softlevel

Now you can start your nginx server :)

/ # rc-service nginx status
 * status: stopped

/ # rc-service nginx start
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/blkio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpu/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuacct/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuset/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/devices/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/freezer/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/hugetlb/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/memory/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_cls/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_prio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/perf_event/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/pids/tasks: Read-only file system
 * /run/nginx: creating directory
 * /run/nginx: correcting owner
 * Starting nginx ...         [ ok ]

/ # rc-service nginx status
 * status: started

Hope I was clear.

like image 80
Holy semicolon Avatar answered Dec 27 '22 21:12

Holy semicolon