1.容器管理
使用 docker create -it *** bash 来创建一个容器,但是这个容器并没有启动
[root@chenshi1 ~]# docker create -it centos7 bash
Unable to find image 'centos7:latest' locally
Error response from daemon: pull access denied for centos7, repository does not exist or may require 'docker login'
[root@chenshi1 ~]# docker create -it centos bash
96471d341dfe3ae7be71638a6b121a064cce8df35dfd02704129f70d1b84a4d1
[root@chenshi1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce962e9d5282 centos "/bin/bash" About a minute ago Up About a minute agitated_jang
[root@chenshi1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96471d341dfe centos "bash" 11 seconds ago Created zen_jennings
ce962e9d5282 centos "/bin/bash" About a minute ago Up About a minute agitated_jang
60c9a5d24759 centos "/bin/bash" 4 days ago Exited (137) 4 days ago eloquent_panini
b3a906d1c099 centos "/bin/bash" 4 days ago Exited (137) 4 days ago awesome_golick
启动容器
[root@chenshi1 ~]# docker start 96471d341dfe
96471d341dfe
[root@chenshi1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96471d341dfe centos "bash" About a minute ago Up 2 seconds zen_jennings
ce962e9d5282 centos "/bin/bash" 2 minutes ago Up 2 minutes agitated_jang
使用docker run相当于先create再start
进入了一个虚拟终端里面,我们可以运行一些命令,使用命令exit或者ctrl d 退出该bash,当退出后这个容器也会停止。
[root@chenshi1 ~]# docker run -it centos
[root@fae1d00bb1fa /]# ps
PID TTY TIME CMD
1 pts/0 00:00:00 bash
14 pts/0 00:00:00 ps
docker run -d 可以让容器在后台运行比如:docker run -d centos bash -c "while :; do echo "123"; sleep 2; done"docker run --name web -itd centos bash // --name 给容器自定义名字docker run --rm -it centos bash -c "sleep 30" //--rm 可以让容器退出后直接删除,在这里命令执行完容器就会退出
[root@chenshi1 ~]# docker run -itd --name centos_1 centos bash
20bb5678a7079e4af79eb4d54d724979d02af47ca61d37ae961dd63fabf46522
[root@chenshi1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20bb5678a707 centos "bash" 5 seconds ago Up 3 seconds centos_1
96471d341dfe centos "bash" 15 minutes ago Up 13 minutes zen_jennings
ce962e9d5282 centos "/bin/bash" 16 minutes ago Up 16 minutes agitated_jang
使用自定义名字进入容器,不用加id
[root@chenshi1 ~]# docker exec -it centos_1 bash
[root@20bb5678a707 /]#
docker logs 可以获取到容器的运行历史信息,用法如下
[root@chenshi1 ~]# docker run -itd centos bash -c "echo 123"
c277970f3288d7fc2cc93a0f82a75c9de236a350b4abd01a260528f0e5a17982
[root@chenshi1 ~]# docker logs c277970f3288d7fc2
123
docker attach 可以进入一个后台运行的容器,比如docker attach container_id //但是attach命令不算好用,比如我们想要退出终端,就得exit了,这样容器也就退出了,还有一种方法docker exec -it container_id bash //可以临时打开一个虚拟终端,并且exit后,容器依然运行着---------------------------------------------------------------------------------
docker rm container_id //container_id是ps的时候查看到的,这样就可以把container删除,如果是运行的容器,可以加-f
[root@chenshi1 ~]# docker rm c277970f3288
c277970f3288
[root@chenshi1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20bb5678a707 centos "bash" 10 minutes ago Up 10 minutes centos_1
fae1d00bb1fa centos "/bin/bash" 18 minutes ago Exited (0) 17 minutes ago wizardly_newton
96471d341dfe centos "bash" 25 minutes ago Up 24 minutes zen_jennings
ce962e9d5282 centos "/bin/bash" 26 minutes ago Up 26 minutes agitated_jang
60c9a5d24759 centos "/bin/bash" 4 days ago Exited (137) 4 days ago eloquent_panini
b3a906d1c099 centos "/bin/bash" 4 days ago Exited (137) 4 days ago awesome_golick
2.仓库管理
使用docker的时候拉取的容器都是从官方网站上拉取的,提交也是提交到官方网站的仓库中。
docker pull registry //下载registry 镜像,registy为docker官方提供的一个镜像,我们可以用它来创建本地的docker私有仓库。
[root@chenshi1 ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
d6a5679aa3cf: Pull complete
ad0eac849f8f: Pull complete
2261ba058a15: Pull complete
f296fda86f10: Pull complete
bcd4a541795b: Pull complete
Digest: sha256:5a156ff125e5a12ac7fdec2b90b7e2ae5120fa249cf62248337b6d04abc574c8
Status: Downloaded newer image for registry:latest
[root@chenshi1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 75835a67d134 4 weeks ago 200MB
registry latest 2e2f252f3c88 2 months ago 33.3MB
docker run -d -p 5000:5000 registry //以registry镜像启动容器,-p会把容器的端口映射到宿主机上,:左边为宿主机监听端口,:右边为容器监听端口做映射是为了通过外网也能访问容器内对应的端口lnmp中将会用到
[root@chenshi1 ~]# docker run -d -p 5000:5000 registry
212b8ed046e4930416b5d7fc68454528ccd62789def51c3d38c0ddbad5114e38
[root@chenshi1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
212b8ed046e4 registry "/entrypoint.sh /etc…" 17 seconds ago Up 16 seconds 0.0.0.0:5000->5000/tcp clever_saha
把镜像上传到本地的仓库中
[root@chenshi1 ~]# docker tag centos 192.168.246.128:5000/centos
[root@chenshi1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.246.128:5000/centos latest 75835a67d134 4 weeks ago 200MB
centos latest 75835a67d134 4 weeks ago 200MB
registry latest 2e2f252f3c88 2 months ago 33.3MB
但是拉取镜像的时候会出错,需要更改配置文件如下
vi /etc/docker/daemon.json//更改为
{ "insecure-registries":["本机ip:5000"] }
systemctl restart docker
重启服务后需要启动镜像
查看推送上来的镜像
[root@chenshi1 ~]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":["centos"]}
tip:
如何做到在docker启动时启动所有容器?