构建容器的实质:为进程封箱
启动一个进程,使用独立的文件系统、网络资源,一套独立的进程树。语法:
docker run [options] IMAGE [[CMD] ARG…]
–add-host=[ ]
添加一条主机到 ip 的映射,可以多次出现
–cidfile=""
容器id 文件,类似于 pid 文件
-d --detach=true|false
后台运行【容器】,并打印容器 id
–dns=[ ]
设置 dns 地址
-e --env=[ ]
为容器的目标进程指定环境变量
–expose=[ ]
暴露端口
-h --hostname=""
指定主机名
-i --interactive=true|false
前台运行【容器】
默认 false
–ip=""
设置容器 ip
–ip6=""
设置容器 ipv6 地址
–name=""
为容器分配一个名字
-P --publish-all=true|false
暴露所有的容器端口,映射到主机的随机端口
默认:false
-p --public=[ ]
暴露容器端口到指定的主机端口
格式:ip:hPort:cPort、ip::cPort、hPort:cPort、cPort
默认值:ip 0.0.0.0、hPort:随机数
$ docker run -d --name tomcat8 tomcat:8.5
$ docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1 mysql:5
$ docker run -it --name centos7 -h argor centos:8
$ docker run -d --name tomcat8_6 -P tomcat:8.5
fd60a0ccd8914376cbe5df0bb2188c0e569605c3abfe9a0c4205999525274c2a
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd60a0ccd891 tomcat:8.5 "catalina.sh run" 6 seconds ago Up 6 seconds 0.0.0.0:32768->8080/tcp tomcat8_6
$ list
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::32768 :::* LISTEN 8755/docker-proxy-c
$ docker run -d --name tomcat8_6 -p 8080 tomcat:8.5
5eaf18f6d093033aa27cdb389117ed4f82b6b2bee0e223405e20d895bef35241
$ list
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::32769 :::* LISTEN 9070/docker-proxy-c
$ docker run -d --name tomcat8_6 -p 80:8080 tomcat:8.5
22080f13c145430f200aa48d9c32307f55fd9b42155f6d6a52ae4dec24442627
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22080f13c145 tomcat:8.5 "catalina.sh run" 6 seconds ago Up 5 seconds 0.0.0.0:80->8080/tcp tomcat8_6
$ list
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::80 :::* LISTEN 9312/docker-proxy-c
$ docker run -d --name tomcat8_6 -p 127.0.0.1::8080 tomcat:8.5
3956a73e0219340ed529c1a1ac902562c2434032c92bb746b0416a5c9fd9db04
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3956a73e0219 tomcat:8.5 "catalina.sh run" 4 seconds ago Up 4 seconds 127.0.0.1:32768->8080/tcp tomcat8_6
$ list
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:32768 0.0.0.0:* LISTEN 9502/docker-proxy-c
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1138/sshd
$ docker run -d --name tomcat8_6 -p 127.0.0.1:8000:8080 tomcat:8.5
bc4dcfdd3c0bfe05657ccca77ea1e61c33222b2c2fa59b7af99939f268616442
$ list
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN 9749/docker-proxy-c
# 挂在主机目录作为容器卷
$ docker run -v /var/db:/data1 -i -t fedora bash
# 主机开启 SELinux,执行会报错。须执行:
$ chcon -Rt svirt_sandbox_file_t /var/db
把主机的目录挂在到【容器】,在主机操作目录,查看【容器】下挂在目录的变化
$ docker run -di --name cent7 -h demo -v /usr/local/demo:/demo centos:7
748c6faf9464d01eb091fa8ef4795109b1dab778691d3c6f20461a871abdcc1f
$ docker exec cent7 ls /demo
etc
games
java
$ mkdir /usr/local/hello
$ docker exec cent7 ls /demo
etc
games
hello
java
$ docker run -it --name centos7 -h argor centos:8
[root@argor /]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)
[root@argor /]# exit
exit
$ docker rename centos7 centos8
$ docker start centos8
$ docker exec -it centos8 /bin/bash
[root@argor /]#
$ docker run -di --name centos7 -h "donatello.cc" centos:7
d1b42aba502d9439833ef192543a051a17a30b32b04efe50b9d9f4bcbe28744a
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1b42aba502d centos:7 "/bin/bash" 6 seconds ago Up 5 seconds centos7
$ docker exec -it centos7 /bin/bash
[root@donatello /]# hostname
donatello.cc
[root@donatello /]# exit
exit
通过 host 主机的任意 ip 的 13306 端口可以外部访问 mysql
$ docker run -di --name=piny_db --hostname=piny_db -p 13306:3306 -e MYSQL_ROOT_PASSWORD=root mysql:5.7.31
8ee3b543f7bbed0d5de0f130628dcd5d3f6f10433d2f655061c30a2394de1071
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8ee3b543f7bb mysql:5.7.31 "docker-entrypoint..." 5 seconds ago Up 3 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp piny_db
$ docker exec -it piny_db bash
root@piny_db:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>