几个命令教会你docker基本操作

首先使用yum将docker-ce部署到本地,部署教程可参照CentOS7部署docker-ce

部署完成后首先启动docker

启动docker

[root@localhost ~]# systemctl start docker

刚刚部署好docker,是没有任何镜像的

查看本地镜像

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED

我们可以到docker的官方镜像仓库中查找我们需要的镜像,比如我们需要一个nginx的镜像,可以按下面命令操作

搜索镜像

[root@localhost ~]# docker search nginx
NAME                               DESCRIPTION
nginx                              Official build of Nginx.
jwilder/nginx-proxy                Automated Nginx reverse proxy for doc
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM cap
linuxserver/nginx                  An Nginx container, brought to you by
bitnami/nginx                      Bitnami nginx Docker Image
tiangolo/nginx-rtmp                Docker image with Nginx using the ngi
jc21/nginx-proxy-manager           Docker container for managing Nginx p
……省略……

为了支持docker,现在如nginx、redis、mysql等软件的官方都已经推出了官方的镜像并已推送到docker的官方镜像仓库中,大家可以按需下载。

下面我们来下载一个nginx的镜像

下载镜像

[root@localhost ~]# docker pull nginx:latest
latest: Pulling from library/nginx
c499e6d256d6: Pull complete
74cda408e262: Pull complete
ffadbd415ab7: Pull complete
Digest: sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        8 days ago          127MB

镜像已下载成功,我们使用这个镜像创建第一个容器

创建容器

[root@localhost ~]# docker create --name mynginx nginx:latest
e91f7d73b7c005e6512f971084b46bcf8d817a13915ea32cb844884467f5ff88

–name 参数指定的是容器的名字

如何查看刚刚创建好的容器呢?

查看容器

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS            PORTS               NAMES
e91f7d73b7c0        nginx:latest        "nginx -g 'daemon of…"   2 seconds ago       Created                                mynginx

可以看到,容器的状态还是Created,并没有开始运行

运行容器

[root@localhost ~]# docker start mynginx
mynginx
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS            PORTS               NAMES
e91f7d73b7c0        nginx:latest        "nginx -g 'daemon of…"   47 seconds ago      Up 21 seconds       80/tcp              mynginx

这里使用docker ps,可以查看正在运行的容器

查看容器信息

[root@localhost ~]# docker inspect mynginx

docker inspect 命令将会以json格式输出有关容器的详细信息,包括容器id、容器镜像、容器状态、容器IP地址等等等等,这里展示一下镜像的cmd信息:

            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "nginx",

可以看到nginx镜像的默认命令是"nginx -g daemon off"

而如果我们需要指定容器去执行特定的命令,可以使用下面的操作

容器执行命令

[root@localhost ~]# docker exec -it mynginx /bin/bash
root@49bafcb653c1:/# pwd
/
root@49bafcb653c1:/# ls
bin   dev  home  lib64	mnt  proc  run	 srv  tmp  var
boot  etc  lib	 media	opt  root  sbin  sys  usr
root@49bafcb653c1:/# exit
exit

这里使用了docker exec命令,两个参数的作用如下:

  -i, --interactive          Keep STDIN open even if not attached
      --privileged           Give extended privileges to the command
  -t, --tty                  Allocate a pseudo-TTY

可见,加-it参数可以保持终端输入,而最后的/bin/bash则是指定容器执行的命令

所以,docker exec -it 也是一种进入容器内部的常用方法

接下来我们停止运行的mynginx容器

停止容器

[root@localhost ~]# docker stop mynginx
mynginx

删除容器

[root@localhost ~]# docker rm mynginx
mynginx

可以使用docker ps -a查看,我们的mynginx容器已经被删除掉了

按照我们上面的步骤,启动一个容器未免太过麻烦,先是拉取镜像,然后创建容器,再启动容器……有没有一步到位的办法?有的!

启动容器 docker run

[root@localhost ~]# docker run -it --name mynginx2 nginx:latest /bin/bash
root@3ccf9452af41:/# ls
bin   dev  home  lib64	mnt  proc  run	 srv  tmp  var
boot  etc  lib	 media	opt  root  sbin  sys  usr
root@3ccf9452af41:/# pwd
/

