How to set hosts in docker for mac

When I use docker before, I can use docker-machine ssh defaultto install hosts in the docker machine /etc/hosts, but in docker for mac I can’t access it. VM because of this does not have it. So, the problem how to set hosts in docker for mac? My secondary domain wants to specify a different ip.

+4
source share
4 answers

I found a solution using this command

screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty

Now edit /etc/hostsin the Docker VM.
To exit screen, use Ctrl + a + d.

+3
source

This is how I do it with a bash script, so the changes are saved between rebooting Docker for Mac.

cd ~/Library/Containers/com.docker.docker/Data/database
git reset --hard

DFM_HOSTS_FILE="com.docker.driver.amd64-linux/etc/hosts"
if [ ! -f ${DFM_HOSTS_FILE} ]; then
  echo "appending host to DFM /etc/hosts"
  echo -e "xxx.xxx.xxx.xxx\tmy.special.host" > ${DFM_HOSTS_FILE}
  git add ${DFM_HOSTS_FILE}
  git commit -m "add host to /etc/hosts for dns lookup"
fi
+1
source

script, , .

#!/bin/sh 
# host entry -> '10.4.1.4   dockerreigstry.senz.local'
# 1. run debian image
# 2. check host entry exists in /etc/hosts file
# 3. if not exists add it to /etc/hosts file
docker run --name debian -it --privileged --pid=host debian nsenter \
    -t 1 -m -u -n -i sh \
    -c "if ! grep -q dockerregistry.senz.local /etc/hosts; then echo -e '10.4.1.4\tdockerregistry.pagero.local' >> /etc/hosts; fi"

# sleep 2 seconds
# remove stopped debian container
sleep 2
docker rm -f debian

.

https://medium.com/@itseranga/set-hosts-in-docker-for-mac-2029276fd448

+1
source

You need to create a file docker-compose.yml. This file will be on the same path as yours. Dockerfile For example, I use this docker-compose.yml file:

version: '2'
services:
  app:
    hostname: app
    build: .
    volumes:
      - ./:/var/www/html
    working_dir: /var/www/html
    depends_on:
      - db
      - cache
    ports:
      - 80:80
  cache:
    image: memcached:1.4.27
    ports:
      - 11211:11211
  rabbitmq:
    image: rabbitmq:latest
    ports:
      - 5672:5672
  db:
    image: postgres:9.5.3
    ports:
      - 5432:5432
    environment:
      - TZ=America/Mazatlan
      - POSTGRES_PASSWORD=root
      - POSTGRES_DB=restaurantcore
      - POSTGRES_USER=rooms
      - POSTGRES_PASSWORD=rooms

Ports bind to the ports of your host dock.

0
source

Source: https://habr.com/ru/post/1648880/


All Articles