Docker容器(二)

1、docker容器简单操作
docker search 镜像名 #搜索镜像、如docker search nginx
docker pull alpine  #拉取镜像、alpine是比较小的镜像
docker images #查看本机存在的镜像
docker save nginx >/tmp/nginx.tar.gz #镜像导出
docker load nginx </tmp/nginx.tar.gz #镜像导入

vim /usr/lib/systemd/system/docker.service

更换存储目录
‐‐graph=/opt/docker  #如opt是新挂载的硬盘
dns服务
默认docker是采用宿主机的dns 可以采用‐‐dns=xxxx的方式制定
docker daemon ‐‐help 这里可以查看到所有支持的参数


docker run alpine sh #启动镜像alpine


[root@linux-node1 ~]# docker run -it alpine sh #加上-it、有可运行的终端
/ #                      #ctrl不放手按p跟q键盘退出、如果直接退出容器也就退出了
/ # [root@linux-node1 ~]# 


[root@linux-node1 ~]# docker ps  #显示运行的容器
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
a85e6f7b48ba        alpine              "sh"                About a minute ago   Up About a minute                       zealous_euclid



[root@linux-node1 ~]# docker ps -a #显示所有容器、不管运行还是没有运行的
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES
a85e6f7b48ba        alpine              "sh"                About an hour ago   Up About an hour                                   zealous_euclid
4edd985edadf        alpine              "sh"                About an hour ago   Exited (0) About an hour ago                       nostalgic_kirch
7d90de5415a3        alpine              "sh"                About an hour ago   Exited (0) About an hour ago                       adoring_bose


[root@linux-node1 ~]# docker rm -f a85e6f7b48ba #删除正在运行的容器加-f、对应容器ID
a85e6f7b48ba



[root@linux-node1 ~]# docker run -it --name mynginx nginx  #启动nginx容器、--name是命名


[root@linux-node1 ~]# docker ps #打开另外一个终端查看
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
074ab44cb48f        nginx               "nginx -g 'daemon ..."   About a minute ago   Up About a minute   80/tcp              mynginx

[root@linux-node1 ~]# docker inspect mynginx  #查看容器的详细信息、IP、网关等
[root@linux-node1 ~]# curl 172.17.0.2  #curl IP

