0

I'm trying to inject a new mapping in my contained /ect/hosts, without luck so far.

This is my docker-compose file content:

version: '3.5'

services:
    mix-portal-ui:
        build: 
            context: ../mix-portal-ui
            dockerfile: Dockerfile-base
        container_name: mix-portal-ui
        volumes:
            - ./app:/home/webapp/app:cached
            - ./scripts:/home/webapp/scripts:cached
        extra_hosts:
            - "entrd-artifactory: 10.28.18.18"
        networks:
            mix-network:
                aliases:
                    - mixui

My Dockerfile-base content is:

FROM node:10.18.1-alpine

RUN cat /etc/hosts
RUN ls -ltr /etc/hosts

RUN echo "10.28.18.18    entrd-artifactory" >> /etc/hosts
RUN cat /etc/hosts
# Add standard user so that npm is not run as root.
RUN adduser -D webapp && apk add --no-cache git bash
USER webapp
ENV HOME /home/webapp
WORKDIR $HOME

# This will reuse the cache and save a significant amout of time
# Unless package.json has changed. In that case all the commands
# after this one will be re-run
ADD ./package.json      $HOME/
ADD ./.npmrc      $HOME/
RUN npm install --loglevel warn

# Now we're free to add any additional files, they won't trigger the
# npm install when they change.
ADD ./.babelrc          $HOME/.babelrc
ADD ./.eslintrc         $HOME/.eslintrc
ADD ./scripts           $HOME/scripts/

# Run
CMD ["npm", "run", "dev"]

Nomatter what I've tried the /etc/hosts file doesn't get updated with the mapping : 10.28.18.18 entrd-artifactory. It feels like that hosts files is locked by something, although there are no errors being thrown when trying to alter it.

The build output is :

build mix-portal-ui
Building mix-portal-ui
Step 1/16 : FROM node:10.18.1-alpine
 ---> cb69d515e572
Step 2/16 : RUN cat /etc/hosts
 ---> Running in f7f5542ce95e
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      f7f5542ce95e
Removing intermediate container f7f5542ce95e
 ---> 1a7182675d98
Step 3/16 : RUN ls -ltr /etc/hosts
 ---> Running in 1c378471818b
-rw-r--r--    1 root     root           174 Jun  4 15:47 /etc/hosts
Removing intermediate container 1c378471818b
 ---> bad80dd1b854
Step 4/16 : RUN echo "10.28.18.18    entrd-artifactory" >> /etc/hosts
 ---> Running in 2008fbd0b8da
Removing intermediate container 2008fbd0b8da
 ---> 5d902c1a486f
Step 5/16 : RUN cat /etc/hosts
 ---> Running in 441310e8803a
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      441310e8803a
Removing intermediate container 441310e8803a
 ---> 1e4798b158f9
......

The docker-compose version i use is: [root@mixvm local-build-tools]# docker-compose version docker-compose version 1.22.0, build f46880f docker-py version: 3.5.0 CPython version: 3.7.3 OpenSSL version: OpenSSL 1.1.1c FIPS 28 May 2019

the docker version is : [root@mixvm local-build-tools]# docker -v Docker version 18.09.5, build e8ff056

Any idea what is going on here?

Thanks

Yonoss
  • 1,242
  • 5
  • 25
  • 41
  • Does this answer your question? [How to update /etc/hosts file in Docker image during "docker build"](https://stackoverflow.com/questions/38302867/how-to-update-etc-hosts-file-in-docker-image-during-docker-build) – Adiii Jun 04 '20 at 17:41

2 Answers2

2

This usage of extra_hosts is a run-time modification to /etc/hosts. Nothing happens at build time. See https://github.com/docker/cli/issues/1293. You should see the extra host when you up the container.

To add an extra host at build-time, you can try what the linked issue suggests (note the version):

version: '2.3'
services:
    mix-portal-ui:
        build: 
            context: ../mix-portal-ui
            dockerfile: Dockerfile-base
            extra_hosts:
                - "entrd-artifactory:10.28.18.18"

The only other alternative seems to be building with docker-build and using the --add-host option.

chash
  • 3,975
  • 13
  • 29
0

Updated /etc/hosts is lost while building the image.

Pay attention that echo updated /etc/hosts in container 2008fbd0b8da and it was removed before step 5.

Step 4/16 : RUN echo "10.28.18.18 entrd-artifactory" >> /etc/hosts ---> Running in 2008fbd0b8da Removing intermediate container 2008fbd0b8da ---> 5d902c1a486f Step 5/16 : RUN cat /etc/hosts ---> Running in 441310e8803a

Updating /etc/hosts should be done after you started the container by appending to it inside the container.

Or better running the container with --add-host flag or extra_hosts when using docker compose.

From the compose docs:

An entry with the ip address and hostname is created in /etc/hosts inside containers for this service, e.g:

From docker run docs:

The --add-host flag can be used to add additional lines to /etc/hosts

rok
  • 9,403
  • 17
  • 70
  • 126