Run docker commands on all or some containers

  • Use the ps command with the q param to fetch the insance id and then apply the command:
    • docker {COMMAND TO RUN} $(docker ps -a -q)  -> for containers
    • docker {COMMAND TO RUN} $(docker images -q) -> for images
  • Examples:
    • Delete all stopped containers: docker rm $(docker ps -a -q)
    • Delete all unused images: docker rmi $(docker images -q)
    • Start all stopped containers: docker start $(docker ps -a -q)
    • Restart all containers: docker restart $(docker ps -a -q)
  • For more elavorated filters:
    • docker  {COMMAND TO RUN} $(docker ps -a |grep {TEXT-TO-CAPTURE}|awk ‘{ print $1 }’)  wehre awk ‘{ print $1 }’ captures the container id value.

Configure a local development environment on MacOS with Docker

Using dnsmasq for development domains

With dnsmasq I am redirecting my developer domains to its corresponding target:

  • *.dev: localhost
  • *.dock: docker container

So no more ip stuff on the browser url.

Install dmasq on mac OS X:

(from http://passingcuriosity.com/2013/dnsmasq-dev-osx/)

brew install dnsmasq
# Copy the default configuration file.
cp $(brew list dnsmasq | grep /dnsmasq.conf.example$) /usr/local/etc/dnsmasq.conf
# Copy the daemon configuration file into place.
sudo cp $(brew list dnsmasq | grep /homebrew.mxcl.dnsmasq.plist$) /Library/LaunchDaemons/
# Start Dnsmasq automatically.
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

Configure dmasq:

(/usr/local/etc/dnsmasq.conf)

#Redirect *.dev urls to 127.0.0.1
address=/dev/127.0.0.1

#Redirect *.dock urls to docker host url (192.168.99.100 in my case)
address=/dock/192.168.99.100

and restart dnsmasq:

sudo launchctl stop homebrew.mxcl.dnsmasq
sudo launchctl start homebrew.mxcl.dnsmasq

Then you need to say the OS to use Dnsmasq for the desired domain. Most UNIX-like operating systems have a configuration file called /etc/resolv.conf which controls the way DNS queries are performed, including the default server to use for DNS queries (this is the setting that gets set automatically when you connect to a network or change your DNS server/s in System Preferences).

OS X also allows you to configure additional resolvers by creating configuration files in the/etc/resolver/ directory. This directory probably won’t exist on your system, so your first step should be to create it:

sudo mkdir -p /etc/resolver

Now you should create a new file in this directory for each resolver you want to configure.  There a number of details you can configure for each resolver but I generally only bother with two:

  • the name of the resolver (which corresponds to the domain name to be resolved); and
  • the DNS server to be used.

Create a new file with the same name as your new top-level domain (I’m using dec and dock) in the /etc/resolver/ directory and add a nameserver to it by running the following commands:

sudo tee /etc/resolver/dev >/dev/null <<EOF
nameserver 127.0.0.1
EOF

Binding all services

I am developing an application that needs several resources:

  • Wildfly AS
  • Mysqls DB
  • keycloak
  • An nginx server as proxy.

To avoid to write each time the ports, I am using nginx to redirect the requests to the correct container. All the containers must be able to locate the proxy server and the proxy will do the rest. To achieve that, when we run a container we need to link it to the proxy server with the domain of the required service. E.g. to get my widlfly App signing in using the keycloak server, we will run the containers as follows:

docker run --link proxy:keycloak.dock some/wildlfy
docker run --link proxy:app.dock some/keycloak

Configuring the proxy:

In order to allow the proxy to bind all services we need to add some server configs: For the widlfy app, a file named app.dock will be added in the conf.d directory:

server {
    listen      80;
    server_name app.dock;

        location / {
            proxy_pass http://${docker-host-ip}:${widlfly-app-port};
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      # needed to map the ip address to the correct domain
      proxy_redirect http://${docker-host-ip}:${widlfly-app-port} http://app.dock;
            proxy_redirect ${docker-host-ip}:${keycloak-port} http://keycloak.dock;
        }

    access_log /var/log/nginx/app.dock_access.log;
        error_log /var/log/nginx/app.dock_error.log;
}

and for the keycloak:

server {
    listen      80;
    server_name app.dock;

        location / {
            proxy_pass http://${docker-host-ip}:${keycloak-port};
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      # needed to map the ip address to the correct domain
            proxy_redirect ${docker-host-ip}:${keycloak-port} http://keycloak.dock;
        }

    access_log /var/log/nginx/app.dock_access.log;
        error_log /var/log/nginx/app.dock_error.log;
}

Run nano on docker ubuntu image

I have tried to run nano on an ubuntu docker image and after installing it I always have this error:
Error opening terminal: unknown.

The solution is so easy as to run into the docker image export TERM=xterm

Problem is that it does not survive the restart, as in any bash session actually. I will try to add it on the Dockerfile but why that the term is set on the run command I am not very optimistic with it.

I have tried to run nano on an ubuntu docker image and after installing it I always have this error:
Error opening terminal: unknown.

The solution is so easy as to run into the docker image export TERM=xterm