Docker has revolutionized the way developers build, ship, and run applications. Whether you're a beginner or an experienced developer, mastering these essential Docker commands and tips will make you more efficient and productive.
1. Check Your Docker Version
Before using Docker, ensure you have it installed by running:
docker --version
To get detailed system info, use:
docker info
2. Run a Container Instantly
Quickly run a container with:
docker run -d -p 8080:80 --name my-container nginx
This runs an Nginx server in detached mode (-d
), mapping port 8080 on your machine to port 80 inside the container.
3. List Running and Stopped Containers
To see all running containers:
docker ps
To include stopped containers:
docker ps -a
4. Stop and Remove Containers Easily
Stop a running container:
docker stop my-container
Remove a stopped container:
docker rm my-container
Remove all stopped containers at once:
docker rm $(docker ps -aq)
5. Remove Unused Images to Free Space
Over time, unused images take up space. Remove them with:
docker image prune -a
6. Build and Tag an Image
If you have a Dockerfile
, build an image with:
docker build -t my-app .
This tags the image as my-app
.
7. Run a Container in Interactive Mode
Use interactive mode to execute commands inside a running container:
docker exec -it my-container /bin/sh
For Ubuntu-based containers, use /bin/bash
.
8. View Real-Time Container Logs
Monitor logs with:
docker logs -f my-container
The -f
flag shows real-time updates.
9. Copy Files Between Host and Container
Copy files from your local machine to a running container:
docker cp myfile.txt my-container:/app/
Copy files from a container to your machine:
docker cp my-container:/app/myfile.txt .
10. Use Docker Compose for Multi-Container Apps
Instead of running multiple containers manually, define them in docker-compose.yml
and start them with:
docker-compose up -d
Stop and remove all services:
docker-compose down
π’ Letβs Connect!
πΌ LinkedIn | π GitHub | βοΈ Dev.to | π Hashnode
π‘ Join the Conversation:
- Found this useful? Like π, comment π¬
- Share π to help others on their journey
- Have ideas? Share them below!
- Bookmark π this content for easy access later
Letβs collaborate and create something amazing! π
Top comments (2)
Thanks for sharing these Docker tips. Are there any specific security practices you'd recommend for beginners?
Hi Christopher Wright,
Thanks for the feedback. Yes, I will share asap