Mac下安装
brew cask install docker
配置国内镜像
镜像加速DaoCloud
输入docker info
在最后几行看是否配置成功。
Registry Mirrors:
http://##DaoCloud获取的地址
Live Restore Enabled: false
运行HelloWorld
输入docker run hello-world
,输出下面内容。
➜ ~ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9db2ca6ccae0: Pull complete
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
镜像命令
查看镜像
查看镜像 docker images
输出安装的镜像。
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 2cb0d9787c4d 5 weeks ago 1.85kB
各个选项说明:
- REPOSITORY:表示镜像的仓库源
- TAG:镜像的标签
- IMAGE ID:镜像ID
- CREATED:镜像创建时间
- SIZE:镜像大小
查找镜像
查找镜像httpd docker search httpd
➜ ~ docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 1908 [OK]
- NAME:镜像仓库源的名称
- DESCRIPTION:镜像的描述
- OFFICIAL:是否docker官方发布
安装镜像
安装镜像httpd docker pull httpd
下载完成后使用 docker run httpd
删除镜像
docker rmi httpd
显示镜像的历史
docker history httpd
容器命令
docker容器可以理解为在沙盒中运行的进程。包含进程运行所必须的资源。包括文件系统、系统类库、shell 环境等等。但这个沙盒默认是不会运行任何程序的。你需要在沙盒中运行一个进程来启动某一个容器。这个进程是该容器的唯一进程,所以当该进程结束的时候,容器也会完全的停止。
交互式进入容器
➜ ~ docker run -i -t centos
[root@1ab526eb869f /]# ps
PID TTY TIME CMD
1 pts/0 00:00:00 bash
14 pts/0 00:00:00 ps
[root@1ab526eb869f /]#
直接在容器中运行命令
docker run centos echo "hello word"
无法进行交互。
查看容器
# 正在运行的容器
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ab526eb869f centos "/bin/bash" About a minute ago Up About a minute festive_newton
# 列出所有的容器,停止和运行的
➜ ~ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ab526eb869f centos "/bin/bash" About a minute ago Up About a minute festive_newton
b0082eefb8fa centos "/bin/bash" 10 hours ago Exited (137) 10 hours ago ecstatic_borg
# 列出最近一次启动的容器
➜ ~ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ab526eb869f centos "/bin/bash" About a minute ago Up About a minute festive_newton
➜ ~
保存容器
当你对某个容器做了修改后,退出容器后并不会保存,要自己保存。
-m:提交的描述信息
-a:指定镜像作者
➜ ~ docker commit 1ab526eb869f centos:01
sha256:8d6ec7ae82d4ff1b42e46ba31a175ac4e0e93ac08f4bdf57a3ee852eab051544
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 01 8d6ec7ae82d4 5 seconds ago 200MB
centos latest 5182e96772bf 13 days ago 200MB
可以看到为刚刚运行的容器保存了,并且从新命名。
对容器操作
rm、stop、start、kill、logs、diff、top、cp、restart、attach
# 删除所有容器
➜ ~ docker rm `docker ps -a -q`
# 删除单个容器
➜ ~ docker rm NAME/ID
# 启动、停止、重启、杀死容器
➜ ~ docker start NAME/ID
➜ ~ docker stop NAME/ID
➜ ~ docker restart NAME/ID
➜ ~ docker kill NAME/ID
从容器中取出日志,可以看到我有两次操作。
➜ ~ docker logs -t 1ab526eb869f
[root@1ab526eb869f /]# ps
2018-08-20T03:44:09.096292900Z PID TTY TIME CMD
2018-08-20T03:44:09.096764000Z 1 pts/0 00:00:00 bash
2018-08-20T03:44:09.096915600Z 14 pts/0 00:00:00 ps
2018-08-20T03:47:47.473272200Z [root@1ab526eb869f /]# exit
2018-08-20T03:47:47.473336800Z exit
[root@1ab526eb869f /]# ls
2018-08-20T04:04:25.545790200Z anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
2018-08-20T04:04:27.207549600Z [root@1ab526eb869f /]# pwd
2018-08-20T04:04:27.208339600Z /
2018-08-20T04:04:28.831168800Z [root@1ab526eb869f /]# cd
2018-08-20T04:04:29.408619400Z [root@1ab526eb869f ~]# ls
2018-08-20T04:04:29.410958700Z anaconda-ks.cfg
2018-08-20T04:04:39.992510800Z [root@1ab526eb869f ~]# exit
列出一个容器里面被改变的文件或者目录
list列表会显示出三种事件,A 增加的,D 删除的,C 被改变的
➜ ~ docker diff 1ab526eb869f
C /root
A /root/.bash_history
容器里的进程,容器必须运行。
➜ ~ docker top 1ab526eb869f
PID USER TIME COMMAND
4611 root 0:00 /bin/bash
➜ ~
从容器中拷贝文件/目录到本地目录
➜ ~ docker cp 1ab526eb869f:/root/a.txt ~/
➜ ~ cd
➜ ~ ls | grep a.txt
a.txt
➜ ~
容器启动中,直接进入命令模式
➜ ~ docker attach 1ab526eb869f
[root@1ab526eb869f ~]#
镜像的保存和加载
保存镜像
➜ ~ docker save 5182e96772bf -o ~/en.tar
➜ ~ docker save 5182e96772bf > ~/an.tar
➜ ~ cd
➜ ~ ls | grep ...tar
an.tar
en.tar
➜ ~
加载镜像
docker load -i en.tar
docker load < an.tar
遇到的问题
在删除镜像的时候,必须要使用改镜像的容器,先将容器停止。
不然报错误。
Error response from daemon: conflict: unable to remove repository reference "centos:01" (must force) - container f70555008ac2 is using its referenced image 8d6ec7ae82d4
我是这样删除,镜像的。
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 01 8d6ec7ae82d4 33 minutes ago 200MB
centos latest 5182e96772bf 13 days ago 200MB
hello-world latest 2cb0d9787c4d 5 weeks ago 1.85kB
scrapinghub/splash latest 3926e5aac017 6 months ago 1.22GB
➜ ~ docker rmi centos:01
Error response from daemon: conflict: unable to remove repository reference "centos:01" (must force) - container f70555008ac2 is using its referenced image 8d6ec7ae82d4
➜ ~ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f70555008ac2 centos:01 "/bin/bash" 21 minutes ago Exited (0) 21 minutes ago laughing_hypatia
1ab526eb869f centos "/bin/bash" 38 minutes ago Up 14 minutes festive_newton
b0082eefb8fa centos "/bin/bash" 11 hours ago Exited (137) 11 hours ago ecstatic_borg
➜ ~ docker rm f70555008ac2
f70555008ac2
➜ ~ docker rmi centos:01
Untagged: centos:01
Deleted: sha256:8d6ec7ae82d4ff1b42e46ba31a175ac4e0e93ac08f4bdf57a3ee852eab051544
Deleted: sha256:f9d191ae4d435e967fec27d5182cb7a4ea6295dddeaa5717be2a4bd72af2517e