本文共 5788 字,大约阅读时间需要 19 分钟。
拉取镜像
docker pull mysql
[root@bogon home]# docker pull mysqlUsing default tag: latestTrying to pull repository docker.io/library/mysql ... latest: Pulling from docker.io/library/mysqlbe8881be8156: Already exists c3995dabd1d7: Pull complete 9931fdda3586: Pull complete bb1b6b6eff6a: Pull complete a65f125fa718: Pull complete 2d9f8dd09be2: Pull complete 37b912cb2afe: Pull complete 54242fcd8eaa: Pull complete 0a9d4d211511: Pull complete 270ae5bd02c2: Pull complete 9b55b8e72e70: Pull complete 68083f7985cd: Pull complete Digest: sha256:d39a8ab7679df309e7eff6ddba434ad5747cc2a2acee2d7c60d8221c9acedcadStatus: Downloaded newer image for docker.io/mysql:latest
查看拉取下拉的镜像
[root@bogon home]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/mysql latest 29e0ae3b69b9 9 days ago 484 MBdocker.io/centos latest 5182e96772bf 2 weeks ago 200 MBdocker.io/nginx latest c82521676580 4 weeks ago 109 MB
通过镜像创建容器
docker run -di --name=th_docker_mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=1234 mysql
[root@bogon home]# docker run -di --name=th_docker_mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=1234 mysql289582afa5ad9a04c99020bd0aa9360fa747e11862c39f4633de106ab38e3d56[root@bogon home]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES289582afa5ad mysql "docker-entrypoint..." 8 seconds ago Up 7 seconds 33060/tcp, 0.0.0.0:33306->3306/tcp th_docker_mysql
-p 代表端口映射,格式为 宿主机映射端口:容器运行端口
-e 代表添加环境变量 MYSQL_ROOT_PASSWORD 是 root 用户的登陆密码
登录容器
docker exec -it th_docker_mysql /bin/bash
[root@bogon home]# docker exec -it th_docker_mysql /bin/bashroot@289582afa5ad:/# mysql -u root -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 10Server version: 8.0.12 MySQL Community Server - GPLCopyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> status--------------mysql Ver 8.0.12 for Linux on x86_64 (MySQL Community Server - GPL)Connection id: 10Current database: Current user: root@localhostSSL: Not in useCurrent pager: stdoutUsing outfile: ''Using delimiter: ;Server version: 8.0.12 MySQL Community Server - GPLProtocol version: 10Connection: Localhost via UNIX socketServer characterset: utf8mb4Db characterset: utf8mb4Client characterset: latin1Conn. characterset: latin1UNIX socket: /var/run/mysqld/mysqld.sockUptime: 4 min 22 secThreads: 2 Questions: 13 Slow queries: 0 Opens: 110 Flush tables: 2 Open tables: 86 Queries per second avg: 0.049--------------
同样我们也可以使用 mysql 的客户端软件去连接 mysql
注意 docker 中安装的最新的 mysql 版本是 8.0.12 版本,具体的版本信息和获取不同版本的方式,可以在 docker hub 上进行查看
docker pull tomcat
[root@bogon home]# docker pull tomcatUsing default tag: latestTrying to pull repository docker.io/library/tomcat ... latest: Pulling from docker.io/library/tomcat55cbf04beb70: Pull complete 1607093a898c: Pull complete 9a8ea045c926: Pull complete 1290813abd9d: Pull complete 8a6b982ad6d7: Pull complete abb029e68402: Pull complete d068d0a738e5: Pull complete 42ee47bb0c52: Pull complete ae9c861aed25: Pull complete 60bba9d0dc8d: Pull complete 091c11d70257: Pull complete 869034383fc7: Pull complete Digest: sha256:eab533908d6c7760a84a5edbb4490b648484941dfa708a1c5f4286db8caab825Status: Downloaded newer image for docker.io/tomcat:latest
docker run -di --name=th_docker_tomcat -p 8099:8080 -v /usr/local/webapps/:/usr/local/tomcat/webapps tomcat
-p : 指定端口映射
-v: 指定目录映射
将应用程序部署到宿主机的/usr/local/webapps/目录下 , 然后进行访问
docker pull nginx
docker run -di --name=th_docker_nginx -p 80:80 nginx
[root@bogon home]# docker run -di --name=th_docker_nginx -p 80:80 nginx5de5bd34482322789ea7bc51e54cd26cf88ae5fc0e2ede762f3c196e6bf3083a
访问容器,这里 ip 映射为宿主机的ip,直接通过宿主机的 ip 就可以访问
注: 容器中的 nginx 的配置文件存储于/etc/nginx 目录下,所以要进行反向代理以及负载均衡的配置可以上/etc/nginx 目录下找到对应的配置文件然后 做更改. 如果在容器中没有办法进行修改,那么我们可以将其容器中的配置文件拷贝到宿主机上进行修改,然后在将修改后的文件拷贝到容器中
docker pull redis
[root@bogon home]# docker pull redisUsing default tag: latestTrying to pull repository docker.io/library/redis ... latest: Pulling from docker.io/library/redisbe8881be8156: Already exists d6f5ea773ca3: Pull complete 735cc65c0db4: Pull complete 787dddf99946: Pull complete 0733799a7c0a: Pull complete 6d250f04811a: Pull complete Digest: sha256:858b1677143e9f8455821881115e276f6177221de1c663d0abef9b2fda02d065Status: Downloaded newer image for docker.io/redis:latest
docker run -di --name=th_docker_redis -p 6379:6379 redis
[root@bogon home]# docker run -di --name=th_docker_redis -p 6379:6379 redisbff4ff3bd48a8d46d0c7035b4912b532350bea277cee407c59e22f093575a536
通过 redis 客户端就可以直接连容器中的 redis了
[root@bogon home]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESbff4ff3bd48a redis "docker-entrypoint..." 12 hours ago Up 12 hours 0.0.0.0:6379->6379/tcp th_docker_redis7201b7a695c1 tomcat "catalina.sh run" 12 hours ago Up 12 hours 0.0.0.0:8099->8080/tcp th_docker_tomcat289582afa5ad mysql "docker-entrypoint..." 12 hours ago Up 12 hours 33060/tcp, 0.0.0.0:33306->3306/tcp th_docker_mysql[root@bogon home]#
转载地址:http://drfli.baihongyu.com/