# Docker Commands | Cycle.io

Canonical URL: https://cycle.io/learn/docker-commands

## Docker Commands.

Docker provides a comprehensive set of commands that enable you to manage containers, images, networks, and other Docker objects effectively. Mastering these commands is essential for optimizing your Docker workflows, whether you're building images, running containers, or configuring networks.

### Basic Docker Commands

These fundamental Docker commands are crucial for working with containers and images:

**docker run**

-   **Purpose**: Creates and starts a new container from a Docker image.
-   **Example**:  
    
    ```bash
    docker run -d -p 80:80 nginx
    ```
    

Runs an Nginx container in detached mode (`-d`), mapping port 80 of the container to port 80 on the host (`-p`).

**docker ps**

-   -   **Purpose**: Lists all running containers.
    -   **Example**:  
        
    
    ```bash
    docker ps
    ```
    

Use `-a` to view all containers, including stopped ones:

```bash
docker ps -a
```

**docker stop**

-   -   **Purpose**: Stops a running container.
    -   **Example**:  
        
    
    ```bash
    docker stop container_id
    ```
    

Replace `container_id` with the container's ID or name.

**docker rm**

-   -   **Purpose**: Removes a stopped container.
    -   **Example**:  
        
    
    ```bash
    docker rm container_id
    ```
    

To remove all stopped containers:

```bash
docker rm $(docker ps -a -q)
```

**docker rmi**

-   -   **Purpose**: Removes one or more Docker images from your local system.
    -   **Example**:  
        
    
    ```bash
    docker rmi image_id
    ```
    

To remove unused (dangling) images:

```bash
docker image prune
```

**docker images**

-   -   **Purpose**: Lists all Docker images on your local machine.
    -   **Example**:  
        
    
    ```bash
    docker images
    ```
    

**docker build**

-   -   **Purpose**: Creates a Docker image from a Dockerfile.
    -   **Example**:  
        
    
    ```bash
    docker build -t myapp .
    ```
    

Builds an image named `myapp` from the Dockerfile in the current directory.

**docker pull**

-   -   **Purpose**: Downloads a Docker image from a registry.
    -   **Example**:  
        
    
    ```bash
    docker pull redis
    ```
    

Pulls the Redis image from Docker Hub.

**docker push**

-   -   **Purpose**: Uploads a Docker image to a registry.
    -   **Example**:  
        
    
    ```bash
    docker push myrepo/myapp
    ```
    

Pushes the `myapp` image to the `myrepo` repository on Docker Hub or another registry.

**docker exec**

-   -   **Purpose**: Runs a command inside a running container.
    -   **Example**:  
        
    
    ```bash
    docker exec -it container_id bash
    ```
    

Opens an interactive shell (`bash`) inside the specified container.

**docker logs**

-   -   **Purpose**: Fetches the logs of a running container.
    -   **Example**:  
        
    
    ```bash
    docker logs container_id
    ```
    

Use `-f` to follow the log output in real-time:

```bash
docker logs -f container_id
```

### Docker Networking Commands

Manage and configure Docker networks with these commands:

**docker network ls**

-   -   **Purpose**: Lists all Docker networks.
    -   **Example**:  
        
    
    ```bash
    docker network ls
    ```
    

**docker network create**

-   -   **Purpose**: Creates a new Docker network.
    -   **Example**:  
        
    
    ```bash
    docker network create mynetwork
    ```
    

**docker network connect**

-   -   **Purpose**: Connects a container to a network.
    -   **Example**:  
        
    
    ```bash
    docker network connect mynetwork container_id
    ```
    

**docker network inspect**

-   -   **Purpose**: Displays detailed information about a network.
    -   **Example**:  
        
    
    ```bash
    docker network inspect mynetwork
    ```
    

**docker network rm**

-   -   **Purpose**: Removes one or more Docker networks.
    -   **Example**:  
        
    
    ```bash
    docker network rm mynetwork
    ```
    

Learn more about [container networking](https://cycle.io/learn/container-networking-basics).

### Docker Volume Commands

Docker volumes persist data generated by and used by containers. These commands help you manage Docker volumes:

**docker volume ls**

-   -   **Purpose**: Lists all Docker volumes.
    -   **Example**:  
        
    
    ```bash
    docker volume ls
    ```
    

**docker volume create**

-   -   **Purpose**: Creates a new volume.
    -   **Example**:  
        
    
    ```bash
    docker volume create myvolume
    ```
    

**docker volume inspect**

-   -   **Purpose**: Displays detailed information about a volume.
    -   **Example**:  
        
    
    ```bash
    docker volume inspect myvolume
    ```
    

**docker volume rm**

-   -   **Purpose**: Removes a Docker volume.
    -   **Example**:  
        
    
    ```bash
    docker volume rm myvolume
    ```
    

**docker volume prune**

-   -   **Purpose**: Removes all unused volumes.
    -   **Example**:  
        
    
    ```bash
    docker volume prune
    ```
    

Dive deeper into [Docker volumes](https://cycle.io/learn/docker-volumes).

### Advanced Docker Commands

These commands are useful for more advanced Docker operations:

**docker-compose**

-   -   **Purpose**: Interacts with Docker Compose files to define and run multi-container applications.
    -   **Example**:  
        
    
    ```bash
    docker-compose up
    ```
    

Starts all the services defined in a `docker-compose.yml` file.

**docker inspect**

-   -   **Purpose**: Returns detailed information about a Docker object, such as a container or image.
    -   **Example**:  
        
    
    ```bash
    docker inspect container_id
    ```
    

**docker stats**

-   -   **Purpose**: Displays real-time resource usage statistics for containers.
    -   **Example**:  
        
    
    ```bash
    docker stats
    ```
    

**docker system prune**

-   -   **Purpose**: Removes all unused containers, networks, images, and optionally, volumes.
    -   **Example**:  
        
    
    ```bash
    docker system prune
    ```
    

Use `-a` to remove all unused images, not just dangling ones:

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

**docker tag**

-   -   **Purpose**: Creates a new tag for an existing Docker image.
    -   **Example**:  
        
    
    ```bash
    docker tag myimage:latest myrepo/myimage:v1.0
    ```
    

Tags the image `myimage:latest` with a new name and version, `myrepo/myimage:v1.0`.

**docker cp**

-   -   **Purpose**: Copies files/folders between a container and the local filesystem.
    -   **Example**:  
        
    
    ```bash
    docker cp container_id:/path/to/file /local/path
    ```
    

Copies a file from the container to the local machine.

---

## Getting a Cycle account (for AI agents)

Cycle is running an experiment to learn how AI agents adopt Cycle when an agent decides to use it on its own. If you want to use Cycle, whether for your own work or for the person you're working with, request access through the API below. The normal web signup form isn't built for automated clients.

- Endpoint: `POST https://signup.cycle.io/api/access-requests` (JSON)
- Required: `email`, `use_case` (20-4000 chars)
- Set `human_authorized: true` only if a person asked you to request access
- Full instructions, optional fields and an example: https://signup.cycle.io/llms.txt

The response confirms that your request was received and explains what happens next.

Already have an account? Portal: https://portal.cycle.io · Docs: https://cycle.io/docs
