DEV Community

Cover image for Docker Like a Pro: Essential Commands and Tips
Ramkumar M N
Ramkumar M N

Posted on

Docker Like a Pro: Essential Commands and Tips

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
Enter fullscreen mode Exit fullscreen mode

To get detailed system info, use:

docker info
Enter fullscreen mode Exit fullscreen mode

2. Run a Container Instantly

Quickly run a container with:

docker run -d -p 8080:80 --name my-container nginx
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To include stopped containers:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

4. Stop and Remove Containers Easily

Stop a running container:

docker stop my-container
Enter fullscreen mode Exit fullscreen mode

Remove a stopped container:

docker rm my-container
Enter fullscreen mode Exit fullscreen mode

Remove all stopped containers at once:

docker rm $(docker ps -aq)
Enter fullscreen mode Exit fullscreen mode

5. Remove Unused Images to Free Space

Over time, unused images take up space. Remove them with:

docker image prune -a
Enter fullscreen mode Exit fullscreen mode

6. Build and Tag an Image

If you have a Dockerfile, build an image with:

docker build -t my-app .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

For Ubuntu-based containers, use /bin/bash.

8. View Real-Time Container Logs

Monitor logs with:

docker logs -f my-container
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

Copy files from a container to your machine:

docker cp my-container:/app/myfile.txt .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Stop and remove all services:

docker-compose down
Enter fullscreen mode Exit fullscreen mode

πŸ“’ 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)

Collapse
 
cwrite profile image
Christopher Wright

Thanks for sharing these Docker tips. Are there any specific security practices you'd recommend for beginners?

Collapse
 
ramkumar-m-n profile image
Ramkumar M N

Hi Christopher Wright,
Thanks for the feedback. Yes, I will share asap