markdown Docker提示和技巧 - 有用的命令

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Docker提示和技巧 - 有用的命令相关的知识,希望对你有一定的参考价值。

## 1. General Stuff

### Terminology

An `image` is a set of layers. When you start an `image`, it becomes a running `container`. You can have many running `container`s of the same image.

An instance of an `image` is called a `container`. 

The `image` is the recipe, the `container` is the cake. You can make as many cakes as you like with a given recipe. A stopped `container` is a cake in the freezer.

A `volume` is a virtual drive which enables us to persist data and share between containers. 

### Start / build environment

docker-compose up 

### Start as background process

```
docker-compose up -d
```

The `-d` flag backgrounds the process and log output. 
To view logs for a specific container, use `docker-compose logs [container]`

### Stop vs Down

The `docker-compose stop` command will stop your containers, but it won’t remove them.

The `docker-compose down` command will stop your containers, but it also removes the stopped containers as well as any networks that were created.

You can take `down` 1 step further and add the -v flag to remove all volumes too. This is great for doing a full blown reset on your environment by running `docker-compose down -v`.


### To show and follow the log output of the container execute:

```
docker logs -ft container_name
```

### Copy files between host and container

https://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container

`mycontainer` is a container ID, not an image ID. Get the image ID with `docker ps`

The cp command can be used to copy files. One specific file can be copied into the container like:

```
docker cp foo.txt mycontainer:/foo.txt
docker cp mycontainer:/foo.txt foo.txt
```

Multiple files contained by the folder `src` can be copied into the `target` folder using:

```
docker cp src/. mycontainer:/target
docker cp mycontainer:/src/. target
```

You can also copy files out of a container:
```
docker cp divihub-test_phpfpm_1:/usr/local/etc/php-fpm.d/www.conf .
docker cp <containerId>:/file/path/within/container /host/path/target
```


## 2. Containers

### List Running Docker Containers

```
docker ps
```

### List All Docker Containers

```
docker ps -a
```

### SSH into a docker container

See: https://docs.docker.com/compose/reference/exec/

In this example `wp` is the service defined in the `docker-compose.yml`
`docker-compose exec <service> bash`

```bash
docker-compose exec <docker_name> bash
docker-compose exec wp bash
# or
docker exec -ti <docker_name> /bin/bash
docker exec -ti divi-card-factory-rsmm_wp_1 /bin/bash
```


### Kill all running docker containers:

```
docker kill $(docker ps -q)
```

### Delete containers of current docker-compose

`-s` flag also shuts down running containers if needed

```
docker-compose rm -s
```

### Delete a specific Docker container

```
docker rm <container_ID>
```

## 3. Images

### Delete the dangling docker images
The ones that are not tagged properly and are hanging around usually as the result of the intermediate container creation:

```
docker rmi $(docker images -q -f dangling=true)
```

### List docker images

```
docker images -a
```

### remove a specific docker image

```
docker rmi Image Image
```

## 4. Volumes

### Determine which volumes are used by a container

```
docker inspect --format="{{.Mounts}}" <container_id>
```

### Delete specific volumes

```
docker volume ls
docker volume rm <name_of_volume>
```

### Delete all volumes

```
docker volume prune
```

### Recreating the volumes

Recreating the volumes is as simple as restarting the application
```
docker-compose up --build
```

## 5. Cleanup & Reset

#### Purging All Unused or Dangling Images, Containers, Volumes, and Networks

```
docker system prune
```

To additionally remove any stopped containers and all unused images (not just dangling images), add the -a flag to the command:

```
docker system prune -a
```

以上是关于markdown Docker提示和技巧 - 有用的命令的主要内容,如果未能解决你的问题,请参考以下文章

markdown 一般:一些有用的mysql技巧

markdown Docker - 有用的Docker命令

markdown Laravel提示和技巧

markdown JS提示和技巧

markdown 确认提示和技巧

markdown GDB:提示和技巧