Update Docker Docs
This commit is contained in:
154
Docker/Docker.md
154
Docker/Docker.md
@@ -1,120 +1,64 @@
|
|||||||
## Docker Cheat Sheet
|
# Docker Commands and Concepts
|
||||||
|
|
||||||
### Docker Data Directory
|
## Docker Concepts
|
||||||
- **State Less:** Don't save your data like Nginx.
|
|
||||||
- **State Full:** Save your data.
|
|
||||||
- `/var/lib/docker/` ==> Docker data directory
|
|
||||||
|
|
||||||
### Docker Commands
|
- **Stateless**: Do not save your data (like Nginx).
|
||||||
|
- **Stateful**: Save your data.
|
||||||
|
|
||||||
#### Login to Docker Hub
|
## Docker Data Directory
|
||||||
```bash
|
|
||||||
docker login
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Pull Docker Image
|
- `/var/lib/docker/`: Docker data directory.
|
||||||
```bash
|
- `/var/lib/docker/containers/`: Container configuration and file directory.
|
||||||
docker pull <repo-addr>
|
- `/var/lib/docker/volumes`: Directory where docker volumes are saved.
|
||||||
```
|
|
||||||
|
|
||||||
#### Show Pulled Images
|
## Docker CLI Commands
|
||||||
```bash
|
|
||||||
docker images
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Run Docker Container
|
### Authentication
|
||||||
```bash
|
|
||||||
docker run <options> <image>
|
|
||||||
```
|
|
||||||
- **Options**:
|
|
||||||
- `-it`: Run interactively and allocate a pseudo-TTY.
|
|
||||||
- `-dit`: Run in detached mode with interactive TTY.
|
|
||||||
|
|
||||||
#### Go to Container Shell
|
- `docker login`: Login to Docker Hub with CLI.
|
||||||
```bash
|
|
||||||
docker exec -it <container name> bash
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Remove Docker Container
|
### Image Management
|
||||||
```bash
|
|
||||||
docker rm <container name or ID>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Stop Docker Container
|
- `docker pull <repo-addr>`: Pull a Docker image.
|
||||||
```bash
|
- `docker images`: Show pulled images.
|
||||||
docker stop <container name or ID>
|
- `docker rmi -f <image-id>`: Remove an image.
|
||||||
```
|
- `docker save -o <file-location-and-name> <image-name>`: Save image as an external file.
|
||||||
|
- `docker load -i <file-location>`: Import a Docker image.
|
||||||
|
|
||||||
#### Stop and Remove Docker Container
|
### Container Management
|
||||||
```bash
|
|
||||||
docker rm -f <container name or ID>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### List Docker Containers
|
- `docker run <options> <img-name>`: Run a Docker container.
|
||||||
```bash
|
- `docker run`: Run Docker (after run exited).
|
||||||
docker ps -aq
|
- `docker run -it`: Run and give bash to me.
|
||||||
```
|
- `docker run -dit`: Run and give bash to me and run in the background.
|
||||||
|
- `docker exec -it <container-name>`: Go to container shell.
|
||||||
|
- `docker rm <container-name>`: Remove Docker container.
|
||||||
|
- `docker stop <container-name>`: Stop Docker container.
|
||||||
|
- `docker rm -f <container-name>`: Stop and remove Docker container.
|
||||||
|
- `docker ps -aq`: Give all Docker container IDs.
|
||||||
|
- `docker ps -aq -f status=exited`: Give all Docker container IDs with exited status.
|
||||||
|
- `docker container prune`: Remove all stopped containers.
|
||||||
|
- `docker commit <container-name> <new-name>`: Make a custom Docker image.
|
||||||
|
- `docker inspect <container-name>`: Get all data about container information.
|
||||||
|
- `docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-name>`: Get IP of the container.
|
||||||
|
- `docker cp <file_on_local> <container-name>:/<location>`: Copy from local to container.
|
||||||
|
- `docker cp <container-name>:/<location> <local-location>`: Copy from container to local.
|
||||||
|
- `docker stats`: Monitor Docker stats.
|
||||||
|
- `docker run -dit --name server --restart=always ubuntu`: Run container again after restarting the service or main server.
|
||||||
|
- `docker build -t <appname>:<appver> <location-of-docker-file>`: Build Docker image from a Dockerfile.
|
||||||
|
|
||||||
#### List Exited Docker Containers
|
### Volume Management
|
||||||
```bash
|
|
||||||
docker ps -aq -f status=exited
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Remove Exited Docker Containers
|
- `docker volume ls`: List all volumes.
|
||||||
```bash
|
- `docker volume create <name-of-volume>`: Create a volume for Docker.
|
||||||
docker rm $(docker ps -aq -f status=exited)
|
- `docker volume inspect <vol-name>`: Give information about the volume.
|
||||||
```
|
- `docker run -dit --name <container-name> -v <volume-name>:<location-in-container> <img-name>`: Run Docker image and save target location data in volume.
|
||||||
|
|
||||||
#### Remove All Stopped Containers
|
### Network Management
|
||||||
```bash
|
|
||||||
docker container prune
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Container Configuration and Files
|
- `docker network ls`: List all Docker networks.
|
||||||
- `/var/lib/docker/containers/` ==> Container config and file directory
|
- `docker network create <network-name>`: Create a Docker network.
|
||||||
|
- `docker network create --subnet <ip>/<subnet> --gateway <gateway-ip> --driver=<network-type> <net-name>`: Create network with custom settings.
|
||||||
#### Get Container Information
|
- `docker run -dit --name <container-name> --network <network-name> <img-name>`: Run a container and connect it to a custom network.
|
||||||
```bash
|
- `docker network connect <network-name> <container-name>`: Connect a container to a custom network.
|
||||||
docker inspect <container name or ID>
|
- `docker network disconnect <network-name> <container-name>`: Disconnect a network from a container.
|
||||||
```
|
|
||||||
|
|
||||||
#### Get Container IP Address
|
|
||||||
```bash
|
|
||||||
docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container name>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Copy Files Between Local and Container
|
|
||||||
```bash
|
|
||||||
docker cp <file_on_local> <container-name>:/<location>
|
|
||||||
docker cp <container-name>:/<location> <local_location>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Monitor Docker Container Statistics
|
|
||||||
```bash
|
|
||||||
docker stats
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Run Container with Restart Policy
|
|
||||||
```bash
|
|
||||||
docker run -dit --name server --restart=always ubuntu
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Create Custom Docker Image
|
|
||||||
```bash
|
|
||||||
docker commit <containername> <newname>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Save Docker Image to External File
|
|
||||||
```bash
|
|
||||||
docker save -o <file-location-and-name> <image-name>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Remove Docker Image
|
|
||||||
```bash
|
|
||||||
docker rmi -f <image-id>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Load Docker Image from File
|
|
||||||
```bash
|
|
||||||
docker load -i <file-location>
|
|
||||||
```
|
|
||||||
|
|||||||
Reference in New Issue
Block a user