Essential Docker Commands for Developers
Tags: docker, developer, dockercompose
Published: 2024-06-04 05:52:00

[Docker](https://www.docker.com/) has revolutionized the way we build, ship, and run applications. 
In this article, we’ll explore the most useful Docker commands every developer should know.

## 1. Installation and Setup

Before diving into Docker commands, ensure Docker is installed on your machine. You can download Docker Desktop from the official [Docker website](https://www.docker.com/get-started).

## 2. Basic Docker Commands

### `docker run`

Run a Docker container from an image. If the image is not present locally, Docker will pull it from the Docker Hub.

```sh
docker run hello-world
```

### `docker pull`

Download an image from a Docker registry.

```sh
docker pull nginx
```

### `docker ps`

List running Docker containers.

```sh
docker ps
```

To include stopped containers, use:

```sh
docker ps -a
```

### `docker stop`

Stop a running container.

```sh
docker stop <container_id>
```

### `docker rm`

Remove a stopped container.

```sh
docker rm <container_id>
```

### `docker rmi`

Remove an image.

```sh
docker rmi <image_id>
```

## 3. Image Management

### `docker build`

Build an image from a Dockerfile.

```sh
docker build -t myapp .
```

### `docker images`

List all Docker images on your machine.

```sh
docker images
```

### `docker tag`

Tag an image for a specific repository.

```sh
docker tag myapp myrepo/myapp:latest
```

### `docker push`

Push an image to a Docker registry.

```sh
docker push myrepo/myapp:latest
```

## 4. Container Management

### `docker exec`

Run a command in a running container. This is useful for debugging and inspection.

```sh
docker exec -it <container_id> /bin/bash
```

### `docker logs`

View the logs of a container.

```sh
docker logs <container_id>
```

### `docker inspect`

Inspect the details of a container or image.

```sh
docker inspect <container_id>
```

### `docker-compose`

[Docker Compose](https://docs.docker.com/compose/) is a tool for defining and running multi-container Docker applications. Define your services in a `docker-compose.yml` file, and then use the following commands:

#### `docker-compose up`

Start and run your entire application defined in `docker-compose.yml`.

```sh
docker-compose up
```

#### `docker-compose down`

Stop and remove all containers defined in `docker-compose.yml`.

```sh
docker-compose down
```

## 5. Networking and Volumes

### `docker network ls`

List all Docker networks.

```sh
docker network ls
```

### `docker network create`

Create a new Docker network.

```sh
docker network create mynetwork
```

### `docker volume ls`

List all Docker volumes.

```sh
docker volume ls
```

### `docker volume create`

Create a new Docker volume.

```sh
docker volume create myvolume
```

### `docker run` with Networking and Volumes

Run a container with a specific network and volume.

```sh
docker run -d --name mycontainer --network mynetwork -v myvolume:/data myimage
```

## 6. Cleanup Commands

### `docker system prune`

Clean up unused Docker objects (containers, images, networks, and volumes).

```sh
docker system prune
```

### `docker volume prune`

Remove all unused volumes.

```sh
docker volume prune
```

### `docker network prune`

Remove all unused networks.

```sh
docker network prune
```