docker 常用命令

属性

  • 镜像 => 静态
    • name
    • tag
    • id
  • 容器:container => 运行中 => 程序
  • 存储卷:volume => 硬盘
  • 端口 : port

常用命令

# 查看镜像
#     
# name = > <仓库地址>//
# harbor.aigauss.com/tranlsate/liteboots
docker images

# 重命名镜像
# docker tag   
docker tag harbor.aigauss.com/tranlsate/liteboots:v0.1.4-new  test:v1

# 删除镜像
# docker rmi  
docker rmi  test:v1

# 查找镜像
docker search centos

# 下载镜像
#  默认下载 latest ,但 latest 不一定是最后的版本,具体看开发者定义 
docker pull centos

# 查看镜像所有tag
# 一般情况是登录 https://hub.docker.com](https://hub.docker.com/ 查看
# 如果需要使用脚本查看,可以参考: https://www.jianshu.com/p/b4ffe1cb4f20

# 保存镜像
# docker save  -o 
docker save centos:latest -o centos.tar

# 加载镜像文件
# docker load -i 
docker load -i centos.tar
# 启动参数
# -it: 前台启动
# -d: 后台启动
# -p: 端口映射
# -e: 配置环境参数
# -v: 配置存储卷
# --name: 设置容器名称
# --restarted: 这只重启模式, restarted=always  可以开机自启
#  --rm: 当进程挂掉时,删除容器
# 前台启动容器
docker run -it centos bash
# 后台启动容器
docker run -d centos bash

# 查看当前容器
docker ps 
# 查看所有容器
docker ps -a 

# 删除容器
doker rm 
# 强制删除容器
doker rm  -f 

# 进入容器<在容器里面执行一个命令>
doker exec  -it  

# 查看容器所有日志
docker logs mysql
# 持续查看容器日志
docker logs -f mysql
# 查看 5 分钟内的日志
docker logs --since 5m -f mysql

# 挂在本地目录
root@zsx-translate-test:~# docker run --rm -it -v /home/data:/data nginx bash
root@2a6fcf14ee5e:/# df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay         296G  207G   76G  74% /
tmpfs            64M     0   64M   0% /dev
tmpfs            16G     0   16G   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/vda1       296G  207G   76G  74% /data
tmpfs            16G     0   16G   0% /proc/acpi
tmpfs            16G     0   16G   0% /proc/scsi
tmpfs            16G     0   16G   0% /sys/firmware

# 端口映射
root@zsx-translate-test:~# docker run --rm -d -p 10080:80  nginx
47ac55262892e162d4d50ae64dd71d70e857e750d770c19039faaa87db7335d0
root@zsx-translate-test:~# docker ps | grep nginx
47ac55262892        nginx                         "nginx -g 'daemon of…"   3 seconds ago        Up 1 second         0.0.0.0:10080->80/tcp              elated_mclaren
8c0622290638        nginx                         "nginx -g 'daemon of…"   55 seconds ago       Up 53 seconds       80/tcp                             silly_jackson
6ccf73b6683c        nginx                         "nginx -g 'daemon of…"   About a minute ago   Up About a minute   80/tcp                             vigilant_ellis

#  配置环境参数 -e
docker run -p 3306:3306 -d -e "MYSQL_ROOT_PASSWORD=123456" mysql:5.7

docker-compose

# 安装
# centos 可以使用 yum install
pip install docker-compose

# 使用 docker-compose 创建一组容器
docker-compose up -d

# 使用 docker-compose 关闭一组容器
docker-compose down

# mysql docker-compose
version: '2'
services:
  mysql:
    image: mysql:5.7
    container_name: mysql
    restart: always
    ports:
      - "3306:3306"
    volumes:
     - mysql_data:/var/lib/mysql
    environment:
     - MYSQL_ROOT_PASSWORD=123456
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

volumes:
  mysql_data:
    driver: local
# redis docker-compose
version: '2'
services:
  redis:
    image: redis
    container_name: redis
    restart: always
    ports:
      - "6379:6379"
    volumes:
      - data:/data
    command: redis-server --requirepass 123456

volumes:
  data:
    driver: local

镜像 build

(cmdb) [root@infra-jumpserver002 ht-docker]# tree
.
├── Dockerfile
├── pip.conf
├── requirements.txt
└── sources.list

0 directories, 4 files
# filename: Dockerfile
FROM python:3.6

ENV LANG C.UTF-8
WORKDIR /app
COPY sources.list /etc/apt/sources.list
COPY pip.conf /root/.pip/pip.conf

COPY requirements.txt .
RUN pip install --no-cache-dir cpython
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir gunicorn==19.9.0
docker build -t ht:v1 .
# filename: pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com
# filename: sources.list
deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb http://mirrors.aliyun.com/debian-security stretch/updates main
deb-src http://mirrors.aliyun.com/debian-security stretch/updates main
deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
# filename: requirements.txt
backcall==0.1.0
boto3==1.9.249
botocore==1.12.249
certifi==2019.9.11
chardet==3.0.4
Click==7.0
decorator==4.4.0
docutils==0.15.2
Flask==1.1.1
Flask-Cors==3.0.8
idna==2.8
ipython==7.8.0
ipython-genutils==0.2.0
itsdangerous==1.1.0
jedi==0.15.1
Jinja2==2.10.3
jmespath==0.9.4
line-profiler==2.1.2
MarkupSafe==1.1.1
numpy==1.17.2
parso==0.5.1
pexpect==4.7.0
pickleshare==0.7.5
prompt-toolkit==2.0.10
ptyprocess==0.6.0
Pygments==2.4.2
python-dateutil==2.8.0
python-Levenshtein==0.12.0
pytorch-crf==0.7.2
pytorch-pretrained-bert==0.6.2
regex==2019.8.19
requests==2.22.0
s3transfer==0.2.1
torch==1.3.0
tqdm==4.36.1
traitlets==4.3.3
urllib3==1.25.6
wcwidth==0.1.7
Werkzeug==0.16.0

你可能感兴趣的:(docker 常用命令)