Docker入门笔记1

747 字
4 分钟
Docker入门笔记1
2020-01-27
浏览量 加载中...

前言#

目前环境为centos7 64位

[root@diudiu ~]# cat /proc/version Linux version 3.10.0-514.26.2.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Tue Jul 4 15:04:05 UTC 2017 Docker版本为 1.13.1 主要参考W3C的相关课程 w3cschool

安装与测试#

yum install docker -y

启动后台:

[root@diudiu ~]# systemctl start docker

查看版本号

[root@diudiu ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7f2769b/1.13.1
Built: Sun Sep 15 14:06:47 2019
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7f2769b/1.13.1
Built: Sun Sep 15 14:06:47 2019
OS/Arch: linux/amd64
Experimental: false

运行Docker 程序#

拉取并运行hello-world#

[root@diudiu ~]# docker pull hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
Status: Image is up to date for docker.io/hello-world:latest
[root@diudiu ~]# docker run hello-world
container_linux.go:235: starting container process caused "process_linux.go:258: applying cgroup con
figuration for process caused \"Cannot set property TasksAccounting, or unknown property.\""
/usr/bin/docker-current: Error response from daemon: oci runtime error: container_linux.go:235: star
ting container process caused "process_linux.go:258: applying cgroup configuration for process cause
d \"Cannot set property TasksAccounting, or unknown property.\"".

可以发现是运行失败的。 寻求了一下度娘和谷哥的帮助,得到了解决的方案

yum update

即可以解决了。

[root@diudiu ~]# docker run hello-world
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/get-started/

Docker的使用#

Docker Hello World#

使用 docker run 命令来在容器内运行一个应用程序#

docker run ubuntu:18.04 /bin/echo "Hello world"
  • docker: Docker 的二进制执行文件。
  • run:与前面的 docker 组合来运行一个容器。
  • ubuntu:18.04指定要运行的镜像,Docker首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
  • /bin/echo “Hello world”: 在启动的容器里执行的命令

输出:

[root@diudiu ~]# docker run ubuntu:18.04 /bin/echo "Hello world"
Unable to find image 'ubuntu:18.04' locally
Trying to pull repository docker.io/library/ubuntu ...
18.04: Pulling from docker.io/library/ubuntu
5c939e3a4d10: Pull complete
c63719cdbe7a: Pull complete
19a861ea6baf: Pull complete
651c9d2d6c4f: Pull complete
Digest: sha256:8d31dad0c58f552e890d68bbfb735588b6b820a46e459672d96e585871acc110
Status: Downloaded newer image for docker.io/ubuntu:18.04
Hello world

运行交互的容器#

这句话很好理解,就是进入容器的终端,

  • t:在新容器内指定一个伪终端或终端。

  • i:允许你对容器内的标准输入 (STDIN) 进行交互。

    [root@diudiu ~]# docker run -i -t ubuntu:18.04 /bin/bash root@33d4c7c3f7be:/#

通过exit或者Ctrl D 退出

启动容器(后台模式)#

[root@diudiu ~]# docker run -d ubuntu:18.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
161723c789e4c225bf7a83962320a94743f81f8290d3b74ffd60a4614929380b

会输出容器ID,且为唯一的,

通过docker ps 确认是否有容器在运行 [root@diudiu ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES 161723c789e4 ubuntu:18.04 “/bin/sh -c ‘while…” About a minute ago Up About a min ute gracious_kare

查看logs

[root@diudiu ~]# docker logs 161723c789e4
hello world
hello world
hello world
hello world
hello world
hello world
hello world

或者: docker logs gracious_kare

停止Docker#

docker stop 161723c789e4
or
docker stop gracious_kare

使用docker ps 查看状态

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

Docker入门笔记1
http://blog.diudiudevil.cn/posts/docker-getting-started-part-1/
作者
diudiu (李明)
发布于
2020-01-27
许可协议
CC BY-NC-SA 4.0
最后更新于 2020-01-28,距今已过 2237 天

部分内容可能已过时

评论区

Profile Image of the Author
diudiu (李明)
读书、笔记、复盘和思考.
公告
博客重生🎉,正在建设中
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
站点统计
文章
13
分类
4
标签
23
总字数
8,928
运行时长
0
最后活动
0 天前

目录