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

发布了27 篇原创文章 · 获赞 14 · 访问量 524

猜你喜欢

转载自blog.csdn.net/weixin_45636702/article/details/104002292