最近工作需要用到docker,那就简单写个笔记到时候可以查阅使用。
参考菜鸟教程:https://www.runoob.com/docker/docker-hello-world.html
简单介绍:
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows 机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
一个完整的Docker有以下几个部分组成:
-
DockerClient客户端
-
Docker Daemon守护进程
-
Docker Image镜像
检索容器:
运行 docker search xxx
[root@VM_0_17_centos sidian]# docker search python3 INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/rackspacedot/python37 10 docker.io docker.io/openwhisk/python3action Apache OpenWhisk runtime for Python 3 Actions 5 docker.io docker.io/sellpy/python3-jupyter-sklearn python3-jupyter-sklearn 5 [OK]
docker pull 拉取镜像 通过全名拉取。
[root@VM_0_17_centos sidian]# docker pull docker.io/rackspacedot/python37 Using default tag: latest Trying to pull repository docker.io/rackspacedot/python37 ... latest: Pulling from docker.io/rackspacedot/python37 55cbf04beb70: Pull complete 1607093a898c: Pull complete 9a8ea045c926: Pull complete d4eee24d4dac: Downloading [===================================> ] 35.72 MB/50.06 MB b59856e9f0ab: Downloading [=========> ] 40.71 MB/213.2 MB b023afffd10b: Downloading [===> ] 436.6 kB/5.745 MB 13e2e806d7c8: Waiting
运行
docker -id 可以后台启动容器,可以通过docker exec -it 容器id /bin/bash 进入交互界面,这样的启动方式。退出的话,容器不会退出。
shijianzhongdeMacBook-Pro:~ shijianzhong$ docker exec -it dab2 /bin/bash [root@dab2da3d3404 /]# exit exit shijianzhongdeMacBook-Pro:~ shijianzhong$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dab2da3d3404 centos "/bin/bash" 4 minutes ago Up 4 minutes dreamy_engelbart shijianzhongdeMacBook-Pro:~ shijianzhong$
也可以通过docker -itd后台运行镜像,这样的话可以通过attach连接后台的运行容器,或者通过docker exec -it 容器id 进入交互界面,但通过exit退出的话,
运行的容器也会退出
docker exec:推荐大家使用 docker exec 命令,因为此退出容器终端,不会导致容器的停止。
shijianzhongdeMacBook-Pro:~ shijianzhong$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dab2da3d3404 centos "/bin/bash" 4 minutes ago Up 4 minutes dreamy_engelbart shijianzhongdeMacBook-Pro:~ shijianzhong$ docker run -itd centos 88a6596beedc9ebe06429b1b70437c33bf89ad1172ec96330eec28cae41f6c1a shijianzhongdeMacBook-Pro:~ shijianzhong$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 88a6596beedc centos "/bin/bash" 7 seconds ago Up 6 seconds wonderful_borg dab2da3d3404 centos "/bin/bash" 8 minutes ago Up 8 minutes dreamy_engelbart shijianzhongdeMacBook-Pro:~ shijianzhong$ docker attach 88 [root@88a6596beedc /]# exit exit shijianzhongdeMacBook-Pro:~ shijianzhong$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dab2da3d3404 centos "/bin/bash" 8 minutes ago Up 8 minutes dreamy_engelbart shijianzhongdeMacBook-Pro:~ shijianzhong$
1.停用全部运行中的容器:
docker stop $(docker ps -q)
2.删除全部容器:
docker rm $(docker ps -aq)
3.一条命令实现停用并删除容器:
docker stop $(docker ps -q) & docker rm $(docker ps -aq)