A Quick Guide to Command Your Docker Containers
Tags: docker, devops
Published: 2023-11-18 09:44:32


## Basic Container Operations

### Run a Container
```bash
docker run -it ubuntu
```
*Starts a new interactive container using the Ubuntu image.*

### Build an Image
```bash
docker build -t my-custom-image:latest .
```
*Creates a custom Docker image named 'my-custom-image' from the current directory.*

### Pull an Image
```bash
docker pull nginx:latest
```
*Downloads the latest version of the Nginx image from the Docker Hub.*

### Push an Image
```bash
docker push my-custom-image:latest
```
*Uploads the custom image 'my-custom-image' to a Docker registry.*

### List Images
```bash
docker images
```
*Displays a list of locally available Docker images.*

## Container Lifecycle Management

### Stop a Container
```bash
docker stop my_container
```
*Gracefully stops the running container 'my_container'.*

### Start a Container
```bash
docker start my_container
```
*Resumes execution of the stopped container 'my_container'.*

### Restart a Container
```bash
docker restart my_container
```
*Stops and then starts the container 'my_container'.*

### Kill a Container
```bash
docker kill -s SIGTERM my_container
```
*Sends a termination signal to forcefully stop the running container 'my_container'.*

### Remove a Container
```bash
docker rm my_container
```
*Deletes the container 'my_container'.*

## Image Management

### Remove an Image
```bash
docker rmi my-custom-image:latest
```
*Deletes the image 'my-custom-image' with the latest tag.*

### Create Image from Changes
```bash
docker commit my_container my-custom-image:latest
```
*Creates a new image 'my-custom-image' from changes made in a running container 'my_container'.*

### Save Images to Archive
```bash
docker save -o my-images.tar.gz my-custom-image:latest
```
*Saves the image 'my-custom-image' with the latest tag to a tar archive.*

### Load Image from Archive
```bash
docker load -i my-images.tar.gz
```
*Loads an image from the tar archive 'my-images.tar.gz'.*

## Container Interaction

### Execute Command in Container
```bash
docker exec -it my_container bash
```
*Runs an interactive bash shell in the running container 'my_container'.*

### Fetch Container Logs
```bash
docker logs my_container
```
*Retrieves and displays the logs of the container 'my_container'.*

### Inspect Container or Image
```bash
docker inspect my_container
```
*Retrieves detailed information about the container 'my_container'.*

### Copy Files Between Container and Host
```bash
docker cp my_container:/path/in/container /path/on/host
```
*Copies files or folders between the container 'my_container' and the local filesystem.*

## System Cleanup and Maintenance

### Remove Unused Resources
```bash
docker system prune
```
*Cleans up stopped containers, dangling images, and unused networks and volumes.*

## Miscellaneous Commands

### List Port Mappings
```bash
docker port my_container
```
*Shows the port mappings of the container 'my_container'.*

### Display Running Processes
```bash
docker top my_container
```
*Lists the running processes inside the container 'my_container'.*

### Manage Volumes
```bash
docker volume ls
```
*Lists the Docker volumes on the host.*

### Show Docker Version
```bash
docker version
```
*Displays information about the installed Docker version.*

### Show Changes in Container Filesystem
```bash
docker diff my_container
```
*Shows the filesystem changes made in the container 'my_container'.*

### Export Container Filesystem
```bash
docker export my_container > my_container_exported.tar
```
*Exports the filesystem of the container 'my_container' to a tar archive.*

### Import Filesystem to Create Image
```bash
docker import my_container_exported.tar my-imported-image:latest
```
*Imports the filesystem from the tar archive 'my_container_exported.tar' to create a new image 'my-imported-image' with the latest tag.*

## Docker Network Commands

### List Docker Networks
```bash
docker network ls
```
*Displays a list of Docker networks.*

### Create a Docker Network
```bash
docker network create my_network
```
*Creates a new Docker network named 'my_network'.*

### Inspect Docker Network
```bash
docker network inspect my_network
```
*Retrieves detailed information about the Docker network 'my_network'.*

## Docker Registry Commands

### Docker Login
```bash
docker login
```
*Logs in to a Docker registry.*

### Docker Logout
```bash
docker logout
```
*Logs out from a Docker registry.*

## Docker Volume Commands

### Create a Docker Volume
```bash
docker volume create my_volume
```
*Creates a new Docker volume named 'my_volume'.*

### Inspect Docker Volume
```bash
docker volume inspect my_volume
```
*Retrieves detailed information about the Docker volume 'my_volume'.*

## Docker Compose Commands

### Docker Compose Up
```bash
docker-compose up
```
*Starts services defined in a `docker-compose.yml` file.*

### Docker Compose Down
```bash
docker-compose down
```
*Stops and removes containers, networks, and volumes defined in a `docker-compose.yml` file.*