Docker入门笔记(1)

Docker入门笔记(1)

1、安装Docker

yum -y install docker-ce

2、查看Docker版本

[root@localhost ~]# docker -v
Docker version 18.09., build 774a1f4

3、启动Docker

[root@localhost ~]# docker ps # docker服务尚未启用,此处测试
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@localhost ~]# systemctl start docker # 启动Docker
[root@localhost ~]# docker ps # 查看docker应用列表
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

3、修改Docker镜像源,/etc/docker/daemon.json写入以下内容

[root@localhost ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}

4、重启Docker

[root@localhost ~]# systemctl restart docker

5、下载镜像busybox测试

[root@localhost ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
Digest: sha256:061ca9704a714ee3e8b80523ec720c64f6209ad3f97c0ff7cb9ec7d19f15149f
Status: Image is up to date for busybox:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest d8233ab899d4 weeks ago .2MB

6、docker run :创建一个新的容器并运行一个命令

语法

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

OPTIONS说明:

  • -a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;

  • -d: 后台运行容器,并返回容器ID;

  • -i: 以交互模式运行容器,通常与 -t 同时使用;

  • -p: 端口映射,格式为:主机(宿主)端口:容器端口

  • -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;

  • --name="nginx-lb": 为容器指定一个名称;

  • --dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;

  • --dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;

  • -h "mars": 指定容器的hostname;

  • -e username="ritchie": 设置环境变量;

  • --env-file=[]: 从指定文件读入环境变量;

  • --cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行;

  • -m :设置容器使用内存最大值;

  • --net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container:<name|id> 四种类型;

  • --link=[]: 添加链接到另一个容器;

  • --expose=[]: 开放一个端口或一组端口;

[root@localhost ~]# docker run -it busybox
/ # pwd
/
/ # ll
sh: ll: not found
/ # ls
bin dev etc home proc root sys tmp usr var
/ # date
Tue Mar :: UTC
/ # echo "hello docker"
hello docker
/ #

查看docker应用:

[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e68ba125f9e3 busybox "sh" seconds ago Up seconds romantic_franklin

退出busybox,并查看docker应用

[root@localhost ~]# docker run -it busybox
/ # pwd
/
/ # exit
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

7、删除镜像(-f 强制删除)

[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest d8233ab899d4 weeks ago .2MB
[root@localhost ~]# docker rmi -f d8233ab899d4
Untagged: busybox:latest
Untagged: busybox@sha256:061ca9704a714ee3e8b80523ec720c64f6209ad3f97c0ff7cb9ec7d19f15149f
Deleted: sha256:d8233ab899d419c58cf3634c0df54ff5d8acc28f8173f09c21df4a07229e1205
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE

8、删除某个container

[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
598c5f3d52e0 nginx "nginx -g 'daemon of…" minutes ago Up minutes 0.0.0.0:->/tcp myNginx
[root@localhost ~]# docker stop myNginx
myNginx
[root@localhost ~]# docker rm myNginx
myNginx

9、删除所有container

[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e377dc9fde3 nginx "nginx -g 'daemon of…" minutes ago Exited () minutes ago xNginx
e68ba125f9e3 d8233ab899d4 "sh" minutes ago Exited () minutes ago romantic_franklin
b042ee32107a d8233ab899d4 "sh" minutes ago Exited () minutes ago zealous_hermann
e29fe83c5eb4 d8233ab899d4 "sh" minutes ago Exited () minutes ago fervent_galileo
d1915c908b41 d8233ab899d4 "sh" minutes ago Exited () minutes ago practical_hoover
14c261d6051e d8233ab899d4 "echo “hello world”" hours ago Exited () hours ago thirsty_chaum
450982479c5c 9b9cb95443b5 "/bin/sh -c 'while t…" hours ago Exited () hours ago infallible_clarke
5627a01ee522 9b9cb95443b5 "/bin/bash" hours ago Exited () hours ago epic_brattain
485bddc78ae8 9b9cb95443b5 "/bin/echo 'Hello wo…" hours ago Exited () hours ago gallant_banzai
018b86f38fd5 9b9cb95443b5 "/bin/echo 'Hello wo…" hours ago Exited () hours ago inspiring_franklin
[root@localhost ~]# docker rm $(docker ps -a -q)
9e377dc9fde3
e68ba125f9e3
b042ee32107a
e29fe83c5eb4
d1915c908b41
14c261d6051e
450982479c5c
5627a01ee522
485bddc78ae8
018b86f38fd5
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
上一篇:redis入门笔记


下一篇:Linux 入门笔记