mauvehed's dotfiles for personal and work environments
1# Docker Usage
2
3This document provides a basic guide to using Docker, including common commands and concepts relevant to a development workflow.
4
5## Overview
6
7[Docker](https://www.docker.com/) is a platform for developing, shipping, and running applications in containers. Containers allow you to package an application with all of its dependencies into a standardized unit for software development.
8
9## Installation
10
11* **Docker Desktop**: For macOS and Windows, [Docker Desktop](https://www.docker.com/products/docker-desktop/) is the easiest way to install Docker. It includes the Docker Engine, Docker CLI client, Docker Compose, Kubernetes, and more.
12* **Linux**: On Linux, you can install the Docker Engine and CLI separately. Follow the official installation guides for your distribution (e.g., [Install Docker Engine on Ubuntu](https://docs.docker.com/engine/install/ubuntu/)).
13
14Installation might be managed by `chezmoi` if Docker is part of your standard toolset defined in your dotfiles.
15
16## Common Docker CLI Commands
17
18Here are some of the most frequently used Docker commands:
19
20* **Pull an image from a registry (e.g., Docker Hub)**:
21 ```sh
22 docker pull <image_name>:<tag>
23 # Example: docker pull ubuntu:latest
24 ```
25
26* **Run a container from an image**:
27 ```sh
28 docker run [options] <image_name>:<tag> [command]
29 # Example (interactive shell in an Ubuntu container):
30 # docker run -it ubuntu:latest /bin/bash
31 # Example (run a web server, map port 8080 to container's port 80):
32 # docker run -d -p 8080:80 nginx:latest
33 ```
34 * `-d`: Detached mode (run in background)
35 * `-it`: Interactive TTY
36 * `-p <host_port>:<container_port>`: Port mapping
37 * `--name <container_name>`: Assign a name to the container
38 * `-v <host_path>:<container_path>`: Mount a volume
39
40* **List running containers**:
41 ```sh
42 docker ps
43 ```
44
45* **List all containers (running and stopped)**:
46 ```sh
47 docker ps -a
48 ```
49
50* **View logs of a container**:
51 ```sh
52 docker logs <container_name_or_id>
53 # Follow logs: docker logs -f <container_name_or_id>
54 ```
55
56* **Stop a running container**:
57 ```sh
58 docker stop <container_name_or_id>
59 ```
60
61* **Remove a stopped container**:
62 ```sh
63 docker rm <container_name_or_id>
64 ```
65
66* **List images**:
67 ```sh
68 docker images
69 ```
70
71* **Remove an image**:
72 ```sh
73 docker rmi <image_name_or_id>
74 ```
75
76* **Clean up unused resources (containers, networks, images)**:
77 ```sh
78 docker system prune
79 # To remove all unused images, not just dangling ones:
80 # docker system prune -a
81 ```
82
83## Docker Compose
84
85[Docker Compose](https://docs.docker.com/compose/) is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file (`docker-compose.yml`) to configure your application's services.
86
87### Example `docker-compose.yml` snippet:
88```yaml
89version: '3.8'
90services:
91 web:
92 image: nginx:latest
93 ports:
94 - "8080:80"
95 app:
96 image: my-custom-app
97 build: .
98 ports:
99 - "3000:3000"
100```
101
102### Common Docker Compose Commands:
103
104* **Start services defined in `docker-compose.yml`**:
105 ```sh
106 docker-compose up
107 # Start in detached mode: docker-compose up -d
108 ```
109
110* **Stop services**:
111 ```sh
112 docker-compose down
113 ```
114
115* **List services**:
116 ```sh
117 docker-compose ps
118 ```
119
120## Development Containers (Dev Containers)
121
122[Development Containers](https://containers.dev/) (Dev Containers) allow you to use a Docker container as a full-featured development environment. This is particularly useful with editors like VS Code.
123
124A `devcontainer.json` file in your project tells VS Code (and other tools) how to access (or create) a development container with a well-defined tool and runtime stack.
125
126### Key Benefits:
127* **Consistent Environment**: Ensures everyone on the team uses the same development environment.
128* **Tool Isolation**: Keeps tools and dependencies for different projects separate.
129* **Pre-configured Setups**: Quickly onboard new developers with ready-to-use environments.
130
131Refer to the [official Dev Containers documentation](https://containers.dev/docs) and the [VS Code Remote - Containers documentation](https://code.visualstudio.com/docs/remote/containers) for setup and usage details.
132
133## Resources
134
135* **Official Docker Documentation**: [https://docs.docker.com/](https://docs.docker.com/)
136* **Docker Hub (Image Registry)**: [https://hub.docker.com/](https://hub.docker.com/)
137* **Docker Compose Documentation**: [https://docs.docker.com/compose/](https://docs.docker.com/compose/)
138* **Development Containers**: [https://containers.dev/](https://containers.dev/)
139* **VS Code Remote - Containers**: [https://code.visualstudio.com/docs/devcontainers/containers](https://code.visualstudio.com/docs/devcontainers/containers) (Note: VS Code documentation now prefers /devcontainers/ over /remote/)
140
141This guide provides a starting point. Docker is a vast ecosystem, so refer to the official documentation for in-depth information.