Docker从无到有

随着各个软件的版本越来越多,软件开发、使用环境愈发复杂,Docker日益受到广泛应用。本文记录下从零开始了解、使用docker的各个步骤。

命令行安装docker(其实安装的是docker engine,区别于docker desktop):一键运行

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh ./get-docker.sh

注意,如果在ubuntu20.04下遇到apt update的问题
The repository 'https://dl.bintray.com/sbt/debian Release' no longer has a Release file.
去software update -> other software 里把所有https://dl.bintray.com/sbt/debian都取消勾选。
不要用apt安装docker.io,那个是过时的。

然后post-install,把用户(自己)加入可运行docker的组中:

sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker

验证安装正确:docker run hello-world
如果在做上述post-install之前就sudo docker run hello-world 验证,那么在普通用户下,会有权限错误:permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json": dial unix /var/run/docker.sock: connect: permission denied
解决方法:sudo chmod 666 /var/run/docker.sock.


Docker有3个基本概念:

  1. Image,镜像。镜像就是系统的快照。静态。每个Image以<Repo Name>:<Tag Name>为名字,如ubuntu:15:10.
  2. Container,容器。容器是基于某个镜像开始的运行状态。动态。基于一个Image,可以有多个Container。
  3. Repo/Hub,仓库。跟github差不多,是镜像集中存储的地方。

关于镜像的操作:

  1. 如何获取镜像:有2种方法:
    1. 直接pull,类似git pull。
      直接运行命令
      docker pull <repo>:<tag>
    2. 通过Dockerfile。
      Dockerfile是构建Image的文本文件,自然也可以用来创建一个Image。只要在Dockerfile内加入
      FROM <repo>:<tag>,然后在当前目录内运行
      docker build . 即可。
  2. 如何查看镜像:
    docker images / docker image ls
  3. 如何删除镜像:
    docker image rm <repo>:<tag> / docker rmi <repo>:<tag>

关于容器的操作。容器是某个镜像的运行实例。

  1. Docker的七个status具体定义:
  • Created: A created container was prepared but never started. You can create a container in advance with docker create in preparation for a job you want to run later. It’s also possible for a container to get stuck in the created state. This can happen when it needs a resource that another container already has, such as a network port.
  • Restarting: A container is in the process of restarting. You can manually restart a container with docker restart or configure a container to restart on failure.
  • Running: A container that’s up and running. This indicates that docker start succeeded.
  • Removing: After you stop a container, it remains available until it’s removed. This state indicates that removal has started. This state may mean it’s a large container or there’s a problem removing it.
  • Paused: A container has been paused with docker pause.
  • Exited: The command that started the container has exited. This may be because of an exception, the docker stop command, or because the command completed. An exited container isn’t consuming any CPU or memory.
  • Dead: Containers in a dead state aren’t operational and can only be removed.
    状态跳转。其中created和started通过rm进入dead。
    图源(https://dev.to/dhanushkadev/introduction-to-the-docker-life-cycle-4e6j)
  1. 如何启动镜像,操作容器。有了上述定义,可以从镜像启动容器了。指定要运行的镜像,Docker 首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
命令 说明
docker run -it --name=<string> <IMAGE> /bin/bash <IMAGE>创建一个新的名为<string>的container,并交互运行bash。如果<IMAGE>只有仓库名,那么会自动下载tag为latest的Image并运行。所以一定要加:<tag>
docker container create -it --name=<string> <IMAGE> 创建容器
在docker里输入exitCtrl+D 退出docker,且docker状态可能为exited
在docker里输入Ctrl+P+Q 退出docker,且docker状态为running
docker start -ai <Container> 启动stopped(exited)或created容器
docker exec -it <Container> bash 启动running容器,此时无论怎么退出都保持running
docker stop <Container> 使容器stopped(exited)
docker attach <Container> 启动容器,此时exit退出会导致exited
docker rename <Container ID/name> <string> 重命名容器
docker export -o <output_name> <Container> 导出容器(可以供别人使用)
docker rm <ID/name> 删除容器
docker commit <option> <repo:tag> 从当前运行的container制作镜像
docker [image] tag <source img> <target img> 给image换个名字,id不变
docker system df 查看docker大小size,占磁盘容量
  1. 容器内外传输文件。在主机中输入命令:
    docker cp <src_path> <container>:<dest_path> 是主机向容器复制
    docker cp <container>:<src_path> <dest_path> 是容器向主机复制

  2. 主机通过指定命令或脚本,在docker内运行程序:
    docker exec <Container> bash -c "<commands>"
    对于实时验证开发十分管用。不进入交互界面。

  3. 进入容器以后,首先
    apt update
    否则所有apt install命令都E: Unable to locate package <package name>。这是因为package cache里没有任何东西。首先要更新。


图源https://blog.csdn.net/Castlehe/article/details/115118710

  1. 制作持久化容器

save
export

  1. 制作新镜像:

有两种方式,一个是dockerfile,一个是手动操作
(1)dockerfile:docker build即可
(2)手动操作:docker run 一个image,做修改,ctrl-P-Q + docker commit + docker tag


Docker Hub
是镜像image集中存放的地方。
类似于github,也有账号、仓库的概念,每次push都可以有个tag来做同一个仓库下版本的区分。
如何提交docker image(docker push)

  1. 首先创建镜像
  2. docker login
  3. docker push <repo:tag>

错误分析:

  1. denied: requested access to the resource is denied 一般是tag或者仓库名问题,检查仓库名是否正确,不正确重新tag
  2. 发现repo的name和tag都为none:git tag <img id> <repo:tag>

猜你喜欢

转载自blog.csdn.net/qq_44345567/article/details/128623632