working with docker
here are my notes when working with docker
installing docker using apt on ubuntu
how to view running docker container
docker ps
provides a list of the Docker containers on your machine. You can use it to check whether a container is running, display container sizes, and find stopped containers that you need to restart or remove.
docker ps
you should see the list of running images below
root@ubuntu-s-1vcpu-1gb-amd-sfo3-01:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b074c8242a0b sportbook-backend-20250521 "fastapi run main.py…" 11 days ago Up 11 days 0.0.0.0:80->80/tcp, :::80->80/tcp, 8000/tcp trusting_mahavira
05571ff8142c sportbook-data-20250521 "python football_api…" 11 days ago Up 11 days quizzical_leakey
how to stop a docker container
To stop a running docker container
docker stop <container_name_or_id>
# example stopping using a container name
docker stop modest_ritchie
# docker stop using a container id
docker stop e1b364fc2ee3
how to start/run a docker container
docker run command in detached mode
docker run -d sportbook-data-20250521
docker run -d -p 80:80 sportbook-backend-20250521
how to view all docker images
docker images
you should see the list of docker images like this
root@ubuntu-s-1vcpu-1gb-amd-sfo3-01:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sportbook-backend-20250521 latest f7525557f05a 11 days ago 1.31GB
sportbook-data-20250521 latest 75c818f374bc 11 days ago 1.12GB
sportbook-backend-20250516 latest ef45ada96f4f 2 weeks ago 1.31GB
how to view log
how to view the last 100 lines of docker log
docker logs --tail 100 strange_black1
how to view logs with timestamps
docker logs --timestamps strange_black1
to follow logs in real time
docker logs -f strange_black1
to remove all docker logs
sudo find /var/lib/docker/containers/ -name '*-json.log' -exec truncate -s 0 {} \;
This command:
- Finds all log files in Docker’s container storage directory
- Truncates each to 0 bytes (clearing contents without deleting the file)
how to set up up log rotation in docker
step 1 - create and edit the daemon.json file
Since you’re using the Snap version of Docker, the correct location for daemon.json is: /var/snap/docker/current/config/daemon.json
/var/snap/docker/current/config/daemon.json
edit or create the log file
sudo nano /var/snap/docker/current/config/daemon.json
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
max-size
: rotates the log when it exceeds 10 megabytesmax-file
: keeps up to 3 rotated logs (30 MB total)
step 2 - restart docker (snap) version
sudo snap restart docker
to verify that docker is working
docker run --name logtest -d busybox sh -c "while true; do echo 'hello'; sleep 1; done"
Wait until the log rotates, or manually confirm the log file respects the max-size.
# clean up the test log
docker stop logtest && docker rm logtest
how to verify if docker is running?
sudo snap services docker
you should see sth like this
Service Startup Current
docker.daemon enabled active
# start docker
sudo snap start docker
check your docker socket
ls -l /var/run/docker.sock
# srw-rw---- 1 root docker 0 ... /var/run/docker.sock
restart docker
sudo snap restart docker
start and stop docker images
start the docker daemon
sudo snap start
you should see this after running this sudo snap start command
// you should now see this
// docker.dockerd enabled active -
delete and clean up
how to remove a docker image
docker rmi abc123456789