Sto utilizzando Docker Compose per ospitare un piccolo ambiente di sviluppo, che include un contenitore DNS. Ho aggiunto l'IP di docker-machine
come primo server DNS nelle mie impostazioni wifi (non ci sono connessioni cablate):
192.168.99.100
èl'indirizzoIPdidocker-machine
.HoanchesvuotatolacacheDNS/mDNSResponderconilcomandosudodscacheutil-flushcache;sudokillall-HUPmDNSResponder
.
Tutteleutilitàdellarigadicomandoperrisolvereinomihostrisolvonocorrettamenteilnomehost,inclusinslookup
,dig
ehost
:
$nslookup>foo.foobarServer:192.168.99.100Address:192.168.99.100#53Non-authoritativeanswer:Name:foo.foobarAddress:172.21.0.5>exit$digfoo.foobar;<<>>DiG9.8.3-P1<<>>foo.foobar;;globaloptions:+cmd;;Gotanswer:;;->>HEADER<<-opcode:QUERY,status:NOERROR,id:12335;;flags:qrrdra;QUERY:1,ANSWER:1,AUTHORITY:0,ADDITIONAL:0;;QUESTIONSECTION:;foo.foobar.INA;;ANSWERSECTION:foo.foobar.17INA172.21.0.5;;Querytime:4msec;;SERVER:192.168.99.100#53(192.168.99.100);;WHEN:MonJun607:49:502016;;MSGSIZErcvd:44$hostfoo.foobarfoo.foobarhasaddress172.21.0.5Hostfoo.foobarnotfound:3(NXDOMAIN)
L'unicacosasospettanell'outputprecedenteèl'ultimariga.QuandoIcurl
owget
pernome,ottengounerrore:
$curlhttp://foo.foobarcurl:(6)Couldnotresolvehost:foo.foobar$wgethttp://foo.foobar--2016-06-0608:27:05--http://foo.foobar/Resolvingfoo.foobar...failed:nodenamenorservnameprovided,ornotknown.wget:unabletoresolvehostaddress'foo.foobar'
L'utilizzodell'indirizzoIPfunziona,confermandocheimieipercorsisonoimpostaticorrettamente:
$curlhttp://172.21.0.5Hellofoo$wgethttp://172.21.0.5--2016-06-0608:28:22--http://172.21.0.5/Connectingto172.21.0.5:80...connected.HTTPrequestsent,awaitingresponse...200OKLength:9[text/html]Savingto:'index.html'index.html100%[==============================================>]9--.-KB/sin0s2016-06-0608:28:22(732KB/s)-'index.html'saved[9/9]
Infine,quandoutilizzoChromeoSafariperrecuperarehttp://foo.foobar
,ricevoerroriDNS(el'elaborazionedell'IPfunzionacorrettamente).
Sembrachecisianomoltipostsuquestonell'interwewe,manessunodilorosembrafunzionareperme.
QualèlasalsasegretaperconvincereElCapitanarisolverecorrettamenteilnomehostfoo.foobar
?
Modifica:aggiungiloscreenshotdeiproxy:
Modifica2:aggiungil'outputdellaversionedocker:
$docker--versionDockerversion1.11.0,build4dc5990$docker-compose--versiondocker-composeversion1.7.0,build0d7bf73$docker-machine--versiondocker-machineversion0.7.0,builda650a40
Modifica3:aggiungiifiledifinestramobile
NB:horimossoleinformazionisensibili
Eccoloscriptcheusiamopergeneraretuttoedeseguirecomandinelcontenitoredell'app;èpensatoperessereeseguitodaunadirectoryrootdell'applicazionenode.js(contenenteilfilepackage.json
)sulcomputerhost(mappatoa/app
nelcontenitoredell'app)ilcuibasenamefungeanchedanomedell'applicazioneeassumedocker-machinestartdefault
ègiàstatoeseguitocorrettamente:
#!/usr/bin/envbashif[!-f~/.npmrc];thentouch~/.npmrcfiif["$(uname)" == "Darwin" ]; then
eval $(docker-machine env default)
fi
if [ -z "$APP_NAME" ]; then
APP_NAME=${PWD##*/} # (basename of the current directory)
fi
if [ -d .git ]; then
APP_TAG=$(git status | head -n 1 | awk '{ print $3 }')
else
APP_TAG=master
fi
docker-compose up -d
if [ "$#" -eq 0 ]; then
CMD=bash
else
CMD="$@"
fi
docker exec -it ${APP_NAME}-app $CMD
Ecco il file Docker della nostra app (trovato in ./docker/Dockerfile
, dove .
è la directory root dell'applicazione node.js):
FROM node:4
# essential invariants
ENV PATH ./node_modules/.bin:$PATH
RUN apt-get update && \
apt-get install -y openjdk-7-jre-headless # (we need java7)
RUN apt-get install -y sudo
# app & user specifics
ARG UID
ARG USER
RUN useradd -ms /bin/bash ${USER:-docker} --uid ${UID:-1000}
RUN adduser ${USER:-docker} sudo
RUN echo "${USER:-docker} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
USER ${USER:-docker}
COPY .setup.sh /.setup.sh
CMD ["/.setup.sh"]
Ecco il file .setup.sh
, trovato accanto a cui ci si riferisce dal file Docker sopra; risolve i problemi relativi ai permessi dei file con i volumi condivisi dal computer host:
#!/usr/bin/env bash
# Ensures current user is the owner of /app's files
UID=$(id -u)
FILE_OWNER_UID=$(stat -c '%u' /app/package.json)
USER=$(id -un)
if [ ! $UID == $FILE_OWNER_UID ]; then
echo "Changing ${USER}'s uid from $UID to $FILE_OWNER_UID"
sudo usermod -u $FILE_OWNER_UID $USER
fi
# Keeps container running
sleep infinity
Ecco il nostro docker-compose.yaml
:
version: '2'
services:
app:
build:
context: ./docker
args:
USER: docker
image: ${APP_NAME}:${APP_TAG}
environment:
FORCE_AMQP: 1
AMQP_HOST: ${APP_NAME}-amqp
PORT: 80
MONGO_HOST: ${APP_NAME}-mongo
depends_on:
- amqp
- mongo
hostname: ${APP_NAME}-app
container_name: ${APP_NAME}-app
volumes:
- ./:/app
- ~/.npmrc:/home/docker/.npmrc
- ~/.npm:/home/docker/.npm
networks:
network:
aliases:
- ${APP_NAME}.foobar
working_dir: /app
amqp:
hostname: ${APP_NAME}-amqp
container_name: ${APP_NAME}-amqp
image: rabbitmq
networks:
- network
mongo:
hostname: ${APP_NAME}-mongo
container_name: ${APP_NAME}-mongo
image: mongo:3.0.4
networks:
- network
dns:
hostname: ${APP_NAME}-dns
container_name: ${APP_NAME}-dns
image: andyshinn/dnsmasq
cap_add:
- NET_ADMIN
ports:
- 53:53/udp
- 53:53/tcp
command: --max-cache-ttl=30 --max-ttl=30
networks:
- network
networks:
network:
driver: bridge
Ecco come aggiungiamo un percorso alla rete di docker-machine
per macchine Mac OS X che usano docker-machine
tramite VirtualBox (non dlite
):
#!/usr/bin/env bash
eval $(docker-machine env default)
DOCKER_IP=$(docker-machine ip default)
sudo route -n add $(docker network inspect --format '{{ range .IPAM.Config }}{{ .Subnet}}{{end}}' ${PWD##*/}_network) $DOCKER_IP