docker : basic commands
Published: 2020-07-12 16:55:47

# Docker info

### check the version or if docker is already installed

```bash
$ docker --verison
```

### show system information regarding docker installation

```bash
$ docker info
```

### show stats of runnign container

```bash
$ docker stats
```

### show container log

```bash
$ docker logs <CONTAINER_ID>
```

### show processes of container

```bash
$ docker top <CONTAINER_ID>
```

### show detailed info of container

```bash
$ docker inspect <CONTAINER_ID>
```

### show modified files in a container

```bash
$ docker diff <CONTAINER_ID>
```

### show mapped ports in a container

```bash
$ docker port <CONTAINER_ID>
```

---

# Help / manual

### list available commands

```bash
$ docker
```

### manual for specific command

```bash
$ docker <COMMAND> --help
```

---

# Run a new container

### create a new container

```bash
// create from latest version
$ docker create <IMAGE>

// create from specific version
$ docker create <IMAGE[:TAG]>
```

### start a container

```bash
$ docker start <CONTAINER_ID>
```

### run (create and start) a new container

```bash
// create from latest version
$ docker run <IMAGE>

// create from specific version
$ docker run <IMAGE[:TAG]>
```

### run in the background

```bash
$ docker run -d <IMAGE>
```

### run and assign it a name

```bash
$ docker run --name <CONTAINER_NAME> <IMAGE>
```

### run and map a port

```bash
$ docker run -p <HOST_PORT>:<CONTAINER_PORT> <IMAGE>
```

### run and map all port

```bash
$ docker run -P <IMAGE>
```

### run and assign a hostname

```bash
$ docker run --hostname <HOSTNAME> <IMAGE>
```

### run and add a dns entry

```bash
$ docker run --add-host <HOSTNAME>:<IP> <IMAGE>
```

### run and map a local dir

```bash
$ docker run -v <HOST_DIR>:<TARGET_DIR> <IMAGE>
```

### run and change the entry point

```bash
$ docker run -it --executable <COMMAND> <IMAGE>
```

---

# Manage Container

### show list of all running container

```bash
$ docker ps
```

### show list of all container

```bash
$ docker ps -a
```

### stop a running container

```bash
$ docker stop <CONTAINER_ID>
```

### kill a running container

```bash
$ docker kill <CONTAINER_ID>
```

### start a stopped container

```bash
$ docker start <CONTAINER_ID>
```

### restart a container

```bash
$ docker restart <CONTAINER_ID>
```

### suspend a container

```bash
$ docker pause <CONTAINER_ID>
```

### resume a container

```bash
$ docker unpause <CONTAINER_ID>
```

### rename a container

```bash
$ docker rename <OLD_NAME> <NEW_NAME>
```

### start shell inside a running container

```bash
$ docker exce -it <CONTAINER_ID> <COMMAND>
```

### copy a file from host to container

```bash
$ docker cp <SOURCE> <CONTAINER_ID>:<DESTINATION>
```

### copy a file from container to host

```bash
$ docker cp <CONTAINER_ID>:<SOURCE> <DESTINATION>
```

### delete a container

```bash
$ docker rm <CONTAINER_ID>
```

### destroy (kill + rm) a running container

```bash
$ docker rm -f <CONTAINER_ID>
```

### delete all stopped containers

```bash
$ docker container prune
```

### create an image out of container

```bash
$ docker commit <CONTAINER_ID>
```

---

# Manage Images

### download an image

```bash
$ docker pull <IMAGE[:TAG]>
```

### upload an image to repository

```bash
$ docker push <IMAGE[:TAG]>
```

### show list of images

```bash
$ docker images
```

### build an image

```bash
$ docker build <DIRECTORY>
```

### build and tag the image

```bash
$ docker build -t <IMAGE[:TAG]> <DIRECTORY>
```

### tag an image

```bash
$ docker tag <SOURCE_IMAGE[:TAG]> <TARGET_IMAGE[:TAG]>
```

### delete an image

```bash
$ docker rmi <IMAGE>
```

### delete dangling images

```bash
$ docker image prune
```

### delete all unused images

```bash
$ docker image prune -a
```

### save an image to tar file

```bash
$ docker save <IMAGE> > <FILE.tar>
```

### load an image from tar file

```bash
$ docker load -i <FILE.tar>
```