Essential Docker commands every developer should know
Tags: webdev, react, docker, devops
Published: 2024-06-08 15:25:07

Docker has revolutionized the way developers build, ship, and run applications. Whether you're just starting out or looking to brush up on your skills, understanding key Docker commands can greatly enhance your workflow. In this blog post, we'll cover some of the most useful Docker commands and explain how to use them effectively.

## Docker Basics

**1. docker --version**

**Purpose:** Check your Docker version.

```
docker --version
```

This command helps verify that Docker is installed and provides information about the installed version.

**2. docker info**

**Purpose:** Display system-wide information about Docker.

```
docker info
```

Use this command to get detailed information about your Docker installation, including the number of containers, images, and system resources.

## Working with Containers

**3. docker run**

**Purpose:** Create and start a new container.

```
docker run -d -p 80:80 --name my_container nginx
```

This command starts a new container in detached mode (-d), maps port 80 of the host to port 80 of the container (-p 80:80), and names the container my_container.

**4. docker ps**

**Purpose:** List running containers.

```
docker ps
```

This command displays a list of all currently running containers, showing details like container ID, image, and ports.

**5. docker stop**

**Purpose:** Stop a running container.

```
docker stop my_container
```

This command stops the container named my_container. Use the container ID if the name is not specified.

**6. docker rm**

**Purpose:** Remove a stopped container.

```
docker rm my_container
```

After stopping a container, you can remove it using this command. This helps free up system resources.

**7. docker exec**

**Purpose:** Run a command inside a running container.

```
docker exec -it my_container /bin/bash
```

This command opens an interactive terminal session inside the container my_container. It’s useful for debugging and inspecting running containers.

## Managing Images

**8. docker images**

**Purpose:** List all Docker images on the host.

```
docker images
```

This command displays a list of all images stored on your local Docker host, including repository names, tags, and sizes.

**9. docker pull**

**Purpose:** Download an image from a Docker registry.

```
docker pull node
```

This command fetches the latest node image from the Docker Hub, the default registry.

**10. docker build**

**Purpose:** Build an image from a Dockerfile.

```
docker build -t my_image:latest
```

This command builds a new Docker image from a Dockerfile in the current directory and tags it as my_image:latest.

**11. docker rmi**

**Purpose:** Remove an image.

```
docker rmi my_image:latest
```
This command deletes the specified image from your local Docker host.

## Networks and Volumes

**12. docker network ls**

**Purpose:** List all Docker networks.

```
docker network ls
```

This command shows all networks available on your Docker host, including bridge, host, and overlay networks.

**13. docker network create**

**Purpose:** Create a new Docker network.

```
docker network create my_network
```

This command creates a new network named my_network.

**14. docker volume ls**

**Purpose:** List all Docker volumes.

```
docker volume ls
```

This command displays all volumes created on your Docker host, which are used for persistent data storage.

**15. docker volume create**

**Purpose:** Create a new Docker volume.

```
docker volume create my_volume
```

This command creates a new volume named my_volume.

Cleaning Up

**16. docker system prune**

**Purpose:** Clean up unused Docker objects.

```
docker system prune
```
This command removes all stopped containers, unused networks, dangling images, and build caches to free up space.

**Conclusion**

Mastering these Docker commands will help streamline your development workflow and make managing containers more efficient. Whether you're building applications, testing new environments, or deploying services, these commands provide a solid foundation for working with Docker.

Happy Dockering!