WebTechKitchen; Your Web Technology Kitchen, contact us to create, or maintain your websites and other digital properties.

Useful docker commands

Submitted by barnettech on Tue, 08/06/2019 - 11:05

docker system prune —all

From Chat: " The error message you're encountering indicates that there's no space left on the device where you're trying to perform a lando rebuild. This situation is different from the devfs being full, as it pertains directly to the available disk space on your system's storage device, not the device file system. The error occurs during the process of rebuilding your Lando environment, specifically when Docker attempts to pull or build images. Docker images and containers can consume significant amounts of disk space, and if your system's storage is full, Docker cannot create or update images. Here are steps to resolve the "no space left on device" error: Check Disk Space: First, confirm that your disk is indeed full by checking the available space. You can use the df -h command in the terminal to see disk usage. Clean Up Docker Resources: Docker can accumulate unused images, containers, networks, and volumes over time. Running the following commands can help reclaim space:

docker system prune: This will remove all stopped containers, all networks not used by at least one container, all dangling images, and all build cache. It will ask for confirmation before proceeding.

docker volume prune: Removes all unused volumes. Ensure that no data you need is kept in unused volumes.

docker image prune: Removes unused images. You can also remove specific containers or images that you know are no longer needed with docker rm (for containers) and docker rmi (for images)."

 

When in doubt shut down docker and reboot, or try the following first:

It’s great to hear that rebooting and restarting Docker fixed the issue with ActiveRecord not being able to find your database. It sounds like Docker was caching an old IP address for your `db` service, which was corrected upon restart. Docker's networking can occasionally hold onto old configurations, especially IP addresses, which are dynamically assigned unless specifically configured otherwise.

Here are a few methods to refresh Docker's state and clear network-related caches without needing to reboot your entire machine:

### Method 1: Restart Docker Service

Instead of rebooting the whole system, you can simply restart the Docker service to clear the cache. This will force Docker to refresh all connections, remove temporary files, and reinitialize the network settings.

```bash
# For systems using systemd (most Linux distributions)
sudo systemctl restart docker

# For systems using the older SysVinit (some older Linux distributions)
sudo service docker restart
```

### Method 2: Re-create Docker Networks

If Docker's network settings are causing issues (like caching wrong IP addresses), you can recreate the Docker networks:

1. **Remove All Networks**:
   First, list all networks and then remove them. This action will drop all custom configurations and cached settings.
   ```bash
   docker network ls
   docker network rm $(docker network ls -q)
   ```

2. **Recreate Networks**:
   If you are using Docker Compose, it will automatically recreate necessary networks when you run it next:
   ```bash
   docker-compose up -d
   ```

   If not using Docker Compose, recreate networks manually as needed.

### Method 3: Remove and Recreate Containers

Sometimes, just refreshing the network isn’t enough; you might need to recreate the Docker containers:

1. **Stop and Remove All Containers**:
   Use the following commands to stop and remove all containers. This ensures that there are no lingering connections or cached configurations.
   ```bash
   docker-compose down

   # Alternatively, if you're not using Docker Compose:
   docker stop $(docker ps -a -q)
   docker rm $(docker ps -a -q)
   ```

2. **Restart Docker Daemon**:
   Sometimes, just stopping the containers isn't enough; you might want to restart the Docker daemon as well:
   ```bash
   sudo systemctl restart docker
   ```

3. **Recreate and Start Containers**:
   Now, you can recreate the containers:
   ```bash
   docker-compose up -d
   ```

### Method 4: Clear Docker Cache

Docker also maintains a build cache, which can sometimes cause issues. To clear the build cache:

```bash
docker system prune -a
```

This command will remove:
- All stopped containers
- All networks not used by at least one container
- All images without at least one container associated to them
- All build cache

**Warning**: This is a destructive action and will remove images, containers, and networks not in use.

### Conclusion

These methods will help you reset Docker's internal state and avoid the necessity for a full system reboot when you encounter networking issues like the one you described. They are particularly useful in development environments where Docker containers and networks are frequently modified.

Run any command inside of the container

docker-compose run web <command>