Archive 2018

docker could not resolve deb.debian.org

While building a Dockerfile which installs some packages, following error is raised:

Could not resolve 'deb.debian.org'

In fact, docker daemon can’t resolve specified host, because no dns server is set in config.

To fix issue, at least one valid dns server must be configured in /etc/docker/daemon.json like so (google dns as example):

{
    "dns": ["8.8.8.8"]
}

And then restart docker daemon sudo service docker restart

register and update docker containers ips in etc hosts

Use following script to update /etc/hosts with entries of docker containers mycontainersXXXXwitr.local

# delete lines between '# docker-compose containers start' and '# docker-compose containers end' in /etc/hosts
sudo sed -i.bak '/^# docker-compose containers start/,/# docker-compose containers end/d' /etc/hosts


echo -e "\nyour /etc/hosts is updated with following:"
echo "======================"
echo "" | sudo tee -a /etc/hosts
echo "# docker-compose containers start" | sudo tee -a /etc/hosts
echo "" | sudo tee -a /etc/hosts
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} {{.Name}}' $(docker ps --filter name=mycontainers.*witr.local -q) | sed 's/\///' | sudo tee -a /etc/hosts
echo "" | sudo tee -a /etc/hosts
echo "# docker-compose containers end" | sudo tee -a /etc/hosts
echo "======================"

enable CORS with spring

CORS could be enabled with spring programmatically. One of the ways to enable it is to add mapping to CorsRegistry like so:

@Bean
public WebMvcConfigurer corsConfigurer() {
  return new WebMvcConfigurerAdapter() {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
      registry.addMapping("/**").allowedOrigins("*");
    }
  };
}

ansible with docker containers as target

Ansible needs ssh access to target machines. But for testing It’s so heavy to use vagrant/virtualbox vm.

Even if docker containers are not the appropriate target to be used with ansible, but light weight and speed starting of containers helps to quickly test playbooks.

To do we will run an ssh server inside our docker container.

Create Dockerfile

FROM debian:jessie

RUN apt update && DEBIAN_FRONTEND=noninteractive apt install -y openssh-server sudo python python-apt apt-transport-https

RUN apt install -y unzip

RUN mkdir -p /var/run/sshd && sed -i "s/UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config \
  && sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config \
  && touch /root/.Xauthority \
  && true

RUN useradd myuser \
        && passwd -d mypassword \
        && mkdir /home/myuser \
        && chown myuser:myuser /home/myuser \
        && addgroup myuser staff \
        && addgroup myuser sudo \
        && true

RUN echo "%sudo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

ADD ./entrypoint.sh /entrypoint.sh

EXPOSE 22
ENTRYPOINT ["/entrypoint.sh"]

Create entrypoint.sh:

#!/bin/bash
set -e

if [ -z "${SSH_KEY}" ]; then
        echo "ERROR: missed public key in the SSH_KEY environment variable"
        exit 1
fi

for MYHOME in /root /home/myuser; do
        echo "=> Adding SSH key to ${MYHOME}"
        mkdir -p ${MYHOME}/.ssh
        chmod go-rwx ${MYHOME}/.ssh
        echo "${SSH_KEY}" > ${MYHOME}/.ssh/authorized_keys
        chmod go-rw ${MYHOME}/.ssh/authorized_keys
        echo "${MYHOME} ssh configured: OK"
done
chown -R myuser:myuser /home/myuser/.ssh

echo "========================================================================"
echo "You can now connect to this container via SSH using:"
echo ""
echo "    ssh root@<host>"
echo "    ssh myuser@<host>"
echo ""
echo "========================================================================"

exec /sbin/init
exec /usr/sbin/sshd -D

Build and run container

docker build -t my/image .
docker run -d my/image