wiki-files/docker-use.md

262 lines
4.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: docker的使用
description: docker的简单使用
published: true
date: 2021-08-02T07:26:50.035Z
tags: docker
editor: markdown
dateCreated: 2021-08-02T07:26:50.035Z
---
# 容器使用
## 拉取镜像
```bash
$ docker pull ubuntu
```
## 启动新容器
### 交互式启动
```bash
$ docker run -it 镜像 /bin/bash
```
参数说明:
- -i交互式操作
- -t终端
- /bin/bash在镜像后的是命令这里希望交互式Shell。
### 后台启动
```bash
$ docker run -itd --name 容器名称 镜像 /bin/bash
```
参数说明:
- -d默认不会进入容器
## 查看现有容器
```bash
$ docker ps -a
```
## 启动已经停止的容器
```bash
$ docker start 容器的ID或名称
```
## 停止容器
```bash
$ docker stop 容器ID或名称
```
## 重启容器
```bash
$ docker restart 容器ID或名称
```
## 进入容器
### docker attach
进入正在执行的命令行,退出后容器停止
```bash
$ docker attach 容器ID或名称
```
### docker exec
打开新的命令行,退出后容器不停止
```bash
docker exec -it 容器ID或名称 bash
```
## 导出容器
```bash
$ docker export 容器ID或名称 > 导出文件名.tar
```
## 导入容器
### 指定文件
```bash
$ cat 要导入的镜像.tar | docker import - 导入后的镜像名称:TAG
```
```bash
$ docker import - 导入后的镜像名称:TAG
```
### 通过URL导入
```bash
$ docker import http://example.com/exampleimage.tgz example/imagerepo
```
## 导出镜像
```bash
$ docker save 镜像ID或名称 > 导出名称.tar
```
## 导入镜像
```bash
$ docker load < 导入名称.tar
```
## 删除容器
```bash
$ docker rm -f 容器ID或名称
```
# mongo
## 运行容器
```bash
$ docker run -p 27017:27017 --name MongoDB -v /root/docker/mongo/db:/data/db -d mongo
```
# nginx
## 运行容器
```bash
$ docker run --name Nginx1 -p 9091:80 -v /root/docker/nginx/www1:/usr/share/nginx/html -v /root/docker/nginx/conf1:/etc/nginx -d nginx
```
# mysql
## 运行容器
```bash
$ docker run --name mysql57 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
```
```bash
$ docker run --name MySQL --network service -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/db/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
```
说明:
1. 将MySQL加入service网络
2. 指定了配置文件卷
3. 指定了数据库卷
4. 指定了ROOT用户密码
5. 指定了utf8mb4字符集如果用配置文件可省略
## 脱离my.cnf文件配置
```bash
$ docker run --name mysql57 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
```
# nexus3
## 创建目录并授权
```bash
$ mkdir /root/docker/nexus/nexus-data && chown -R 200 /root/docker/nexus/nexus-data
```
## 运行容器
```bash
$ docker run -p 9898:8081 --name Nexus -e INSTALL4J_ADD_VM_PARAMS="-Xms200m -Xmx500m -XX:MaxDirectMemorySize=500m" -v /root/docker/nexus/nexus-data:/nexus-data -d sonatype/nexus3:latest
```
# 卷与数据持久化
## 新卷
```bash
$ docker volume create 卷名
```
## 查看卷列表
```bash
$ docker volume ls
```
## 查看卷详情
```bash
$ docker volume inspect 卷名
```
## 删除所有未使用的卷
```bash
$ docker volume prune
```
## 删除一个或多个
```bash
$ docker volume rm 卷名...
```
## 挂载卷
```bash
$ docker run --name 容器名称 -d -p 8080:80 -v 卷名或者绝对路径:/usr/share/nginx/html/ nginx:v2
```
## 挂载配置文件
```bash
$ docker run --name 容器名称 -d -p 8080:80 -v 配置文件:/etc/nginx/nginx.conf nginx:v2
```
-v 可以有多个配置
挂载配置文件的前提是配置文件需提前存在
# 拷贝
## 拷贝文件到容器中
```bash
$ docker cp /root/nginx/conf/config/conf 容器名:/etc/nginx/conf
```
## 从容器中拷贝出文件
```bash
$ docker cp 容器名:/etc/nginx/conf/config.cnf /root/nginx/conf/
```
# 网络
## 查看网络
```bash
$ docker network ls
```
## 创建网络
```bash
$ docker network create 网络名称
```
## 容器加入网络
```bash
$ docker run --network 网络名称 --network-alias 容器在网络中的别名(用于通信时直接使用)
```