启动一个容器并且进到里面,退出后,容器结束

[root@bogon ~]# docker run --name mynginx -it nginx

启动一个容器,退出后自动删除
[root@bogon stress]# docker run -it --rm stress --cpu 1 后台启动一个容器,启动后,退出容器,容器仍然是up状态 [root@bogon ~]# docker run -d --name mynginx nginx
[root@lab3 ~]# docker run -d -p 8085:8081 --restart=always --name nexus3 sonatype/nexus3 #注意8081是服务真实的端口 过滤某个容器
[root@bogon ~]# docker ps -a | grep mynginx 启动已有的容器:
[root@bogon ~]# docker start 3ec6e022d3ec 只看最后一个容器
[root@bogon ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
66cb3c773f86 nginx "nginx -g 'daemon ..." 16 seconds ago Up 15 seconds 0.0.0.0:91->80/tcp mynginx2 进入容器: #进入后再退到linux环境中,容器仍然运行,生产中提倡 [root@bogon yum.bak]# yum install util-linux -y
[root@bogon ~]# docker start 3ec6e022d3ec
3ec6e022d3ec
[root@bogon ~]# docker inspect --format "{{.State.Pid}}" mynginx2 #上面启动后要速度,否则 docker 又exited
32850
[root@bogon ~]# nsenter --target 32850 --mount --uts --ipc --net --pid
[root@3ec6e022d3ec /]# exit
logout
[root@bogon ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78a40f56544c nginx "/bin/bash" About an hour ago Exited (0) About an hour ago mydockre1
3ec6e022d3ec nginx "/bin/bash" About an hour ago Up 2 minutes nginx
863c93fc46fb nginx "echo 'hello word'" About an hour ago Exited (0) About an hour ago unruffled_varahamihira 脚本进入容器:
[root@bogon ~]# cat c.sh
#!/bin/bash
CNAME=$1
CPID=$(docker inspect --format "{{.State.Pid}}" $CNAME)
nsenter --target $CPID --mount --uts --ipc --net --pid
[root@bogon ~]# docker start nginx
nginx
[root@bogon ~]# ./c.sh mynginx
[root@3ec6e022d3ec /]# 停止容器:
[root@bogon ~]# docker stop 3ec6e022d3ec
3ec6e022d3ec 删除容器: [root@bogon ~]# docker stop acdde4415e79
[root@bogon ~]# docker rm acdde4415e79 强制删除容器 docker rmi fc7a05ff1928 -f 网络访问; [root@bogon ~]# yum install -y bridge-utils
[root@bogon ~]# brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.02427c67ad94 no veth67067d2
vethc4fa94d
[root@bogon ~]# iptables -t nat -l -n
iptables v1.4.21: unknown option "-l"
Try `iptables -h' or 'iptables --help' for more information.
[root@bogon ~]# iptables -t nat -L -n
target prot opt source destination
MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0 随机映射端口:
[root@bogon ~]# docker run -d -P --name mynginx1 nginx
[root@bogon ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
60ffbffe8f27 nginx "nginx -g 'daemon ..." About a minute ago Up About a minute 0.0.0.0:32768->80/tcp mynginx1 访问nginx
http://192.168.43.71:32768/ Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com. Thank you for using nginx. 指定映射端口:
[root@bogon ~]# docker run -d -p 91:80 --name mynginx2 nginx
66cb3c773f8610563fb382cf5045792478225affbb126258802b7a193a964e12
[root@bogon ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
66cb3c773f86 nginx "nginx -g 'daemon ..." 16 seconds ago Up 15 seconds 0.0.0.0:91->80/tcp mynginx2 访问nginx
http://192.168.43.71:91/ Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com. Thank you for using nginx. 数据管理: 1、数据卷: [root@bogon ~]# docker run -it --name volume-test1 -h nginx -v /data nginx
[root@bogon ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7f26eddfe04 nginx "nginx -g 'daemon ..." About a minute ago Exited (0) 12 seconds ago volume-test1
[root@bogon ~]# docker rm b7f26eddfe04
b7f26eddfe04
[root@bogon ~]# docker run -it --name volume-test1 -h centos -v /data centos #-h 指定版本
[root@centos /]#
[root@centos /]#exit
[root@bogon ~]# docker inspect -f {{.Volumes}} volume-test1
Template parsing error: template: :1:2: executing "" at <.Volumes>: map has no entry for key "Volumes"
[root@bogon ~]# docker inspect volume-test1 | grep Mounts
"Mounts": [
[root@bogon ~]# docker inspect volume-test1 | grep Mounts -A 10
"Mounts": [
{
"Type": "volume",
"Name": "92abd271864afe570792711fda8a5755fff219dfeabb6a59f58f02c7802c903c",
"Source": "/var/lib/docker/volumes/92abd271864afe570792711fda8a5755fff219dfeabb6a59f58f02c7802c903c/_data",
"Destination": "/data",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
} [root@bogon ~]# cd /var/lib/docker/volumes/92abd271864afe570792711fda8a5755fff219dfeabb6a59f58f02c7802c903c/_data
[root@bogon _data]# mkdir hello
[root@bogon _data]# ls
hello [root@bogon _data]# docker attach fd4d63501e40
[root@centos /]# ls /data/
hello
[root@centos /]# cd /data/
[root@centos data]# mkdir shanghai
[root@centos data]# ls
hello shanghai 指定目录: [root@bogon ~]# docker run -it --name volume-test2 -h centos -v /opt:/opt centos #本地opt:容器opt
[root@centos /]# ls /opt/
centos.yum.bak 2、数据库容器:容器之间共享 [root@bogon ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
becd2c381f3e centos "/bin/bash" 10 minutes ago Up 10 minutes volume-test2
fd4d63501e40 centos "/bin/bash" 42 minutes ago Exited (127) 8 minutes ago volume-test1
66cb3c773f86 nginx "nginx -g 'daemon ..." 2 hours ago Up 2 hours 0.0.0.0:91->80/tcp mynginx2
60ffbffe8f27 nginx "nginx -g 'daemon ..." 2 hours ago Up 2 hours 0.0.0.0:32768->80/tcp mynginx1
4351a4435e22 nginx "nginx -g 'daemon ..." 3 hours ago Up 3 hours 80/tcp nginx
78a40f56544c centos "/bin/bash" 4 hours ago Exited (0) 4 hours ago mydockre1
3ec6e022d3ec centos "/bin/bash" 4 hours ago Up 3 hours mydocker
863c93fc46fb centos "echo 'hello word'" 4 hours ago Exited (0) 4 hours ago unruffled_varahamihira [root@bogon ~]# docker run -it --name volume-test4 -h centos --volumes-from volume-test1 centos
[root@centos /]# ls /data/
hello shanghai 手动构建docker镜像 [root@bogon ~]# docker run --name centos-man -it centos
[root@c10e6c649a66 /]#
[root@c10e6c649a66 /]# yum install -y wget gcc gcc-c++ make openssl-devel
[root@c10e6c649a66 /]# wget http://nginx.org/download/nginx-1.9.9.tar.gz
[root@c10e6c649a66 /]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz
[root@c10e6c649a66 /]# useradd -s /sbin/nologin -M www
[root@c10e6c649a66 nginx-1.9.9]# mkdir /usr/local/nginx
[root@c10e6c649a66 nginx-1.9.9]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre=../pcre-8.42
[root@c10e6c649a66 nginx-1.9.9]# make
[root@c10e6c649a66 nginx-1.9.9]# make install
[root@c10e6c649a66 nginx-1.9.9]# vi /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot. touch /var/lock/subsys/local
#/usr/local/nginx/sbin/nginx #这句话不要添加,否则容器启动后就会退出
[root@c10e6c649a66 nginx-1.9.9]# vi /usr/local/nginx/conf/nginx.conf
daemon off; #需要添加这句话,在前台运行
#user nobody;
worker_processes 1; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid;
[root@c10e6c649a66 nginx-1.9.9]# exit
logout
[root@bogon ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c10e6c649a66 centos "/bin/bash" 13 hours ago Up About an hour centos-man
[root@bogon ~]# docker commit -m "my nginx" c10e6c649a66 zhuzihao/my-nginx:v1
sha256:dcefc0f1811797422d5d69745e2b92d72c5052026208f3dc658ec951e157eab1
[root@bogon ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhuzihao/my-nginx v1 dcefc0f18117 20 seconds ago 393MB
nginx latest 5699ececb21c 4 days ago 109MB
centos latest 49f7960eb7e4 3 weeks ago 200MB
[root@bogon ~]# docker run -d -p 95:80 zhuzihao/my-nginx:v1 /usr/local/nginx/sbin/nginx
ced0276ad948614b4ff5866e7abf5e25185d0b8bbd8c45465df93c0045966d4c
[root@bogon ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ced0276ad948 zhuzihao/my-nginx:v1 "/usr/local/nginx/..." 9 seconds ago Up 9 seconds 0.0.0.0:95->80/tcp clever_beaver Dockerfile构建dicker镜像 [root@bogon ~]# mkdir /opt/docker-file
[root@bogon ~]# cd /opt/docker-file/
[root@bogon docker-file]# mkdir nginx
[root@bogon docker-file]# cd nginx/
[root@bogon nginx]# wget http://nginx.org/download/nginx-1.9.9.tar.gz
[root@bogon nginx]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz
[root@bogon nginx]# vi Dockerfile #必须大写
#this is my dockerfile
#verion 1.0
#auther zi hao #Base imags
FROM centos :默认从本地 #MAINTAINER
MAINTAINER zi hao #ADD
ADD pcre-8.42.tar.gz /usr/local/src
ADD nginx-1.9.9.tar.gz /usr/local/src #RUN
RUN yum install -y wget gcc gcc-c++ make openssl-devel
RUN useradd -s /sbin/nologin -M www #WORKDIR
WORKDIR /usr/local/src/nginx-1.9.9
RUN ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.42 && make && make install
RUN echo "daemon off;" >> /usr/local/nginx/nginx.conf ENV PATH /usr/local/ngix/sbin:$PATH
EXPOSE 80
CMD ["nginx"] [root@bogon nginx]# docker build -t nginx-file:v1 /opt/docker-file/nginx/ #只写docker文件的路径,不用带上dockerfile
[root@bogon nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-file v1 5def32732043 5 minutes ago 390MB
zhuzihao/my-nginx v1 a743f6bd0741 About an hour ago 393MB
nginx latest 5699ececb21c 4 days ago 109MB
centos latest 49f7960eb7e4 3 weeks ago 200MB Docker核心原理-资源隔离和限制 [root@bogon ~]# cd /opt/docker-file/
[root@bogon docker-file]# ls
nginx
[root@bogon docker-file]# mkdir stress
[root@bogon docker-file]# cd stress/
[root@bogon stress]# wget http://mirrors.aliyun.com/repo/epel-7.repo
[root@bogon stress]# vi Dockerfile
FROM centos
ADD epel-7.repo /etc/yum.repos.d/
RUN yum install stress && yum clean all
ENTRYPOINT ["stress"] [root@bogon stress]# docker build -t stress .
[root@bogon stress]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
stress latest 244ae61acc08 7 minutes ago 340MB
nginx-file v1 5def32732043 9 hours ago 390MB
zhuzihao/my-nginx v1 a743f6bd0741 10 hours ago 393MB
nginx latest 5699ececb21c 4 days ago 109MB
centos latest 49f7960eb7e4 3 weeks ago 200MB
registry latest d1fd7d86a825 5 months ago 33.3MB [root@bogon stress]# docker run -it --rm stress --cpu 1
[root@bogon ~]# top
[root@bogon ~]# docker run -it --rm -m 256m stress --vm 1 --vm-bytes 200m --vm-hang 0
[root@bogon stress]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8932a850dd60 stress "stress --vm 1 --v..." 2 minutes ago Up 2 minutes festive_jennings
[root@bogon stress]# docker exec 8932a850dd60 docker网络和registry [root@bogon ~]# docker pull registry
[root@bogon ~]# brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.0242f82b6209 no veth62318cf
vethdbb7907
[root@bogon ~]# iptables -t nat -L -n
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL Chain INPUT (policy ACCEPT)
target prot opt source destination Chain OUTPUT (policy ACCEPT)
target prot opt source destination
DOCKER all -- 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0 Chain DOCKER (2 references)
target prot opt source destination
RETURN all -- 0.0.0.0/0 0.0.0.0/0
[root@bogon ~]# vi /usr/lib/systemd/system/docker.service
在12行后面增加 --insecure-registry ip:5000
[root@bogon ~]# systemctl daemon-reload
[root@bogon ~]# systemctl restart docker
[root@bogon ~]# docker run -d -p 5000:5000 registry
426f87a37407473caf505c8817903515b86d1032bedc48367a8769a4f11e5355
[root@bogon ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
426f87a37407 registry "/entrypoint.sh /e..." 7 seconds ago Up 5 seconds 5000/tcp heuristic_swirles
[root@bogon ~]# docker tag nginx 192.168.43.71:5000/test/ng:v1
[root@bogon ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
stress latest 244ae61acc08 About an hour ago 340MB
nginx-file v1 5def32732043 10 hours ago 390MB
zhuzihao/my-nginx v1 a743f6bd0741 11 hours ago 393MB
192.168.43.71:5000/test/ng v1 5699ececb21c 4 days ago 109MB
nginx latest 5699ececb21c 4 days ago 109MB
centos latest 49f7960eb7e4 3 weeks ago 200MB
registry latest d1fd7d86a825 5 months ago 33.3MB
[root@bogon ~]# docker push 192.168.43.71:5000/test/ng:v1 从另一台机子上pull镜像
[root@bogon ~]# docker pull 192.168.43.71:5000/test/ng

docker笔记、常遇问题、常用命令的更多相关文章

  1. docker学习笔记二:常用命令

    docker学习笔记二:常用命令 查看docker常用命令 docker --help 返回结果如下: 其中常用的命令如下: 1.image相关操作 展示所有的image: 删除image: rmi ...

  2. windows下docker的安装及常用命令学习

    docker search 镜像名 本文主要介绍Docker在Windows下的安装.关于Docker的介绍和文档在其官网中可以找到:http://www.docker.com .安装环境:Windo ...

  3. Oracle学习笔记—数据字典和常用命令(转载)

    转载自: oracle常用数据字典和SQL语句总结 Oracle常用命令大全(很有用,做笔记) 一.Oracle数据字典 数据字典是Oracle存放有关数据库信息的地方,其用途是用来描述数据的.比如一 ...

  4. docker的安装及常用命令

    一:概述 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows 机器上,也可以实现虚拟化,容器是完全使用 ...

  5. Docker的入门及常用命令

    Docker入门及常用命令 1. 各个容器之间是相互隔离状态: 这样减少了我们软件之间的影响. 2. docker是os层虚拟化架构的一种产品体现, os层虚拟化架构出来的操作系统需要和宿主机操作系统 ...

  6. Docker第二弹之常用命令

    Docker的常用命令 底层原理 Docker是如何工作的 Docker是一个Client-Server结构的系统,Docker守护进程运行在主机上, 然后通过Socket连接从客户端访问,守护进程从 ...

  7. 【Docker】docker的安装和常用命令

    一.docker安装和启动 1.yum 命令 yum install docker 2.docker启动命令 sudo systemctl start docker 二.docker常用命令 dock ...

  8. docker入门资料及常用命令

      Docker17中文开发手册 :https://www.php.cn/manual/view/36147.html   Linux部署Docker及常用命令: https://www.cnblog ...

  9. Docker——基本使用及常用命令

    Docker 是一个开源的应用容器引擎,而一个容器其实是一个虚拟化的独立的环境,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化. ...

  10. CentOS-yum安装Docker环境(含:常用命令)

    安装Docker环境 $ yum install docker -y 启动Docker $ systemctl start docker 设置自启动 $ systemctl enable docker ...

随机推荐

  1. Mac下 CMD常用命令

    1.常用命令 pwd     当前工作目录 cd(不加参数) 进root cd(folder)      进入文件夹 cd ..     上级目录 cd ~     返回root cd -     返 ...

  2. Pycharm2019最新激活码

    激活pycharm的方法有很多,一种是使用最新的激活码,另一种是使用破解补丁的方式(可以长期使用) pycharm2019最新激活码: 812LFWMRSH-eyJsaWNlbnNlSWQiOiI4M ...

  3. 4 saltstack ssh

    1.salt ssh https://docs.saltstack.com/en/latest/topics/ssh/index.html 免agent的 Salt ssh 串行 执行的时候,发送给其 ...

  4. composer (一)

    composer参考文档: composer中文网 安装composer:  Windows安装:使用安装程序:https://docs.phpcomposer.com/00-intro.html#U ...

  5. LA 6434 The Busiest City dfs

    Tree Land Kingdom is a prosperous and lively kingdom. It has N cities which are connected to eachoth ...

  6. POJ - 3162 Walking Race 树形dp 单调队列

    POJ - 3162Walking Race 题目大意:有n个训练点,第i天就选择第i个训练点为起点跑到最远距离的点,然后连续的几天里如果最远距离的最大值和最小值的差距不超过m就可以作为观测区间,问这 ...

  7. JavaScript如何比较两个数组的内容是否相同

    今天意外地发现JavaScript是不能用==或===操作符直接比较两个数组是否相等的. alert([]==[]); // false alert([]===[]); // false 以上两句代码 ...

  8. http状态码301和302详解及区别——辛酸的探索之路

    原文链接:https://blog.csdn.net/grandPang/article/details/47448395 一直对http状态码301和302的理解比较模糊,在遇到实际的问题和翻阅各种 ...

  9. Linux shell - `dirname $0` 定位到运行脚本的相对位置

    例1. 在/home/admin/test/下新建test.sh内容如下: cd `` echo `pwd` 然后返回到/home/admin/执行 sh test/test.sh 运行结果: /ho ...

  10. idea 远程代码调试

    声明一点:重要的事情说3遍 本地代码和服务器代码必须一致 本地代码和服务器代码必须一致 本地代码和服务器代码必须一致 第一步,创建remote 第二步.填写服务器信息 第三部.tomcat/bin/s ...