网络知识 娱乐 docker 命令

docker 命令

docker

一 安装

a Ubuntu

sudo apt-get update 
sudo apt-get install docker-io

b centos

yum -y install docker-io

c 运行docker

systemctl restart docker

二 镜像命令

1. docker images

  • 查看本地已经拥有的镜像

2. docker pull 镜像名

  • 下载镜像

3. docker rmi 镜像id/镜像名

  • 删除镜像(多个)

4. docker commit

  • 用运行的容器创建镜像
  • 参数: -a 作者 -m '注解' 容器名(id) 镜像名:镜像版本号

5. docker push 镜像名:镜像版本号

  • 上传镜像

三 容器命令

1. docker ps

查看运行中的容器

2. docker ps -a

查看所有容器

3. docker rm 容器id

删除容器

4. docker stop 容器id

停止容器

5. docker start 容器id

启动容器

6. docker restart 容器id

重启容器

7. docker run

创建并启动容器

  • -it 启动交互终端(结尾需要驾驶 /bin/bash)
  • -d 后台启动
  • -p 80:8080 端口映射80为宿主端口,8080为容器端口 -- name=blog 容器命名

8. control+p+q

退出容器并保持容器运行(终止容器退出命令为exit)

9. docker exec -it 容器名或者容器的id /bin/bash

进入正在运行的容器

10. docker cp

  • 宿主机到容器 # 将主机/www/runoob目录拷贝到容器96f7f14e99ab中,目录重命名为www。 docker cp /www/runoob 96f7f14e99ab:/www# 将容器96f7f14e99ab的/www目录拷贝到主机的/tmp目录中。 docker cp 96f7f14e99ab:/www /tmp/四 Dockerfile
  • 容器到宿主机

1. Dockerfile文件

# 指定基础镜像文件
FROM ubuntu:latest

# 指定维护者信息
MAINTAINER wanghaifei

# 将代码copy到容器中。如 ADD ./blog /blog 表示将和dockerfile文件同级的blog代码copy到容器的根路径/blog中
ADD 和dockerfile文件相对的项目地址 拷贝到容器中的项目地址

# 指定工作目录
WORKDIR /blog

# 复制
COPY 地址  新地址

# 执行更新命令
RUN apt update
# 创建项目地址的日志文件或者媒体文件等
RUN mkdir -p /日志logs地址
RUN mkdir -p /媒体media地址

# 指定开放端口
EXPOSE 8000

# 在容器启动时执行命令
ENTRYPOINT ["python"]
CMD ["manage.py", "runserver", "0.0.0.0:8000"]

2. 生成镜像

docker build -t 镜像名 . (注意最后有个点用来表示当前目录,初次构建速度会比较慢,需要多等一会。)

五 dockerHUB私服

1. 搭建registry

# 拉取registry镜像
root@qjun-x230:/home/qjun# docker pull registry

# 查看镜像
root@qjun-x230:/home/qjun# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
registry            latest              f32a97de94e1        3 months ago        25.8MB

# 基于registry 创建启动 qdockerhub容器
root@qjun-x230:/home/qjun# docker run -d -v /data/registry:/var/lib/registry -p 5000:5000 --restart=always --name qdockerhub registry:latest
b8621bf02781ffccd25a2d2f21cbb54aee093c53ddbf77e39dfab6f05578126d

# 安装curl测试工具(Ubuntu系统)
root@qjun-x230:/home/qjun# apt install curl

# 测试/v2/_catalog接口
root@qjun-x230:/home/qjun# curl http://127.0.0.1:5000/v2/_catalog
{"repositories":[]}

2. 提交镜像

# 查看镜像
root@qjun-x230:/home/qjun# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              62c261073ecf        5 days ago          109MB

# tag 加上传地址标签
root@qjun-x230:/home/qjun# docker tag nginx 127.0.0.1:5000/qnginx

# 查看镜像
root@qjun-x230:/home/qjun# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
127.0.0.1:5000/qnginx   latest              62c261073ecf        5 days ago          109MB
nginx                   latest              62c261073ecf        5 days ago          109MB

# push镜像
root@qjun-x230:/home/qjun# docker push 127.0.0.1:5000/qnginx
The push refers to repository [127.0.0.1:5000/qnginx]
ea06a73e56fc: Pushed 
22c458a3ff08: Pushed 
6270adb5794c: Pushed 
latest: digest: sha256:8c3cdb5acd050a5a46be0bb5637e23d192f4ef010b4fb6c5af40e45c5b7a0a71 size: 948

# curl验证
root@qjun-x230:/home/qjun# curl http://127.0.0.1:5000/v2/_catalog
{"repositories":["qnginx"]}
root@qjun-x230:/home/qjun# 

3. 拉取镜像

root@qjun-x230:/home/qjun# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
# 拉取
root@qjun-x230:/home/qjun# docker pull 127.0.0.1:5000/qnginx:latest
latest: Pulling from qnginx
743f2d6c1f65: Already exists 
d6c2f01b1dae: Pull complete 
d4da6ff1b555: Pull complete 
Digest: sha256:8c3cdb5acd050a5a46be0bb5637e23d192f4ef010b4fb6c5af40e45c5b7a0a71
Status: Downloaded newer image for 127.0.0.1:5000/qnginx:latest
root@qjun-x230:/home/qjun# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
127.0.0.1:5000/qnginx   latest              62c261073ecf        5 days ago          109MB

六 错误集合

a. 运行错误1

错误提示: [root@izj6c0zsm04q86s2tu4e12z /]# docker run -it docker.io/ubuntu:latest /bin/bash
/usr/bin/docker-current: Error response from daemon: oci runtime error: container_linux.go:235: starting container process caused "process_linux.go:258: applying cgroup configuration for process caused "Cannot set property TasksAccounting, or unknown property."".

解决办法: yum update

b. 私有仓库push错误(改配置后一定要重启docker)

  • docker私有仓库的5000端口是否在防火墙中打开
  • daemon.json文件中提交镜像的地址是否修改为私有docker仓库的地址
  • 添加镜像仓库地址insecure-registries参数,内容修改如下:
vim /etc/docker/daemon.json
{
    "registry-mirrors":["https://registry.docker-cn.com"],
     "insecure-registries":["47.240.32.247:5000"]
}