Skip to content

Docker

Commands

  • Logs: docker logs container
  • Logs since: docker logs --since 2m container (2m, 5h)
  • Last logs: docker logs --tail 100 container
  • Follow logs: docker logs --follow --tail 10 container
  • Info on sizes: docker system df -v
  • Show commands inside containers: docker top container

Build image

docker build --no-cache --progress=plain . 2>&1 | tee build.log

Move an image between hosts

docker image save -o image.tar image:tag
# Copy the file
docker image load --input image.tar

Fix network not found on docker compose up

If the network changes and you try to start a container, the container fails with a message similar to the following:

ERROR: for SERVICE  Cannot start service SERVICE: Could not attach to network xq8k2g3yn3g15d1tonbmas6b4: rpc error: code = NotFound desc = network xq8k2g3yn3g15d1tonbmas6b4 not found
ERROR: for SERVICE  Cannot start service SERVICE: network eqm5t4dml9e05oqyb80lw7b7g not found

Docker compose up will create the network but the new network will have another ID and docker compose will not edit the container to use the new network. So after a docker-compose up -d you have to connect the network to the container.

So first, we make sure it was created.

docker network ls
---
NETWORK ID          NAME                     DRIVER              SCOPE
fl6dacfxefa0        NETWORK_NAME             overlay             local

And add it to the container.

docker network connect NETWORK_NAME CONTAINER_NAME

Copy a file from container to the host

docker cp <container>:/path/in/container /host/path/

Start container in interactive mode

In bash:

docker run --rm -it -v $(pwd):/usr/src/project gcc:4.9

In powershell:

docker run -it --entrypoint=/bin/sh -v ${pwd}:/mnt pdftk/pdftk:latest

In CMD:

docker run --rm -it -v %cd%:/mnt gcc:4.9


Compose

  • Rebuild an image docker-compose up -d --no-deps --build <service_name>

Network definition

Docker may step on 192.168.x.x networks. To avoid it, change the default network address bip and the pool of addresses default-address-pools where base is the whole network size and size is the size of each subnetwork.

/etc/docker/daemon.json
---
{
  "bip": "172.16.20.1/16",
  "default-address-pools": [
    {
      "base": "172.17.0.0/16",
      "size": 24
    }
  ]
}

Check current configuration with docker info.