1.拉取hello-world镜像并运行
docker pull hello-world 拉取hello-world镜像
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[root@localhost ~]#docker images 查看当前docker镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 12 months ago 1.84kB
[root@localhost ~]#docker run --name myhelloword hello-world 运行hello-world镜像
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
docker ps 查看正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#docker ps -a 查看后台存在的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3854e40102c hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago myhelloword
删除容器
docker rmi -f hello-world:latest 删除容器
Untagged: hello-world:latest
Untagged: hello-world@sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
[root@localhost ~]# docker images 查看容器
REPOSITORY TAG IMAGE ID CREATED SIZE
拉取tomcat镜像
docker pull tomcat 默认拉取最新的,也可以指定版本拉取 docker pull tomcat:8
docker run -d --name my-tomcat -p 9090:8080 tomcat 默认会找latest
Unable to find image 'tomcat:latest' locally
^[[A^[[A^[[A^C
[root@localhost ~]# docker run -d --name my-tomcat -p 9090:8080 tomcat:8 创建一个container
2aed373de5eff79c66df4ef9f0e4deef6c44fc9b1ed51c6c11b32d073a930af2
[root@localhost ~]# docker pscontainer 信息
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2aed373de5ef tomcat:8 "catalina.sh run" 13 seconds ago Up 12 seconds 0.0.0.0:9090->8080/tcp my-tomcat
[root@localhost ~]# docker exec -it my-tomcat /bin/bash 进入my-tomcat容器里
root@2aed373de5ef:/usr/local/tomcat# ls 发现和tomcat目录是一样的
BUILDING.txt LICENSE README.md RUNNING.txt conf lib native-jni-lib webapps
CONTRIBUTING.md NOTICE RELEASE-NOTES bin include logs temp work
再创建两个:
root@2aed373de5ef:/usr/local/tomcat# exit
exit
[root@localhost ~]# docker run -d --name my-tomcat -p 8080:8080 tomcat:8 创建一个container
[root@localhost ~]# docker run -d --name my-tomcat -p 8081:8080 tomcat:8 创建一个container
都可以访问
[root@localhost ~]# docker ps 发现有三个container了
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c14286a57c7 tomcat:8 "catalina.sh run" 5 minutes ago Up 5 minutes 0.0.0.0:8081->8080/tcp tomcat3
1bfd9489dad8 tomcat:8 "catalina.sh run" 5 minutes ago Up 5 minutes 0.0.0.0:8080->8080/tcp tomcat2
2aed373de5ef tomcat:8 "catalina.sh run" 17 minutes ago Up 17 minutes 0.0.0.0:9090->8080/tcp my-tomcat
[root@localhost ~]# docker rm -f $(docker ps -aq) 删除全部容器
5c14286a57c7
1bfd9489dad8
2aed373de5ef
e3854e40102c
[root@localhost ~]# docker ps 查看容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#
docker拉取mysql
docker pull mysql
docker run -d --name mysql01 -p 3301:3306 -e MYSQL_ROOT_PASSWORD=lf1234 --privileged mysql创建mysql容器,参数可以参考(docker官网mysql文档)
2e06bb21508602ebee2f59b00521556fa43fcdb5b3fea01e35566c6f01561c40
由于连接工具不支持高级版本mysql,所以从新拉取了mysql5.5版本的,
docker pull mysql:5.5
docker run -d --name mysql02 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=lf1234 --privileged mysql:5.5
然后用mysql连接工具进行连接即可
docker stop mysql01停止容器
mysql01
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
729964e122ad mysql:5.5 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp mysql02
在客户端连接工具中创建一个db_lf库并创建一张表lf_001,在docker进入容器中查看
[root@localhost ~]# docker exec -it mysql02 /bin/bash
root@729964e122ad:/#
root@729964e122ad:/# mysql -uroot -plf1234
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db_lf |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
mysql> use db_lf;
。。。
Database changed
mysql> show tables
-> ;
+-----------------+
| Tables_in_db_lf |
+-----------------+
|lf_001 |
+-----------------+
1 row in set (0.00 sec)
mysql>
拉取redis镜像
docker pull redis 下载最新版本的redis镜像
修改redis.config配置文件
修改默认配置文件
bind 127.0.0.1 #注释掉这部分,这是限制redis只能本地访问
protected-mode no #默认yes,开启保护模式,限制为本地访问
daemonize no#默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程(可选),改为yes会使配置文件方式启动redis失败
dir ./ #输入本地redis数据库存放文件夹(可选)
appendonly yes #redis持久化(可选)
运行redis:
docker run -d --name redis1 -p 6379:6379 redis redis-server 直接启动redis
创建redis容器(指定配置文件)自定义路径
docker run -d --name redis1 -p 6379:6379 -v /usr/local/redis/config:/etc/redis/redis.config-v /usr/local/redis/data:/data redis redis-server /etc/redis/redis.conf --requirepass "root123" --appendonly yes
参数说明: -p 6379:6379 //容器redis端口6379映射宿主主机6379
-d redis //后台模式启动redis
--name redis //容器名字为redis
#配置文件映射,容器启动成功可以通过更改宿主机的配置文件来达到更改容器实际配置文件的目的-v /usr/local/redis/redis.conf:/etc/redis/redis.conf
#挂载持久化文件-v /usr/local/docker/redis/data:/data/usr/local/my-redis/data是宿主机中持久化文件的位置/data/是容器中持久化文件的位置(需要和配置文件中dir属性值一样),
redis-server /etc/redis/redis.conf //redis将以/etc/redis/redis.conf为配置文件启动
--appendonly yes //开启redis的AOF持久化,默认为false,不持久化
当前目录下$PWD(建议使用自定义路径)[root@localhost ~]# docker run -d -p 6379:6379 -v $PWD/conf/redis.conf:/usr/local/etc/redis/redis.conf -v $PWD/data:/data --name lfredis redis redis-server /usr/local/etc/redis/redis.conf --appendonly yes
-v:表示挂载路径,$PWD表示当前目录下,冒号左面的表示我们宿主机的挂载目录,也就是我们虚拟机所在的文件路径,冒号右边则表是的是redis容器在容器内部的路径,上面的命令我分别挂载了redis.conf(redis的配置文件),如需使用配置文件的方式启动redis,这里则需要加上,还有redis存放数据所在的目录
标红部分
映射到此路径路径
data持久化路径
客户端连接redis
docker exec -it 启动的redis容器名称 redis-cli
如果连接远程:
docker exec -it 启动的redis容器名称 redis-cli -h 远程redis所在ip -p 6379 -a password //如果有密码 使用 -a参数输入密码
拉取RabbitMQ镜像
docker pull rabbitmq:management ,指定版本,该版本包含了web控制页面
注意:如果docker pull rabbitmq 后面不带management,启动rabbitmq后是无法打开管理界面的,所以我们要下载带management插件的rabbitmq.
启动:
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:management
#方式一:默认guest 用户,密码也是 guest
docker run -d --name rabbitmq --hostname my-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:management
#方式二:设置用户名和密码 docker run -d --name rabbit --hostname my-rabbit -e RABBITMQ_DEFAULT_USER=root -e RABBITMQ_DEFAULT_PASS=root1234 -p 5672:5672 -p 15672:15672 rabbitmq:management
访问web控制页面:http://ip:15672/
查看网络
启动一个tomcat容器
[root@localhost ~]# docker run -d --name mytomcat -p 8080:8080 tomcat
0f95d1856b08645f1e2aff71c8577b9815d01b446a8b5ec25054c44beeaf8f7b
[root@localhost ~]# docker exec -it mytomcat /bin/bash
root@0f95d1856b08:/usr/local/tomcat# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
17: eth0@if18: mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scopeglobal eth0
valid_lft forever preferred_lft forever
root@0f95d1856b08:/usr/local/tomcat#
在虚拟机中尝试访问上面ip
root@0f95d1856b08:/usr/local/tomcat# exit
exit
[root@localhost ~]#ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.047 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.079 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.057 ms
64 bytes from 172.17.0.2: icmp_seq=10 ttl=64 time=0.037 ms
^C
--- 172.17.0.2 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9000ms
rtt min/avg/max/mdev = 0.037/0.054/0.079/0.014 ms
[root@localhost ~]# curl 172.17.0.2:8080 访问tomcat,返回一个首页html
!DOCTYPE html>
Apache Tomcat/8.5.50.....
ip a 查看当前网络信息
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo 当前物理主机的127
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:8a:fe:e6 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0和外界通信的eth0
valid_lft 79444sec preferred_lft 79444sec
inet6 fe80::5054:ff:fe8a:fee6/64 scope link
valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:4c:de:2e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.104/24 brd 192.168.1.255 scope global noprefixroute dynamic eth1 和宿主主机通信的eth1
valid_lft 6279sec preferred_lft 6279sec
inet6 fe80::a00:27ff:fe4c:de2e/64 scope link
valid_lft forever preferred_lft forever
4: docker0: mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:d1:e7:1d:84 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 和container通信的docker0
0valid_lft forever preferred_lft forever
inet6 fe80::42:d1ff:fee7:1d84/64 scope link
valid_lft forever preferred_lft forever
16: veth66debb8@if15: mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 9a:87:2f:4b:57:c9 brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet6 fe80::9887:2fff:fe4b:57c9/64 scope link
valid_lft forever preferred_lft forever
18: veth66939a0@if17: mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 2a:31:0b:81:07:23 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::2831:bff:fe81:723/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
c9ce1b7c6802 bridge bridge local
6be1ec1d25da host host local
de6f4329fa79 none null local
[root@localhost ~]# docker inspect bridge
[
{
"Name": "bridge",
"Id": "c9ce1b7c680257db409549372baf346de1cbe83675ba8a4f31bed78a872c96d8",
"Created": "2020-01-03T14:40:54.581957235Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"0f95d1856b08645f1e2aff71c8577b9815d01b446a8b5ec25054c44beeaf8f7b": {
"Name": "mytomcat",
"EndpointID": "ee5c82c52a08e70a7ac843d4e464e301597072a0daf21c04adcfe90d6074523c",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
},
"729964e122adf4291d623d8c869605a4a80348368be0400938c265f60e5b9136": {
"Name": "mysql02",
"EndpointID": "b2ca6b6e5d4e64e32cf5c0a5e6a89b28aed1268dd942b23473518bdccc80703c",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
-d redis //后台模式启动redis