[root@linux-node1 ~]# docker run -it --name mynginx nginx #刚才的命令显示如下访问信息
172.17.0.1 - - [28/Jul/2018:13:44:47 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"

#进入容器
#生产场景是不使用docker attach的,需要我们使用nsenter这个工具,这个工具包含在util-linux软件包里面
[root@linux-node1 ~]# yum install util-linux -y #Centos7默认最小化已经安装

#通过nsenter就可以进入容器,但是nsenter是通过pid进入容器里,所以我们需要知道容器的pid。我们可以通过docker inspect来获取到pid
#每一个容器都有.State.Pid,所以这个命令除了容器的id需要我们根据docker ps -a去查找
[root@linux-node1 ~]# docker inspect -f "{{ .State.Pid }}" mynginx
16314

#nsenter --target上面查到的进程id --mount --uts --ipc --net --pid #输入该命令便进入到容器中
[root@linux-node1 ~]# nsenter -t 16314 -m -u -i -n -p
mesg: ttyname failed: No such file or directory

解释nsenter指令中进程id之后的参数的含义:

* –mount参数是进去到mount namespace中 
* –uts参数是进入到uts namespace中 
* –ipc参数是进入到System V IPC namaspace中 
* –net参数是进入到network namespace中 
* –pid参数是进入到pid namespace中 
* –user参数是进入到user namespace中

#exit(不会让容器关闭)
root@3569ef1059f0:/# exit 
logout

[root@linux-node1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
0be494e9e192        centos              "bash"                   34 hours ago        Up 34 hours                             serene_davinci
3569ef1059f0        nginx               "nginx -g 'daemon ..."   34 hours ago        Up 34 hours         80/tcp              mynginx

#因为每次进入容器都需要输入那两条命令,所以我们可以写一个脚本来获取。 

[root@linux-node1 ~]# cat docker_in.sh 
#!/bin/bash

docker_in(){
  NAME_ID=$1
  PID=$(docker inspect -f "{{ .State.Pid }}" $NAME_ID)
  nsenter -t $PID -m -u -i -n -p
}
docker_in $1

[root@linux-node1 ~]# sh docker_in.sh mynginx



#另一种exec方式进入到容器
[root@linux-node1 ~]# docker exec -it  mynginx sh #-i 让docker的标准输入打开{input},-t 让docker分配一个伪终端
# 
# ls
bin  boot  dev    etc  home  lib    lib64  media  mnt  opt    proc  root  run  sbin  srv  sys  tmp  usr  var

#‐d参数守护状态运行
[root@linux-node1 ~]# docker run -it -d --name mynginx nginx
3569ef1059f0fa123b45c12036f1b55a37d78ea8f4a1484434f107cbc5c1167b

#查看容器运行产生的信息
docker logs
docker logs ‐f

#容器的停止stop    以及kill
docker stop  容器名称或者ID
docker kill  容器名称或者ID


#如何杀死所有正在运行的容器
docker kill $(docker ps ‐a ‐q)



2、docker容器镜像操作
[root@linux-node1 ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ... 
latest: Pulling from docker.io/library/centos
7dc0dca2b151: Pull complete 
Digest: sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322
Status: Downloaded newer image for docker.io/centos:latest

#进到容器里边
[root@linux-node1 ~]# docker run -it centos bash
[root@0be494e9e192 /]# ls
bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var


#优化配置、配置ali yun源
[root@0be494e9e192 /]# cd /etc/yum.repos.d/ 
[root@0be494e9e192 yum.repos.d]# ls
CentOS-Base.repo  CentOS-CR.repo  CentOS-Debuginfo.repo  CentOS-Media.repo  CentOS-Sources.repo  CentOS-Vault.repo  CentOS-fasttrack.repo

[root@0be494e9e192 yum.repos.d]# yum -y install wget
[root@0be494e9e192 yum.repos.d]# rm -rf *
[root@0be494e9e192 yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@0be494e9e192 yum.repos.d]# yum -y install epel-release
[root@0be494e9e192 yum.repos.d]# yum makecache

#Nginx 镜像
[root@0be494e9e192 yum.repos.d]# yum -y install nginx

#增加daemon off; 默认Nginx是后台运行的
[root@0be494e9e192 yum.repos.d]# vi /etc/nginx/nginx.conf

daemon off;

[root@0be494e9e192 yum.repos.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@linux-node1 ~]# docker commit  -m 'add nginx images' mynginx w787815/my_nginx
sha256:8a60ff100595f27d4d790af74f15e2121edb55f6100acc92c629fee659969b69
[root@linux-node1 ~]# docker images  #没打TAG号、默认是latest
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
w787815/my_nginx    latest              8a60ff100595        9 seconds ago       109 MB
docker.io/nginx     latest              c82521676580        3 days ago          109 MB
docker.io/alpine    latest              11cd0b38bc3c        3 weeks ago         4.41 MB
docker.io/centos    latest              49f7960eb7e4        7 weeks ago         200 MB

#生产环境打TAG是很有必要的、可以根据不同版本进行测试
[root@linux-node1 ~]# docker commit  -m 'add nginx images' mynginx w787815/my_nginx:v1  
sha256:99805914d177ba4e9ea0632679fba9a16425d0229d2a14649be57457cdce2260
[root@linux-node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
w787815/my_nginx    v1                  99805914d177        2 seconds ago       109 MB
w787815/my_nginx    latest              8a60ff100595        32 seconds ago      109 MB
docker.io/nginx     latest              c82521676580        3 days ago          109 MB
docker.io/alpine    latest              11cd0b38bc3c        3 weeks ago         4.41 MB
docker.io/centos    latest              49f7960eb7e4        7 weeks ago         200 MB

[root@linux-node1 ~]# docker run -d --name my_nginx w787815/my_nginx nginx #后面的nginx是执行的命令
[root@linux-node1 ~]# docker ps #没有绑定什么端口、上面的命令执行会退出

#容器不是一个虚拟机,因为他就是一个进程,如果我们退出,这个进程就退出了。 
[root@linux-node1 ~]# docker ps -a
以上这种镜像制作时比较简单的、生产中不这样做、知道步骤就好了、比较麻烦


docker的官方仓库地址是https://hub.docker.com/,可以把创建好的镜像上传上去
[root@linux-node1 ~]# 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: w787815
Password: 
Login Succeeded

[root@linux-node1 ~]# docker images #获取制作的镜像ID
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
w787815/my_nginx    v1                  99805914d177        41 minutes ago      109 MB
w787815/my_nginx    latest              8a60ff100595        42 minutes ago      109 MB
docker.io/nginx     latest              c82521676580        4 days ago          109 MB
docker.io/alpine    latest              11cd0b38bc3c        3 weeks ago         4.41 MB
docker.io/centos    latest              49f7960eb7e4        7 weeks ago         200 MB
[root@linux-node1 ~]# docker tag 99805914d177  docker.io/w787815/my_nginx
[root@linux-node1 ~]# docker push docker.io/w787815/my_nginx  #将镜像上传的远程仓库
The push refers to a repository [docker.io/w787815/my_nginx]
9f654061fd65: Pushed 
08d25fa0442e: Mounted from library/nginx 
a8c4aeeaa045: Mounted from library/nginx 
cdb3f9544e4c: Mounted from library/nginx 
latest: digest: sha256:35cc3685d6348e181ee8c8c0731392dd5fc332ea62b25947fd708eb034f68c42 size: 1155

猜你喜欢

转载自www.cnblogs.com/w787815/p/9389134.html
今日推荐