After adding the command, the container does not run

Hello,
I added a command as follows to the YAML file:

version: '3.9'
services:
  web:
    image: nginx:latest
    container_name: Nginx
    command: sh -c "apt update && apt install iputils-ping -y"
    ports:
      - '8080:80'
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
      - /var/www/html:/usr/share/nginx/html
    depends_on:
       - php-fpm
  php-fpm:
    image: php:8-fpm
    container_name: PHP
    volumes:
       - /var/www/html:/usr/share/nginx/html/
       - /var/www/html:/usr/local/apache2/htdocs/

But after this, the Nginx container will no longer run. The log tells me that everything seems to have run successfully:

Attaching to Nginx, PHP
PHP    | [30-Mar-2024 12:47:38] NOTICE: fpm is running, pid 1
PHP    | [30-Mar-2024 12:47:38] NOTICE: ready to handle connections
Nginx  | 
Nginx  | WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Nginx  | 
Nginx  | Get:1 http://deb.debian.org/debian bookworm InRelease [151 kB]
Nginx  | Get:2 http://deb.debian.org/debian bookworm-updates InRelease [55.4 kB]
Nginx  | Get:3 http://deb.debian.org/debian-security bookworm-security InRelease [48.0 kB]
Nginx  | Get:4 http://deb.debian.org/debian bookworm/main amd64 Packages [8786 kB]
Nginx  | Get:5 http://deb.debian.org/debian bookworm-updates/main amd64 Packages [12.7 kB]
Nginx  | Get:6 http://deb.debian.org/debian-security bookworm-security/main amd64 Packages [147 kB]
Nginx  | Fetched 9201 kB in 47s (194 kB/s)
Nginx  | Reading package lists...
Nginx  | Building dependency tree...
Nginx  | Reading state information...
Nginx  | All packages are up to date.
Nginx  | 
Nginx  | WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Nginx  | 
Nginx  | Reading package lists...
Nginx  | Building dependency tree...
Nginx  | Reading state information...
Nginx  | The following additional packages will be installed:
Nginx  |   libcap2-bin libpam-cap
Nginx  | The following NEW packages will be installed:
Nginx  |   iputils-ping libcap2-bin libpam-cap
Nginx  | 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Nginx  | Need to get 96.2 kB of archives.
Nginx  | After this operation, 311 kB of additional disk space will be used.
Nginx  | Get:1 http://deb.debian.org/debian bookworm/main amd64 libcap2-bin amd64 1:2.66-4 [34.7 kB]
Nginx  | Get:2 http://deb.debian.org/debian bookworm/main amd64 iputils-ping amd64 3:20221126-1 [47.1 kB]
Nginx  | Get:3 http://deb.debian.org/debian bookworm/main amd64 libpam-cap amd64 1:2.66-4 [14.5 kB]
Nginx  | debconf: delaying package configuration, since apt-utils is not installed
Nginx  | Fetched 96.2 kB in 0s (219 kB/s)
Nginx  | Selecting previously unselected package libcap2-bin.
(Reading database ... 7590 files and directories currently installed.)
Nginx  | Preparing to unpack .../libcap2-bin_1%3a2.66-4_amd64.deb ...
Nginx  | Unpacking libcap2-bin (1:2.66-4) ...
Nginx  | Selecting previously unselected package iputils-ping.
Nginx  | Preparing to unpack .../iputils-ping_3%3a20221126-1_amd64.deb ...
Nginx  | Unpacking iputils-ping (3:20221126-1) ...
Nginx  | Selecting previously unselected package libpam-cap:amd64.
Nginx  | Preparing to unpack .../libpam-cap_1%3a2.66-4_amd64.deb ...
Nginx  | Unpacking libpam-cap:amd64 (1:2.66-4) ...
Nginx  | Setting up libcap2-bin (1:2.66-4) ...
Nginx  | Setting up libpam-cap:amd64 (1:2.66-4) ...
Nginx  | debconf: unable to initialize frontend: Dialog
Nginx  | debconf: (TERM is not set, so the dialog frontend is not usable.)
Nginx  | debconf: falling back to frontend: Readline
Nginx  | debconf: unable to initialize frontend: Readline
Nginx  | debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.36.0 /usr/local/share/perl/5.36.0 /usr/lib/x86_64-linux-gnu/perl5/5.36 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl-base /usr/lib/x86_64-linux-gnu/perl/5.36 /usr/share/perl/5.36 /usr/local/lib/site_perl) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
Nginx  | debconf: falling back to frontend: Teletype
Nginx  | Setting up iputils-ping (3:20221126-1) ...

What is wrong?

Cheers.

You run apt command, which will be done quickly, then the container is terminated. You need to add the regular process, too.

See original Dockerfiles in repo:

CMD ["nginx", "-g", "daemon off;"]

Beat practice would be to build your own image based on nginx, not do this on every container startup.

Using your way might lose the ability to pass SIGTERM to the app, so you might not have graceful shutdown with nginx, waiting 10 secs for ongoing requests to finish.

1 Like

Hi,
Thank you so much for your reply.
How can I fix this problem by changing the YAML file? How to add the regular process?

Hello,
Nginx works by creating a Dockerfile and adding the following line:

RUN apt update && apt install iputils-ping -y

So what is the use of the command command? Even the below lines could not fix the problem:

command:
       - bash
       - -c
       - |
         apt update
         apt install iputils-ping -y
         update-rc.d -f nginx remove

Let’s keep it simple: just run nginx after apt commands:

nginx -g daemon off;

Hi,
Thanks again.
Do you mean something like below?

command:
       - bash
       - -c
       - |
         apt update
         apt install iputils-ping -y
         nginx -g daemon off;

If yes, then I did this too, but I got an error and because of it, I used update-rc.d -f nginx remove command.

Just create your own image with a Dockerfile:

# Use the official Nginx image as the base
FROM nginx:latest

# Install iputils-ping and clean up in a single RUN to reduce image size
RUN apt-get update \
    && apt-get install -y iputils-ping \
    && rm -rf /var/lib/apt/lists/*

# Expose port 80
EXPOSE 80

# Start Nginx in the foreground
CMD ["nginx", "-g", "daemon off;"]

Then build it:

docker build -t nginx-ping .

Hi,
Thanks again.
I know it is possible with Dockerfile, but I want to do it through YAML file.

Running apt update in yaml is a bad idea, as it will run every time you start or restart your container.

You run the risk of sudden incompatibilities during an upgrade (“today package x was upgraded to 1.23.45, there is a problem with y 2.34.56”), and its gonna be very hard to reproduce, as it changes all the time.

It’s better to have a stable built image that you know works.

1 Like

Hi,
Thanks again.
When the apt command finishes, isn’t control returned to the container? Is this one of the problems with using the command?

Dockerfile usually uses entrypoint (mostly a script) and command (as parameters).

Nothing happens after entrypoint/command is executed.

1 Like

Hello,
Thanks again.
Do I have to write this entrypoint file myself or can I use these files?