docker学习(一)

it2022-05-05  226

docker学习(一)

atomic使用有点费劲,我改为centos7来做为学习环境。

1 安装

epel源就自带,目前版本是1.10.3

yum -y install docker docker version Client: Version: 1.10.3 API version: 1.22 Package version: docker-common-1.10.3-46.el7.centos.10.x86_64 Go version: go1.6.3 Git commit: d381c64-unsupported Built: Thu Aug 4 13:21:17 2016 OS/Arch: linux/amd64 Server: Version: 1.10.3 API version: 1.22 Package version: docker-common-1.10.3-46.el7.centos.10.x86_64 Go version: go1.6.3 Git commit: d381c64-unsupported Built: Thu Aug 4 13:21:17 2016 OS/Arch: linux/amd64
2 使用加速器下载镜像

国内访问docker.io剧慢,好在daocloud有号称终身免费的加速器,使用方法如下

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://682e782c.m.daocloud.io

不过这条命令运行后加速没什么效果,因为set_mirror.sh脚本有问题

if grep "CentOS Linux release 7" /etc/redhat-release > /dev/null then if [[ "$docker_version" -lt 12 ]] then sudo sed -i "s|ExecStart=/usr/bin/docker daemon|ExecStart=/usr/bin/docker daemon --registry-mirror="$MIRROR_URL"|g" /lib/systemd/system/docker.service sudo systemctl daemon-reload else sudo sed -i "s|^ExecStart=/usr/bin/dockerd$|ExecStart=/usr/bin/dockerd --registry-mirror="$MIRROR_URL"|g" /lib/systemd/system/docker.service sudo systemctl daemon-reload fi echo "Success." echo "You need to restart docker to take effect : sudo systemctl restart docker " exit 0 else echo "Error: Set mirror failed, please set registry-mirror manually please." exit 1 fi

脚本里是ExecStart=/usr/bin/docker daemon,docker.servicej里实际是ExecStart=/usr/bin/docker-current daemon,所以在后面手工加上--registry-'mirror=http://682e782c.m.daocloud.io'

更改完后,别忘记执行

systemctl daemon-reload systemctl restart docker && systemctl enable docker
3 下载镜像
docker pull centos:6 Trying to pull repository docker.io/library/centos ... 6: Pulling from docker.io/library/centos 08a7a0bb6122: Pull complete Digest: sha256:cd6d68000b47a91e7c94b558d7e3e653c3f0eac1a77842d97b0b7ad955cad608 Status: Downloaded newer image for docker.io/centos:6 docker pull centos:7 Trying to pull repository docker.io/library/centos ... 7: Pulling from docker.io/library/centos 3d8673bd162a: Pull complete Digest: sha256:a66ffcb73930584413de83311ca11a4cb4938c9b2521d331026dad970c19adf4 Status: Downloaded newer image for docker.io/centos:7 docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos 6 a3c09d36ab4a 3 weeks ago 194.6 MB docker.io/centos 7 970633036444 3 weeks ago 196.7 MB
4 建立交互型容器
docker run -i -t centos:6 /bin/bash [root@cab9ff817b6b /]# hostname cab9ff817b6b [root@cab9ff817b6b /]# cat /etc/hosts ... 172.17.0.2 cab9ff817b6b [root@cab9ff817b6b /]# ifconfig eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02 inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8 errors:0 dropped:0 overruns:0 frame:0 TX packets:8 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:648 (648.0 b) TX bytes:648 (648.0 b) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 [root@cab9ff817b6b /]# yum install vim [root@cab9ff817b6b /]# exit docker run --name web-01 -i -t centos:6 /bin/bash
5 启动容器
docker start cab9ff817b6b cab9ff817b6b docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cab9ff817b6b centos:6 "/bin/bash" 6 minutes ago Up 4 seconds suspicious_bose docker attach cab9ff817b6b 附着容器,可以理解登录容器的交互界面
6 建立守护型容器
docker run --name test_01 -d centos:6 /bin/sh -c 'while true; do echo hello world; sleep 1; done' c7d4781438a7a83d51324c0f2ed1237ab8a61a7b64911f20bc99ceeaf4e27955 docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c7d4781438a7 centos:6 "/bin/sh -c 'while tr" 9 seconds ago Up 7 seconds test_01 docker rm c7d4781438a7 Failed to remove container (c7d4781438a7): Error response from daemon: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f docker stop c7d4781438a7 c7d4781438a7 docker rm c7d4781438a7 c7d4781438a7
7 容器状态
docker top fb6b7ef0ef52 UID PID PPID C STIME TTY TIME CMD root 22974 21672 1 03:12 ? 00:00:00 /bin/sh -c while true; do echo hello world; sleep 1; done root 23031 22974 0 03:12 ? 00:00:00 sl docker logs fb6b7ef0ef52 docker logs -f fb6b7ef0ef52
8 在容器内部运行进程
docker exec -d fb6b7ef0ef52 touch /etc/new_config_file docker exec -i -t fb6b7ef0ef52 /bin/bash [root@fb6b7ef0ef52 /]# ls /etc/new_config_file /etc/new_config_file
8 自动重启容器
docker run --restart=always --name test_01 -d centos:6 /bin/sh -c 'while true; do echo hello world; sleep 1; done' 或者异常退出,自动重启5次 --restart=on-failure:5
9 获取更多容器信息
docker inspect fb6b7ef0ef52 docker inspect --format '{{.Name}} {{.State.Running}}' fb6b7ef0ef52 /test_01 true 杀死所有running状态的容器 docker kill $(docker ps -q) 删除所有已经停止的容器 docker rm $(docker ps -a -q) 删除所有\’untagged/dangling\’ ()状态的镜像 docker rmi $(docker images -q -f dangling=true) 删除所有镜像: docker rmi $(docker images -q) posted on 2016-08-24 10:25 北京涛子 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/liujitao79/p/5801852.html


最新回复(0)