Docker的基本操作命令

Docker的基本操作命令:

 

//查找镜像:

[root@localhost ~]# docker search mysql //默认在docker hub公共仓库进行查找

 

//拉取镜像,下载镜像:

[root@localhost ~]# docker pull busybox

 

//导出镜像到本地:

[root@localhost ~]# docker save -o busybox.tar busybox:latest (docker save > busybox.tar busybox:latest)

 

//查看本地镜像:

[root@localhost ~]# docker images (docker image ls)

PS:虽然我们查看到镜像标签位latest(最新的),但并不表示它一定是最新的,而且镜像如果没有写标签,默认是以latest为标签

 

//删除镜像:

[root@localhost ~]# docker rmi busybox:latest

 

//根据本地镜像包导入镜像:

[root@localhost ~]# docker load -i busybox.tar (docker load < busybox.tar )

 

//查看容器--正在运行的:

[root@localhost ~]# docker ps

 

//查看所有的容器:

[root@localhost ~]# docker ps -a

 

//删除容器:

[root@localhost ~]# docker rm centos [CONTAINER ID/容器名称]

 

//停止容器运行:

[root@localhost ~]# docker stop centos

 

//启动容器:

[root@localhost ~]# docker start centos

PS:开启容器后记得去验证一下容器是否开启

 

//强制删除容器:

[root@localhost ~]# docker rm centos -f

 

//强制删除所有容器(生产环境严禁使用):

[root@localhost ~]# docker ps -a -q | xargs docker rm -f

----------------------------------------------------------------------------------------------

[root@localhost ~]# docker ps -a -q | xargs docker start -f //开启所有容器

[root@localhost ~]# docker ps -a -q | xargs docker stop -f //关闭所有容器

//重启一个容器:

[root@localhost ~]# docker restart test2

 

//运行一个容器:

[root@localhost ~]# docker run -it --name test1 centos:7

-i:交互

-t:伪终端

-d(daemon):后台运行

--name:给容器命名

--restart=always:始终保持运行(随着docker开启而运行)

 

[root@localhost ~]# docker create -it --name test3 centos:7 //不常用

 

//进入一个容器:

[root@localhost ~]# docker exec -it test2 /bin/bash

[root@localhost ~]# docker attach test2

 

区别:

exec进入的方式需要添加-i,-t选项,后面还需要给容器一个shell环境,但attach就不需要这么麻烦

exec进入的方式:如果exit退出,容器仍然保持运行

attach:如果执行exit退出,容器会被关闭,如果想要保持容器不被关闭,可以使用键盘:ctrl+p ctrl+q可以实现

 

本质上区别:

exec进入的方法,会产生新的进程

attach进入的方法,不会产生新的进程

 

Docker的基本操作逻辑:

小实验:

基于centos:7镜像运行一个容器,并且在这个容器内部署nginx服务

1)下载镜像:

[root@localhost ~]# docker pull centos:7

2)运行容器:

[root@localhost ~]# docker run -itd --name webapp --restart=always centos:7

3)进入容器,开始部署nginx服务://将nginx包导入到容器内:


[root@localhost ~]# docker cp nginx-1.14.0.tar.gz
[root@localhost ~]# docker exec  -it  webapp  /bin/bash
[root@01b870908942 ~]# tar zxf nginx-1.14.0.tar.gz   
[root@01b870908942 ~]# cd  nginx-1.14.0
[root@01b870908942 nginx-1.14.0]# yum  -y  install  gcc  pcre  pcre-devel  openssl  openssl-devevl zlib zlib-devel
[root@01b870908942 nginx-1.14.0]# useradd  -s  /sbin/nologin  nginx
[root@01b870908942 nginx-1.14.0]# ./configure  --prefix=/usr/local/nginx  --user=nginx  --group=nginx
[root@01b870908942 nginx-1.14.0]# make  &&  make  install
[root@01b870908942 nginx-1.14.0]# ln  -s  /usr/local/nginx/sbin/*   /usr/local/sbin/
[root@01b870908942 nginx-1.14.0]# nginx
[root@01b870908942 nginx-1.14.0]# cd  /usr/local/nginx/html/
[root@01b870908942 html]# echo  This  is  a  resrweb  in  container  >  index.html
[root@01b870908942 html]# curl  127.0.0.1
This is a resrweb in container

//把容器制作成镜像:(可移植性)

[root@localhost ~]# docker commit webapp myweb:12-10

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值