部署docker容器虚拟化平台(二)

Docker平台基本使用方法

1、运行一个 container并加载镜像centos,运行起来这个实例后,在实例中执行 /bin/bash命令。

  • run  运行 
  • -i      以交互模式运行容器,通常与  -t 同时使用
  • -t      为容器重新分配一个伪输入终端,通常与 -i 同时使用。
[root@docker ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    httpd               04333ce04d20        2 days ago          338.7 MB
docker.io/centos    apache              5d18d07be42a        2 days ago          444.6 MB
docker.io/nginx     latest              719cd2e3ed04        13 days ago         109.2 MB
docker.io/centos    latest              9f38484d220f        3 months ago        201.8 MB
[root@docker ~]# docker run -it docker.io/centos /bin/bash
[root@837e4187e856 /]# ls
anaconda-post.log  dev  home  lib64  mnt  proc  run   srv  tmp  var
bin                etc  lib   media  opt  root  sbin  sys  usr
[root@837e4187e856 /]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core)

2、在 container 中启动一个长久运行的进程,不断向stdin输出 hello world 。模拟一个后台运行的服务。

  • -d    后台运行容器,并返回容器ID
  • -c    后面跟待完成的命令
[root@docker ~]# docker run -d docker.io/centos:latest /bin/sh -c "while true;do echo hello world;sleep 1;done"
11503abd1df32a3e3cce32dfb6c38a143adcf1542df4c47e76bccdea7ce8e020   
[root@docker ~]# docker logs 11       //取日志
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world

查看正在运行的容器

[root@docker ~]# docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                  NAMES
11503abd1df3        docker.io/centos:latest   "/bin/sh -c 'while tr"   3 minutes ago       Up 3 minutes                               focused_jepsen
95f9d2efebc4        nginx                     "nginx -g 'daemon off"   4 hours ago         Up 4 hours          0.0.0.0:8082->80/tcp   runoob-nginx-test-web
ea0daa6a3f0a        nginx                     "nginx -g 'daemon off"   4 hours ago         Up 4 hours          0.0.0.0:8081->80/tcp   runoob-nginx-test

[root@docker ~]# docker ps -a     //-a 列出所有容器(包含沉睡/退出状态的容器)
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                     PORTS                  NAMES
11503abd1df3        docker.io/centos:latest   "/bin/sh -c 'while tr"   5 minutes ago       Up 5 minutes                                      focused_jepsen
837e4187e856        docker.io/centos          "/bin/bash"              11 minutes ago      Exited (0) 7 minutes ago                          amazing_mclean
95f9d2efebc4        nginx                     "nginx -g 'daemon off"   4 hours ago         Up 4 hours                 0.0.0.0:8082->80/tcp   runoob-nginx-test-web
ea0daa6a3f0a        nginx                     "nginx -g 'daemon off"   4 hours ago         Up 4 hours                 0.0.0.0:8081->80/tcp   runoob-nginx-test

[root@docker ~]# docker images       //列出本地所有镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    httpd               04333ce04d20        2 days ago          338.7 MB
docker.io/centos    apache              5d18d07be42a        2 days ago          444.6 MB
docker.io/nginx     latest              719cd2e3ed04        13 days ago         109.2 MB
docker.io/centos    latest              9f38484d220f        3 months ago        201.8 MB

3、杀死一个容器。 比如:杀死一个正在后台运行的容器

