Best Practices and Cleaning Up
In this final step, we will cover some best practices for working with Docker images and clean up our environment.
Best Practices for Docker Images
Always use specific version tags rather than relying on the latest
tag. This ensures consistency in your deployments.
## Better approach
docker pull nginx:1.23.2
## Less predictable
docker pull nginx:latest
2. Keep Images Small
Smaller images are faster to download and use less storage. Consider using alpine-based images when possible:
## Let's see the size difference
docker pull nginx:alpine
docker images | grep nginx
You might see output like:
nginx alpine 2bc7edbc3cf2 2 weeks ago 40.7MB
nginx latest 605c77e624dd 2 weeks ago 142MB
The alpine version is significantly smaller!
3. Use Multi-Stage Builds
When creating images with a Dockerfile, use multi-stage builds to keep the final image small. Here's a simple example (you don't need to run this):
## Build stage
FROM golang:1.17 AS builder
WORKDIR /app
COPY . .
RUN go build -o app
## Final stage
FROM alpine:3.15
COPY --from=builder /app/app /app
CMD ["/app"]
4. Document Your Images
Always maintain documentation for your custom images, including:
- What the image is for
- How to use it
- What changes you've made
- Any configuration options
We created a simple documentation file in the previous step.
5. Scan for Vulnerabilities
Regularly scan your images for vulnerabilities:
## Example of scanning (Docker Desktop has this built-in)
## docker scan my-custom-nginx:v1
Inspecting Docker Images
Let's explore a useful command for inspecting Docker images:
docker inspect my-custom-nginx:v1
This command displays detailed information about the image, including its layers, configuration, and environment variables.
Viewing Image History
You can see the history of how an image was built:
docker history my-custom-nginx:v1
This shows each layer of the image and the commands that created them.
Cleaning Up
Now let's clean up our environment by removing the containers and images we created:
- First, stop and remove our custom container:
docker stop my-custom-container
docker rm my-custom-container
- Remove the images we created:
docker rmi my-custom-nginx:v1 my-custom-nginx:latest my-custom-nginx:stable
- Optionally, remove the Nginx image:
docker rmi nginx:latest nginx:alpine
- Check that all containers and images have been removed:
docker ps -a
docker images
Using Docker System Prune
Docker provides a convenient command to clean up unused resources:
docker system prune
This removes all stopped containers, unused networks, dangling images, and build cache. You'll be asked to confirm before proceeding.
For a more aggressive cleanup, you can use:
docker system prune -a
This additionally removes all unused images, not just dangling ones.
Conclusion
You have now learned how to:
- Pull and run Docker images
- Modify running containers
- Commit changes to create new custom images
- Tag images for organization and distribution
- Save images to files for manual transfer
- Document your custom images
- Clean up your Docker environment
These skills form the foundation of working with Docker images and will be valuable as you continue your journey with containerization.