Docker 基础命令操作

docker 基本操作:

 systemctl start docker #启动Docker服务
systemctl status docker #查看docker是否启动了,是否是运行状态. active(running) 表示docker正在运行
systemctl enable docker #设置Docker开机自启
systemctl disable docker #禁用Docker开机自启
systemctl restart docker #重新启动Docker服务

docker info #查看Docker信息
docker info | grep 'Docker Root Dir:' #查看docker info中具体key的信息

systemctl stop docker  #停止docker服务

一、仓库

最大共有仓库: hub.docker.com/

二、镜像

1、登陆登出   login logout

[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: 15291094743
Password:

[root@localhost ~]# docker logout
Removing login credentials for https://index.docker.io/v1/

2、查看镜像   image ls

[root@localhost ~]# docker image ls
REPOSITORY         TAG       IMAGE ID       CREATED         SIZE
busybox            latest    beae173ccac6   2 months ago    1.24MB
kongd/dockertest   busybox   f0b02e9d092d   17 months ago   1.23MB

3、删除镜像   rmi  image rm

[root@localhost ~]# docker rmi docker kongd/dockertest:busybox
Untagged: kongd/dockertest:busybox
Untagged: kongd/dockertest@sha256:c9249fdf56138f0d929e2080ae98ee9cb2946f71498fc1484288e6a935b5e5bc
Deleted: sha256:f0b02e9d092d905d0d87a8455a1ae3e9bb47b4aa3dc125125ca5cd10d6441c9f
Deleted: sha256:d2421964bad195c959ba147ad21626ccddc73a4f2638664ad1c07bd9df48a675


[root@localhost ~]# docker image rm busybox:latest
Untagged: busybox:latest
Untagged: busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678

[root@localhost ~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
busybox      latest    beae173ccac6   2 months ago   1.24MB

4、拉取镜像:  pull

[root@localhost ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
5cc84ad355aa: Pull complete
Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
Status: Downloaded newer image for busybox:latest

拉取私人仓库镜像:
[root@localhost ~]# docker pull kongd/dockertest:busybox
busybox: Pulling from kongd/dockertest
9758c28807f2: Pull complete
Digest: sha256:c9249fdf56138f0d929e2080ae98ee9cb2946f71498fc1484288e6a935b5e5bc
Status: Downloaded newer image for kongd/dockertest:busybox
docker.io/kongd/dockertest:busybox

拉取特定版本镜像:
[root@localhost ~]# docker pull nginx:1.14-alpine
1.14-alpine: Pulling from library/nginx
bdf0201b3a05: Pull complete
3d0a573c81ed: Pull complete
8129faeb2eb6: Pull complete
3dc99f571daf: Pull complete
Digest: sha256:485b610fefec7ff6c463ced9623314a04ed67e3945b9c08d7e53a47f6d108dc7
Status: Downloaded newer image for nginx:1.14-alpine
docker.io/library/nginx:1.14-alpine   

5、上传镜像  tag  push

上传个人仓库

 

上传准备:
打标签:
[root@localhost ~]# docker tag --help

Usage:  docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

[root@localhost ~]# docker tag busybox:latest 15291094743/hangliuc:busybox_v1

[root@localhost ~]# docker tag busybox:latest 15291094743/hangliuc:busybox_v1
[root@localhost ~]# docker push 15291094743/hangliuc:busybox_v1
The push refers to repository [docker.io/15291094743/hangliuc]
01fd6df81c8e: Pushed
busybox_v1: digest: sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee size: 527

6、查看详细信息   inspect

[root@localhost ~]# #查看镜像详细信息
[root@localhost ~]# docker inspect busybox

7、保存镜像   image save

[root@localhost ~]# #保存镜像
[root@localhost ~]# docker image save busybox:latest > busybox1.tar
[root@localhost ~]# docker image save busybox:latest -o  busybox2.tar
[root@localhost ~]# ll
total 2876
-rw-------. 1 root root    1714 Mar 12 14:18 anaconda-ks.cfg
-rw-r--r--. 1 root root 1468416 Mar 12 22:01 busybox1.tar
-rw-------. 1 root root 1468416 Mar 12 22:01 busybox2.tar

8、导入镜像   image load

[root@localhost ~]# docker image load -i busybox1.tar
Loaded image: busybox:latest

三、容器

1、启动容器

(1)格式 Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 
(2)options 常用命令选项
 -t :打开一个终端,像使用交换机一样使用容器 
-i:交互式访问 
--name:容器名字 
--network:指定网络 
--rm:容器一停,自动删除 
-d:剥离与当前终端的关系;否则会一直占据着终端 
-p: 端口号

[root@localhost ~]# docker run --name nginx1 -d -p 8888:80 nginx:1.14-alpine
32db20f30ac7fe5c350ead0b064d11984cb979e8e71c4ed2eabf97fe52ef4402
[root@localhost ~]# docker port nginx1
80/tcp -> 0.0.0.0:8888
80/tcp -> :::8888


直接交互式进入容器中:(容器未启动)

[root@localhost ~]# docker run --name box -it busybox /bin/sh
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # tree /sys/
/bin/sh: tree: not found
/ # exit


!!!run -it 保持容器运行

ctrl + p,q

2、查看容器

只显示运行中的容器

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED         STATUS         PORTS                                   NAMES
32db20f30ac7   nginx:1.14-alpine   "nginx -g 'daemon of…"   2 minutes ago   Up 2 minutes   0.0.0.0:8888->80/tcp, :::8888->80/tcp   nginx1

显示所有容器
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED              STATUS                        PORTS                                   NAMES
89f775aeb330   busybox             "/bin/sh"                About a minute ago   Exited (127) 58 seconds ago                                           box
32db20f30ac7   nginx:1.14-alpine   "nginx -g 'daemon of…"   2 minutes ago        Up 2 minutes                  0.0.0.0:8888->80/tcp, :::8888->80/tcp   nginx1

等同于 docker ps
[root@localhost ~]# docker container ls

3、停止容器

docker stop 关闭运行的容器 
docker kill 杀死运行的容器    -s:指定信号,和kill 用法一样;-9 强制停止容器

[root@localhost ~]# docker stop nginx1
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
nginx1

4、激活关闭的容器

[root@localhost ~]# docker start nginx1
nginx1
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED         STATUS         PORTS                                   NAMES
32db20f30ac7   nginx:1.14-alpine   "nginx -g 'daemon of…"   4 minutes ago   Up 2 seconds   0.0.0.0:8888->80/tcp, :::8888->80/tcp   nginx1

5、查看容器详细信息

[root@localhost ~]# docker inspect nginx1

6、删除容器  (先关闭容器,再删除容器)

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker rm nginx1
nginx1
[root@localhost ~]# docker rm box
box
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

7、对正在运行的容器进行操作

-d:在后台运行命令 
-e:设置环境变量 
-i:交互式 
-t:打开一个终端 
-u:用户名或UID

[root@localhost ~]# docker run --name nginx1 -d nginx:1.14-alpine
b016df36548790d05a440ce9b33d4aa4ddf4bb9d609e6873dc3619d2f949169a

[root@localhost ~]# docker exec -it nginx1 /bin/sh
/ # ls
bin    etc    lib    mnt    proc   run    srv    tmp    var
dev    home   media  opt    root   sbin   sys    usr

/ # exit

[root@localhost ~]# docker exec nginx1 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

8、查询容器日志

[root@localhost ~]# curl 172.17.0.2
[root@localhost ~]# docker logs nginx1
172.17.0.1 - - [13/Mar/2022:07:22:43 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"

95

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值