Tips and Tricks

Enable to non-root users to run Docker commands

  • Add the docker group if it doesn’t exist# groupadd docker
  • Add your username to docker group# usermod -aG docker [username]
  • Restart the Docker daemon# service docker restartor# systemctl restart docker

Get a full container information

# docker inspect [container ID]

Get container IP address

# docker inspect ‘{{.NetworkSettings.IPAddress}}’ [container ID]

Login into container

# docker exec -it [container ID] bash

See the console output of the running container

# docker logs -f [container ID]

Remove containers that are more than a month old

# docker ps -a | grep 'months ago' | awk '{print $1}' | xargs docker rm

Set a hostname to the container

# docker run -dit --hostname=[container hostname] [image name]

Publish a container’s port to the host

# docker run -dit -p 8000:80 [image name]

How to mount the host directory into the new container

# docker run -dit -v [local directory]:[directory inside container] [image name]

How to delete all existing containers

# docker rm $(docker ps -qa)

How to save the modified container

# docker commit [container ID] [new image name]:[tag]

How to see the size of each image layer

# docker history [image ID]

How to start a stopped container

# docker start [container ID]