Buy Me a Coffee

Docker Handbook (🚧Continuous Update)

1. Install docker and docker-compose on Ubuntu

sudo apt-get update
sudo apt install docker.io
sudo apt install docker-compose

2. Configure rootless user to run docker

sudo groupadd docker
sudo usermod -aG docker $USER

# Linux user can run below to activate the changes
newgrp docker

docker ps

https://docs.docker.com/engine/install/linux-postinstall/

3. List and delete images

docker images

docker iamge ls

docker rmi <image_id_1> <image_id_2>

4. Check if the specific docker image exists

IMAGE_ID=$(docker images | grep 'portainer/base' | awk '{ print $3 }')
if [[ -n ${IMAGE_ID} ]]; then 
docker rm -f ${IMAGE_ID};
fi

5. Login remote registry

# Default is dockerhub. username is docker hub id
docker login
# Login the specific registry
docker login registry.private.com

6. Build image from Dockerfile

docker build -t <user>/<repo>:<tag> -f Dockerfile .

7. Push the local image to remote registry

docker image push <user>/<repo>:<tag>
# Push to the private registry
docker push registry.private.com/<repo>:<tag>

8. Add and view the docker volume

# add volume
docker volume create <volume_name>

# view volume
docker volume ls
sudo ls /var/lib/docker/volumes

9. Check the volume detail

docker volume inspect <volume_name>

10. Test docker container service connection with curl

ping the http service

curl 127.0.0.1:9000

ping the https service

curl -k https://127.0.0.1:9443

11. Run up a container

docker container run <image_name>
# i.e. 
docker container run hello-world

# If we want to see the directory listing after running the container, we can do with
docker container run alpine ls -l

# If we want to run an interactive shell, we can do with
docker container run -it alpine /bin/sh

# list all the running container instances
docker container ls 

# list all the conatiner instances
docker container ls -a

12. Check which docker registries have been logged in

cat ~/.docker/config

13. Create TLS certificate and key

# Before using the commands, we must enable docker swarm  
docker swarm init
docker secret create domain.crt certs/domain.crt
docker secret create domain.key certs/domain.key

14. Remove image from docker registry

# Remove image from private registry
docker image remove registry.private.com/<repo>

15. Pull image from private registry

docker pull registry.private.com/<repo>

16. List docker image with sha256

docker images --digests

17. Remove the dangling images

Danging image are the ones with the <none> repository name

docker rmi $(docker images -f "dangling=true" -q)

18. Copy files from container to host

docker cp <containerId>:/file/path/within/container /host/path/target

19. Get container’s IP address

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' portainer_agent

Enjoyed this article? Support my work with a coffee ☕ on Ko-fi.
Buy Me a Coffee at ko-fi.com
DigitalOcean Referral Badge
Sign up to get $200, 60-day account credit !