H
H
Hfnas2019-07-28 21:03:38
linux
Hfnas, 2019-07-28 21:03:38

How to add an entry line to the end of the /etc/hosts file in a dockerfile?

Purpose of the task: it is necessary that instead of " 127.0.0.1:8080 " you can type http://test in the address bar , those http://127.0.0.1:8080=http://test
1. I write in /etc/apache2/ sites-available/000-default.conf is the following:
in the dockerfile, the line is responsible for this

ADD httpd.conf /etc/apache2/sites-available/000-default.conf

after formation by the build command, there is code in the container
<VirtualHost *:80>
        
        ServerName test
....
</VirtualHost>

2. there is a command in the dockerfile:
RUN
 echo "127.0.0.1 test" >> /etc/hosts \
 && a2enmod rewrite

unfortunately, nothing was added to the /etc/hosts file, but if you run it in the container itself echo "127.0.0.1 test" >> /etc/hosts, it will be added (in this case, I restart Apache, and the container just gets corrupted - forced exit is performed - (Restarting Apache httpd web server: apache2Terminated - and the image has to reload.))
Fully dockerfile
FROM php:7.3.4-apache
ADD httpd.conf /etc/apache2/sites-available/000-default.conf
ADD php.ini /usr/local/etc/php/php.ini
RUN apt-get update \
 && apt-get install -y git libzip-dev sqlite3 mc vim libicu-dev wget unzip\
 && pecl install xdebug-2.7.2 \
 && docker-php-ext-enable xdebug \
 && docker-php-ext-install zip pdo_mysql \
 && docker-php-ext-configure intl \
 && docker-php-ext-install intl \
 && echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \
 && echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini\
 && echo bash -c "127.0.0.1 test" >> /etc/hosts \
 && a2enmod rewrite

the following lines do nothing from this dockerfile, I checked it on the container:
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \
 && echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini\
 && echo bash -c "127.0.0.1 zend_web" >> /etc/hosts \

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
ISE73, 2019-07-29
@Hfnas

the command after && is executed only if the previous one completed without errors.
Since nothing worked for you after "docker-php-ext-install intl ", it means that there is a problem with it.

A
Alexander, 2019-07-29
@shabelski89

It's not clear why this is needed. Leave localhost and that's it. It is better to write the desired name on the machines from where you go to the web server in hosts. In your case, on the host where docker is deployed. You do not access the web from the docker, but from the client, which is on the main host, or on any other PC on this network (by writing pens in the hosts of each ip test) where ip is the address where the docker is running.

T
TyzhSysAdmin, 2019-07-29
@POS_troi

In documentation --add-host

...
--add-host=""      : Add a line to /etc/hosts (host:IP)
...

This is when RUN
Next
It doesn't work that way, at least without a local proxy thread.
So if 127.0.0.1:8080 then test:8080
If I understood you correctly, you want to locally raise the container, in it there is a web service and on the same computer to "log in"?
Then you need to add the line "127.0.0.1 test" to your local host and not the one in the container, because resolving the name to the address happens locally.

A
Anton Kuzmichev, 2019-07-29
@Assargin

Purpose of the task: it is necessary that instead of "127.0.0.1:8080" you can type http://test in the address bar, those http://127.0.0.1:8080=http://test

To solve this problem, you need to do 2 things:
1) manually add the line to the hosts file on your host machine 127.0.0.1 test. This way you will solve the issue with the host name (test instead of IP 127.0.0.1)
2) when starting the container with the web server, bind the web server port from the container with the 80th port of your host machine (using the --publish parameter): what something like docker run --publish 80:8080 ...- it seems to be written here: "bind the internal port of the container 8080 with the external port of the host machine 80" (in your case, it seems like both ports will be 80, both internal and external).
If the external port with which you want to view the website differs from the default one (80 for HTTP and / or 443 for HTTPS) - no prescriptions in any magic files will be able to get rid of the port when typing the address in the address bar of the browser. And one more thing - if you have something
on the host machine that is already listening to the 80th port - well, you never know, maybe some nginx is running or the same Apache - then it will not work to bind the ports, you must first stop the service that is sitting on port 80. Well, or not to bathe and use any other port.
In terms of precisely manipulations with the docker: apart from binding the internal port of the container to the "external" port of your host machine, nothing else needs to be done. The container will not write a line to your host machine file. No, it is technically possible to do this, but it is not absolutely necessary.
And tell me: where did such a need come from in the first place? If you purely make a beautiful address for use locally during the development process - well, ok, although you can do without it. If you are doing this already for production, then everything that you described in the question is not right, wrong, and in the wrong direction.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question