这里使用docker run命令,直接启动容器,若指定的镜像在本地不存在,将自动向默认的仓库进行拉取,拉取到本地之后,直接启动容器。

-it参数与docker exec命令的-it参数作用一样,

–name指定的是容器名字

/bin/bash相信大家也能猜到了,就是容器执行的命令

修改镜像标签

假如现在局域网中有一个私有镜像仓库,你将nginx:latest这个镜像上传上去,那么第一步就是先修改镜像的标签

[root@localhost ~]# docker tag nginx:latest 192.168.218.5/library/nginx:latest
[root@localhost ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
192.168.218.5/library/nginx   latest              ed21b7a8aee9        8 days ago          127MB
nginx                         latest              ed21b7a8aee9        8 days ago          127MB

修改过后就可以上传

上传镜像

[root@localhost ~]# docker push 192.168.218.5/library/nginx:latest

注意,上传成功的前提是,局域网中192.168.218.5的主机已经配置了私有仓库,并且仓库中存在library这个项目,噢对了本地的docker还需要使用用户认证事先登陆私有仓库,使用的是docker login命令,这里不展开细说。

删除镜像

[root@localhost ~]# docker rmi 192.168.218.5/library/nginx:latest
Untagged: 192.168.218.5/library/nginx:latest
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        8 days ago          127MB

制作镜像

在实际环境中,可能你用nginx:latest这个镜像创建了一个容器,并在容器上部署了自己的网页,现在你想要把它打包成镜像,只需要这么做:

[root@localhost ~]# docker commit mynginx 192.168.218.5/library/nginx:v1
sha256:25155573f03a9b82b2dfb817409bc220c15e1d9186a027fb932699e1e887c5f2
[root@localhost ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
192.168.218.5/library/nginx   v1                  25155573f03a        4 seconds ago       127MB
nginx                         latest              ed21b7a8aee9        8 days ago          127MB

使用docker commit命令,是将容器作为模板制作一个新的镜像,我在这里将新的镜像命名为192.168.218.5/library/nginx:v1,可以使用docker push命令将其推送到你的私有仓库中

也许,你认为建立一个私有镜像仓库以及后期对其维护会消耗你的人力和精力以致于你不想搭建一个私有仓库;又或许你的局域网正好有一个ftp服务器,那能不能把镜像作为文件存放在ftp当中呢?答案是可以的。

导出镜像

[root@localhost ~]# docker save -o nginxv1 192.168.218.5/library/nginx:v1
[root@localhost ~]# ls | grep nginxv1
nginxv1

这里我以刚刚创建的192.168.218.5/library/nginx:v1镜像为例,我使用docker save命令将其导出为一个文件,命名为nginxv1,而我们浏览文件系统可以看到当前目录下正有一个nginxv1的文件!

导入镜像

很幸运我们已经把想要的镜像制作成文件的形式传送到了另外一台服务器(无论是使用 ftp还是其他的形式),这个时候或许你想试着以这个镜像部署容器,那么第一步就是将还是文件形式的镜像文件导入到docker中:

[root@localhost ~]# docker load < nginxv1
c3a984abe8a8: Loading layer  72.48MB/72.48MB
99134ec7f247: Loading layer  58.11MB/58.11MB
d37eecb5b769: Loading layer  3.584kB/3.584kB
ca6e78f77fd3: Loading layer  6.656kB/6.656kB
Loaded image: 192.168.218.5/library/nginx:v1

将nginxv1重定向给docker load命令,我们就将文件形式的192.168.218.5/library/nginx:v1镜像导入到了docker当中:

[root@localhost ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
192.168.218.5/library/nginx   v1                  25155573f03a        11 minutes ago      127MB

当然通过文件形式确实是一个在多主机之间共享镜像的方法,但现在更普遍的是使用像harbor的私有镜像仓库,关注笔者的后续文章,将继续更新harbor私有仓库的搭建及使用。

感谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值