Docker tips

Sun 24 May 2015 | tags: Docker

For my tips about docker compose, go here.

Create an image

docker build -t <name> .

Save and import

docker save <image> > file
docker load < <image>

Run

docker run --name aot -d -p 8080:8080 --link redis:redis aot

Attach to a running container

If you are running docker 1.3 or above, you should use: docker exec -it CONTAINER COMMAND to run COMMAND within the container. You can easily create a function to ease the thing and run bash by default:

dk-enter() {
    docker exec -it "$1" "${2:-/bin/bash}"
}

You can easily open a shell in a running docker container with the following commands:

  1. Get the container pid: docker inspect -f {{.State.Pid}} <container-id>
  2. Connect: nsenter --target <pid> --mount --uts --ipc --net --pid

Use customs DNS

During development you may need to use custom domains to reach your site. Since these domains aren't real and must point to your machine, you need to use a custom DNS resolver. I'll detail here, how to acheive this with dnsmaq and Network Manager.

First, you need to install dnsmaq. It should be in the repositories of your distribution.

Then you need to map your domains to the proper ip addresses. To do that, create as root /etc/NetworkManager/dnsmasq.d/mapping.conf and add something like this to map the domain to the ip:

address=/dev.here/127.0.0.1

Now, as described here you need to configure Network Manager to listen to the docker bridge, so the DNS requests from your containers are correctly passed to dnsmasq. To do so, create /etc/NetworkManager/dnsmasq.d/docker-bridge.conf with this content (if you use another ip than the default one, you will have to adapt this):

listen-address=172.17.0.1

Restart Network Manager for the changes to be taken into account: systemctl restart NetworkManager.

Then, as described here, we need to configure the docker daemon to use our DNS. To to that, create /etc/docker/daemon.json with this content (or add the dns entry if the file already exists):

{
  "dns": [
    "172.17.0.1"
  ]
}

Restart docker for the changes to be taken into account: systemctl restart docker.

Finally, you need to allow DNS requests in your firewall (the commands below are for firewalld, default firewall on Fedora):

  1. Allow permanently the DNS service in the firewall (remove --permanent and don't run the second command to make this temporary): firewall-cmd --add-service=dns --permanent
  2. Reload the configuration: firewall-cmd --reload

blogroll

social

>