#查看拉取到的镜像 [root@docker01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx alpine 0f0eda053dc5 2 weeks ago 43.3MB nginx latest 5ef79149e0ec 2 weeks ago 188MB alpine latest 324bc02ae123 6 weeks ago 7.8MB busybox latest 87ff76f62d36 15 months ago 4.26MB centos 6 5bf9684f4720 2 years ago 194MB centos 7 eeb6ee3f44bd 2 years ago 204MB
[root@docker03 ~]# docker image load -i /tmp/eeb6ee3f44bd.tgz [root@docker03 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> eeb6ee3f44bd 2 years ago 204MB
#给镜像加标签(改名) docker tag ID号 镜像名:标签
[root@docker03 ~]# docker tag eeb6ee3f44bd centos:7 [root@docker03 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7 eeb6ee3f44bd 2 years ago 204MB
1、#运行alpine容器 [root@docker03 ~]# docker run alpine /bin/echo 'hello world' hello world
2、#查看正在运行的容器 [root@docker02 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1ed0569ef98e alpine "/bin/echo 'hello wo…" 9 seconds ago Exited (0) 9 seconds ago affectionate_curie a1b13ddd5328 alpine "echo hello world" 19 seconds ago Created goofy_bartik -a:查看所有容器 [root@docker02 ~]# docker ps -aq
#运行一个容器 docker run [1个或者多个选项] 镜像 [command] [root@docker02 ~]# docker run centos:7 /bin/bash
[root@docker01 ~]# docker run -it -d busybox /bin/sh
#让容器在后台运行 [root@docker03 ~]# docker run -d centos:7 tailf /etc/passwd
[root@docker03 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 29d1fc5f02e3 centos:7 "tailf /etc/passwd" 28 seconds ago Up 28 seconds vibrant_ritchie
#运行容器时给容器起一个名字 [root@docker03 ~]# docker run --name wordpress -d nginx:alpine [root@docker03 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c13fdc5ac70e nginx:alpine "/docker-entrypoint.…" 18 seconds ago Up 17 seconds 80/tcp wordpress
#运行一个镜像并连进去 [root@docker03 ~]# docker run -it centos:7 /bin/bash
3、#连接一个容器 如果/bin/bash不能连接进去,就换成/bin/sh [root@docker03 ~]# docker exec -it 29d1fc5f02e3 /bin/bash 查看PID为1的进程 [root@29d1fc5f02e3 /]# ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 4364 584 ? Ss 07:11 0:00 tailf /etc/passwd
--------docker run 帮我们做了哪些事----- [root@docker02 ~]# docker run nginx:alpine
1、查看镜像 [root@docker02 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 324bc02ae123 6 weeks ago 7.8MB
2、删除镜像,报错 [root@docker02 ~]# docker rmi 324bc02ae123 Error response from daemon: conflict: unable to delete 324bc02ae123 (must be forced) - image is being used by stopped container a1b13ddd5328
1、# -p:把容器的80端口映射到宿主机的80端口 [root@docker03 ~]# docker run -p80:80 -d nginx:alpine
[root@docker03 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 92e2dbca064e nginx:alpine "/docker-entrypoint.…" 9 seconds ago Up 8 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp adoring_hertz
浏览器访问10.0.0.103,就可以访问nginx页面
2、 [root@docker03 ~]# docker run -p8080:80 -p90:90 -d nginx:alpine
2、端口映射 [root@docker03 ~]# docker run -p10.0.0.103:80:80 -d nginx:alpine [root@docker03 ~]# docker run -p172.16.1.103:80:80 -d nginx:alpine [root@docker03 ~]# docker run -p127.0.0.1:80:80 -d nginx:alpine
[root@docker03 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7a87c542db29 nginx:alpine "/docker-entrypoint.…" 25 seconds ago Up 24 seconds 127.0.0.1:80->80/tcp kind_mahavira a2d578ffff01 nginx:alpine "/docker-entrypoint.…" 47 seconds ago Up 46 seconds 172.16.1.103:80->80/tcp interesting_joliot 98e6d6611bd6 nginx:alpine "/docker-entrypoint.…" 59 seconds ago Up 58 seconds 10.0.0.103:80->80/tcp interesting_jennings
1、在宿主机上起5个随机端口 [root@docker03 ~]# docker run -p10.0.0.103::80 -d nginx:alpine [root@docker03 ~]# docker run -p10.0.0.103::80 -d nginx:alpine [root@docker03 ~]# docker run -p10.0.0.103::80 -d nginx:alpine [root@docker03 ~]# docker run -p10.0.0.103::80 -d nginx:alpine [root@docker03 ~]# docker run -p10.0.0.103::80 -d nginx:alpine
2、查看端口 [root@docker03 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 57ae53de8781 nginx:alpine "/docker-entrypoint.…" About a minute ago Up About a minute 10.0.0.103:32776->80/tcp distracted_tharp fe081b7a25ac nginx:alpine "/docker-entrypoint.…" About a minute ago Up About a minute 10.0.0.103:32775->80/tcp kind_knuth 2ed44ac6261f nginx:alpine "/docker-entrypoint.…" About a minute ago Up About a minute 10.0.0.103:32774->80/tcp clever_sinoussi c009cf3348ff nginx:alpine "/docker-entrypoint.…" About a minute ago Up About a minute 10.0.0.103:32773->80/tcp inspiring_hawking fe02a74ce3d2 nginx:alpine "/docker-entrypoint.…" About a minute ago Up About a minute 10.0.0.103:32772->80/tcp keen_yonath
2、运行nginx容器 [root@docker01 ~]# docker run -p80:80 -d nginx:alpine
3、查看运行的PID [root@docker01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 74ce711a7128 nginx:alpine "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp zealous_shaw
1、新开一个窗口操作 [root@docker01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 76a4b880872b nginx:alpine "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp funny_sinoussi
10、如果现在退出进行,这个容器就会退出,nginx就会结束,这个镜像相当于没起nginx,没有办法让这个容器起来 [root@docker01 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES afc3071510f6 centos:7 "/bin/bash" 24 minutes ago Exited (0) 8 seconds ago friendly_wozniak