I
I
ioseogio2021-04-16 14:10:47
Nginx
ioseogio, 2021-04-16 14:10:47

How to change hosts file in read-only docker nginx container?

There is a docker container that has been doing great for several months.
After the next update of docker, during the assembly, I began to complain that I could not write addresses to the hosts file, because. read-only system.

DOCKERFILE

spoiler
FROM nginx:alpine

RUN echo admin.localhost 127.0.0.1 >> /etc/hosts
RUN echo api.localhost 127.0.0.1 >> /etc/hosts

Assembly process
spoiler
D:\Programs\openserver\domains\app>docker-compose up -d --build
Building php
failed to get console mode for stdout: The handle is invalid.
[+] Building 0.2s (11/11) FINISHED
 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 417B                                       0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [internal] load metadata for docker.io/library/php:7.3-fpm             0.0s
 => [1/7] FROM docker.io/library/php:7.3-fpm                               0.0s
 => CACHED [2/7] RUN docker-php-ext-install pdo_mysql                      0.0s
 => CACHED [3/7] RUN apt-get update -y && apt-get install -y sendmail lib  0.0s
 => CACHED [4/7] RUN apt-get update &&      apt-get install -y          l  0.0s
 => CACHED [5/7] RUN docker-php-ext-install zip                            0.0s
 => CACHED [6/7] RUN docker-php-ext-configure gd --with-png-dir=/usr --wi  0.0s
 => CACHED [7/7] RUN docker-php-ext-install gd                             0.0s
 => exporting to image                                                     0.0s
 => => exporting layers                                                    0.0s
 => => writing image sha256:0aece223026773ece54fb2924e153ea0c0788ae3b8bc8  0.0s
 => => naming to docker.io/library/app_php                                 0.0s
Successfully built 0aece223026773ece54fb2924e153ea0c0788ae3b8bc87b96eb366e80be3d479
Building nginx
failed to get console mode for stdout: The handle is invalid.
[+] Building 0.4s (4/6)
[+] Building 0.4s (5/6)
 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 154B                                       0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [internal] load metadata for docker.io/library/nginx:alpine            0.0s
 => CACHED [1/3] FROM docker.io/library/nginx:alpine                       0.0s
 => ERROR [2/3] RUN echo admin.localhost 127.0.0.1 >> /etc/hosts           0.3ss]: exit code: 1
------
 > [2/3] RUN echo admin.localhost 127.0.0.1 >> /etc/hosts:
#4 0.259 /bin/sh: can't create /etc/hosts: Read-only file system
------
ERROR: Service 'nginx' failed to build


What can be done? Run in offline read-only mode? Frauds with the root user and sudo do not give any result.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vadim, 2021-04-16
@ioseogio

Yes, the latest alpine brought a lot of problems...
The easiest solution is to use not
FROM nginx:alpine
but the previous version from
https://hub.docker.com/_/nginx?tab=tags&page=1&ord...
more complex solution, not checked)))
try to use a script in docker ENTRYPOINT, for example script.sh in which to write something like
umount /etc/hosts && echo '127.0.0.1 localhost' > /etc/hosts && your_container_process_or_command

T
teewrew, 2021-04-16
@teewrew

Files cannot be modified on a read-only system. Change the hosts file on the target system

I
ioseogio, 2021-04-16
@ioseogio

As a result, I just threw out the dockerfile, through volume I throw in the nginx configuration, in which I specify several virtual servers. Everything runs on docker:alphine

A
Alexander Karabanov, 2021-04-16
@karabanov

There is extra_hosts for docker-compose or --add-host if the container is started manually.
But, as already mentioned, you can change /etc/hosts on the host machine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question