[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                      PORTS                  NAMES
11503abd1df3        docker.io/centos:latest   "/bin/sh -c 'while tr"   9 minutes ago       Up 9 minutes                                       focused_jepsen
837e4187e856        docker.io/centos          "/bin/bash"              15 minutes ago      Exited (0) 11 minutes ago                          amazing_mclean
95f9d2efebc4        nginx                     "nginx -g 'daemon off"   4 hours ago         Up 4 hours                  0.0.0.0:8082->80/tcp   runoob-nginx-test-web
ea0daa6a3f0a        nginx                     "nginx -g 'daemon off"   4 hours ago         Up 4 hours                  0.0.0.0:80

[root@docker ~]# docker kill 11503abd1df3
11503abd1df3

4、启动、停止、重启 container容器实例。

[root@docker ~]# docker run  -d docker.io/centos:latest /bin/sh -c "while true;do echo hello world; sleep 1; done"
b9a562800d00cd79338d15fcc48a355b9d4fdeba8059e93c6b8697f98acb1d5a
[root@docker ~]# docker ps 
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS               NAMES
b9a562800d00        docker.io/centos:latest   "/bin/sh -c 'while tr"   11 seconds ago      Up 7 seconds                            clever_goldstine
[root@docker ~]# docker stop b9a562800d00  
b9a562800d00
[root@docker ~]# docker start b9a562800d00
b9a562800d00
[root@docker ~]# docker restart b9a562800d00
b9a562800d00

[root@docker ~]# docker rm b9a562800d00
Error response from daemon: You cannot remove a running container b9a562800d00cd79338d15fcc48a355b9d4fdeba8059e93c6b8697f98acb1d5a. Stop the container before attempting removal or use -f
[root@docker ~]# docker stop b9a562800d00
b9a562800d00
[root@docker ~]# docker rm b9a562800d00
b9a562800d00

Docker镜像制作方法

  • Docker Image 的制作两种方法

    方法1:docker commit        # 保存 container 的当前状态到 image后,然后生成对应的image

    方法2:docker build            # 使用 Dockerfile 文件自动化制作 image

1、根据容器当前状态做一个image镜像:创建一个安装了apache工具的centos镜像.

[root@docker ~]# docker run -it docker.io/centos:latest /bin/bash
[root@d93933dbd09b /]# yum -y install httpd
[root@d93933dbd09b /]# exit
[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx     latest              719cd2e3ed04        13 days ago         109.2 MB
docker.io/centos    latest              9f38484d220f        3 months ago        201.8 MB
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED              STATUS                        PORTS               NAMES
d93933dbd09b        docker.io/centos:latest   "/bin/bash"              About a minute ago   Exited (0) 47 seconds ago                         nauseous_brown
11503abd1df3        docker.io/centos:latest   "/bin/sh -c 'while tr"   31 minutes ago       Exited (137) 21 minutes ago                       focused_jepsen
837e4187e856        docker.io/centos          "/bin/bash"              37 minutes ago       Exited (137) 18 minutes ago                       amazing_mclean
95f9d2efebc4        nginx                     "nginx -g 'daemon off"   4 hours ago          Exited (0) 18 minutes ago                         runoob-nginx-test-web
ea0daa6a3f0a        nginx                     "nginx -g 'daemon off"   4 hours ago          Exited (0) 18 minutes ago                         runoob-nginx-test
[root@docker ~]# docker commit d93933dbd09b docker.io/centos:apache 
sha256:d6408a990a8fafeebd31cb653866a3bd54acb3605272ecf73b6c5f09165398df
[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    apache              d6408a990a8f        13 seconds ago      338.7 MB
docker.io/nginx     latest              719cd2e3ed04        13 days ago         109.2 MB
docker.io/centos    latest              9f38484d220f        3 months ago        201.8 MB

[root@docker ~]# docker run -it docker.io/centos:apache /bin/bash
[root@c88bf8405a55 /]# rpm -qa httpd
httpd-2.4.6-89.el7.centos.x86_64

2、通过:docker build创建一个基于centos的httpd web服务器镜像。

  • 使用docker build创建镜像时,需要使用 Dockerfile 文件自动化制作 image 镜像

  • 注:Dockerfile有点像源码编译时./configure后产生的Makefile

1. 创建工作目录
[root@docker ~]# mkdir /docker-build
[root@docker ~]# cd /docker-build/
[root@docker docker-build]# touch Dockerfile //注: make自动化编译时需要Makefile文件,自动化创建docker镜像时,需要Dockerfile

2、编辑 Dockerfile
[root@docker docker-build]# vim Dockerfile 
FROM  docker.io/centos:latest  # FROM 基于哪个镜像
MAINTAINER    # MAINTAINER 镜像创建者
RUN yum -y install httpd   #RUN 安装软件用
ADD start.sh /usr/local/bin/start.sh   
ADD index.html /var/www/html/index.html
CMD /usr/local/bin/start.sh
# ADD  将文件拷贝到新产生的镜像的文件系统对应的路径。所有拷贝到新镜像中的文件和文件夹权限为0755,uid和gid为0
CMD echo hello world    #container启动时执行的命令或启动服务,但是一个Dockerfile中只能有一条CMD命令,多条则只执行最后一条CMD.

3、创建 start.sh脚本启动httpd服务和apache默认首页index.html文件
[root@docker docker-build]# echo "/usr/sbin/httpd -DFOREGROUND" > start.sh
[root@docker docker-build]# chmod +x start.sh 
[root@docker docker-build]# echo "docker image build test" > index.html

4、使用命令build来创建新的image 
[root@docker docker-build]# docker build -t docker.io/centos:httpd ./

[root@docker docker-build]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
docker.io/centos    httpd               a50cbc87a061        About a minute ago   338.7 MB
docker.io/centos    apache              d6408a990a8f        10 minutes ago       338.7 MB
docker.io/nginx     latest              719cd2e3ed04        13 days ago          109.2 MB
docker.io/centos    latest              9f38484d220f        3 months ago         201.8 MB

Docker Image 的发布

  • 方法1:Save Image To TarBall

  • 方法2:Push Image To Docker Hub

方法1:Save Image To TarBall

语法:docker save -o 导出的镜像名.tar  本地镜像名:镜像标签
例:
[root@docker ~]# docker save -o docker.io-centos-httpd-docker-image.tar docker.io/centos:httpd

[root@docker docker-build]# ll -h
总用量 332M
-rw-r--r-- 1 root root  180 6月  24 14:30 Dockerfile
-rw------- 1 root root 332M 6月  24 14:41 docker.io-centos-httpd-docker-image.tar
-rw-r--r-- 1 root root   24 6月  24 14:31 index.html
-rwxr-xr-x 1 root root   29 6月  24 14:31 start.sh

使用导入本地镜像

[root@docker docker-build]# docker rmi docker.io/centos:httpd 
[root@docker docker-build]# docker load -i docker.io-centos-httpd-docker-image.tar

方法2:Push Image To Docker Hub  发布到外网

[root@docker ~]# docker tag docker.io/centos:apache 账号/public:apache
[root@docker ~]# docker login -u 账号 -p 密码
Login Succeeded
[root@docker ~]# docker push everycool/public:apache
The push refers to a repository [docker.io/everycool/public]
c5c697cbd671: Pushed 
d69483a6face: Pushed 
apache: digest: sha256:710c331404c91a45d7f1dd6627a3f750eee6379f6e48ab71fce7bb627d6fd18b size: 741

Container 容器端口映射

[root@docker ~]# docker run -d -p 81:80 docker.io/centos:apache 
60e26283967a0b75c448a834b155695471677eeb9fa05c626ac37de35dea7a0d

##  -p 物理机的80端口:容器实例的80端口  ,把容器中的80端口映射到物理机上的80端口

[root@docker ~]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                NAMES
c72fdb8da879        docker.io/centos:httpd   "/bin/bash -c /usr/lo"   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   sleepy_newton

[root@docker ~]# netstat  -antup | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN   50768/docker-proxy-

访问正在运行的 container 容器实例

  • 语法: docker exec -it /bin/bash

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

 

你可能感兴趣的:(虚拟化,Docker)