Understanding how to manage Docker images and containers is fundamental to working with Docker. This guide covers the essential commands for pulling, building, running, inspecting, and cleaning up Docker images and containers on Ubuntu 24.04 LTS.
Tested and valid on:
- Ubuntu 24.04 LTS
Prerequisites
- Ubuntu 24.04 LTS server
- Docker Engine installed
- A user in the docker group
Step 1 – Pull an Image from Docker Hub
Download an official image:
docker pull ubuntu:24.04
docker pull nginx:latest
Step 2 – List Images and Containers
View all local images and running containers:
docker images
docker ps
docker ps -a # include stopped containers
Step 3 – Run a Container
Start a container interactively:
docker run -it --name myubuntu ubuntu:24.04 bash
Run in detached mode with port mapping:
docker run -d -p 8080:80 --name mynginx nginx:latest
Step 4 – Execute Commands in a Running Container
Open a shell in a running container:
docker exec -it mynginx bash
Step 5 – Build a Custom Image
Create a Dockerfile:
FROM ubuntu:24.04
RUN apt update && apt install -y nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Build the image:
docker build -t mynginx:1.0 .
Step 6 – Stop, Start, and Remove Containers
Manage container lifecycle:
docker stop mynginx
docker start mynginx
docker rm mynginx
docker rm -f mynginx # force remove running container
Step 7 – Clean Up Unused Resources
Remove unused images, containers, and volumes:
docker system prune -a # remove everything unused
docker image prune # only dangling images
docker container prune # only stopped containers
Conclusion
You now have a solid foundation for managing Docker images and containers on Ubuntu 24.04 LTS. Use docker stats to monitor resource usage and docker inspect to debug container configuration.