Docker CLI Cheat Sheet

![download.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/77d9db18-43b0-b200-c30f-394a10980ae8.png)


# 参考動画

https://youtu.be/kTp5xUtcalw


# Management
```terminal:terminal
    docker info                          Display system information
    docker version                       Display the system's version
    docker login                         Log into a Docker registry
```


# Container & Image
```terminal:terminal
    // List All Docker images
    docker images

    // List running container
    docker pc
```

# Running & Stopping
```terminal:terminal
    docker pull [imageName]              Pull an image from a registry
    docker run [imageName]               Run containers
    docker run -d [imageName]            Detached mode
    docker start [containerName]         Start stopped containers
    docker ps                            List running containers
    docker ps -a                         List running and stopped containers
    docker stop [containerName]          Stop containers
    docker kill [containerName]          Kill containers
    docker image inspect [imageName]     Get image info
```

# Running a container
```terminal:terminal
    docker run -p 8080:80 --name webserver nginx
    docker ps
    docker stop webserver
    docker rm webserver
```

# Attach Shell
```terminal:terminal
    docker run -it nginx bash
    docker exec -it [containerName] bash 
```

# Cleaning up
```terminal:terminal
    docker rm [containerName]       Removes stopped containers
    docker rm $(docker ps -a -q)    Removes all stopped containers
    docker images                   List images
    docker rmi [imageName]          Deletes the image
    docker system prune -a          Removes all images not in use by any containers
```

# Building
```terminal:terminal
    docker build -t [name:tag] .                 Builds an image using a Dockerfile located in the same folder
    docker build -t [name:tag] -f [fileName]     Builds an image using a Dockerfile located in a differnt folder
    docker tag [imageName] [name:tag]            Tag an existing image
```

# Dockerfile - static HTML site
```Dockerfile:Dockerfile
    FROM nginx:alpine
    COPY . /usr/share/nginx/html
```

```terminal:terminal
    docker build -t webserver-image:v1 . 
    docker run -d -p 8080:80 webserver-image:v1
    curl localhost:8080
```

# Dockerfile - Node site
```Dockerfile:Dockerfile
    FROM alpine                  　　　　　　　　　Base image
    RUN apk add -update nodejs nodejs-npm         Install Node and NPM using the package manager
    WORKDIR /src                                  
    COPY . /src                                   Copy the files from the build context
    RUN npm install                               Run a command
    EXPOSE 8080                                   Add metadata
    CMD ["node", "index.js"]　
```

# Volumes Cheat Sheet
```terminal:terminal
    docker create volume [volumeName]            Creates a new volume
    docker volume ls                             Lists the volumes
    docker volume inspect [volumeName]           Display the volume info
    docker volume rm [volumeName]                Deletes a volume
    docker vokume prune                          Deletes all volumes not mounted
```