docker nginx 和 images 的练习(centos 和 Ubuntu的操作步骤)-爆肝通宵也值得!
Posted 未末0902
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker nginx 和 images 的练习(centos 和 Ubuntu的操作步骤)-爆肝通宵也值得!相关的知识,希望对你有一定的参考价值。
**
docker nginx 和 images 的练习(centos 和 Ubuntu的操作步骤)
**
前言
提示:这里可以添加本文要记录的大概内容:
接上一篇的小练习啦:
nginx 和 nginx 配置
大家动手敲一敲哈>–<
提示:以下是本篇文章正文内容,下面案例可供参考
一、nginx 操作练习:
练习:
1.下载redis的镜像
2.下载nginx的镜像
3.启动3个nginx的容器,名字叫sc-nginx-cali-1~3,映射的端口号不要一样
docker run --name sc-nginx-1 -d -p 8081:80 nginx
docker run --name sc-nginx-2 -d -p 8082:80 nginx
docker run --name sc-nginx-3 -d -p 80832:80 nginx
4.查看你的机器里的所有的docker镜像
5.停止下sc-nginx-cali-1容器
6.再次启动下sc-nginx-cali-1容器
7.删除sc-nginx-cali-2~3容器
8.查看docker版本
9.查找下mongodb的镜像,不下载
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
二、实验具体操作:
1.步骤:
docker create --》静态的容器
[root@sc-docker web]# docker create --name sc-zhengbo-3 -v /web:/usr/share/nginx/html:ro -p 8092:80 daocloud.io/nginx
b874459462b283aba813b9b7e6cf77f330d53728e43a04587b63c44682f31d60 --》容器的唯一标识 id
[root@sc-docker web]#
[root@sc-docker web]# docker create --name sc-zhengbo-3 -v /web:/usr/share/nginx/html:ro -p 8092:80 daocloud.io/nginx
b874459462b283aba813b9b7e6cf77f330d53728e43a04587b63c44682f31d60
[root@sc-docker web]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b874459462b2 daocloud.io/nginx "/docker-entrypoint.…" 54 seconds ago Created sc-zhengbo-3
ae1489fbf5a8 daocloud.io/nginx "/docker-entrypoint.…" 3 minutes ago Created sc-zhengbo-2
ba25850f3254 daocloud.io/nginx "/docker-entrypoint.…" 6 minutes ago Exited (0) 5 minutes ago sc-zhengbo-1
4fec16d613f1 daocloud.io/nginx "/docker-entrypoint.…" 4 hours ago Exited (0) 46 minutes ago sc-nginx-4
f7ae38be6dc6 daocloud.io/nginx "/docker-entrypoint.…" 4 hours ago Exited (0) 46 minutes ago sc-nginx-3
[root@sc-docker web]# cat /etc/passwd|grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@sc-docker web]#
exec 进入容器的内部
-it 开启一个交互式的终端进入容器里面,运行/bin/bash
cali@sanchuang-server:~$ sudo docker exec -it sc-nginx-2 /bin/bash
root@8467c45cf176:/# hostname
8467c45cf176
root@8467c45cf176:/# cat /etc/issue
Debian GNU/Linux 10 \\n \\l
root@8467c45cf176:/# ip add
bash: ip: command not found
root@8467c45cf176:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
root@8467c45cf176:/# mkdir feng
root@8467c45cf176:/# ls
bin dev docker-entrypoint.sh feng lib media opt root sbin sys usr
boot docker-entrypoint.d etc home lib64 mnt proc run srv tmp var
root@8467c45cf176:/# vim
bash: vim: command not found
root@8467c45cf176:/#
**
2.问题:进入容器里后,为什么很多的命令不能使用?
**
1.启动mysql的容器*
docker run --name sc-mysql-1 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
-e 宿主机往容器里传递环境变量 environment
MYSQL_ROOT_PASSWORD 是变量名
my-secret-pw 变量的值
docker run --name sc-mysql-1 -e MYSQL_ROOT_PASSWORD='sc123456' -d -p 3306:3306 mysql:5.7.35
centos
yum install mariadb mariadb-server
mariadb 是客户端的软件包
mariadb-server 是服务器的软件包
ubuntu
apt install mariadb-server 安装mariadb-server软件包,会自动安装mysql的客户端的命令
cali@sanchuang-server:~$ sudo ps aux|grep mysql
mysql 19361 0.1 1.9 1711320 79832 ? Ssl 08:47 0:00 /usr/sbin/mysqld
cali 20821 0.0 0.0 6432 668 pts/0 S+ 08:54 0:00 grep --color=auto mysql
cali@sanchuang-server:~$ sudo ss -anplut|grep mysql
tcp LISTEN 0 80 127.0.0.1:3306 0.0.0.0:* users:(("mysqld",pid=19361,fd=21))
cali@sanchuang-server:~$ sudo service mariadb stop 关闭mariadb服务
cali@sanchuang-server:~$ sudo ss -anplut|grep mysql
cali@sanchuang-server:~$
cali@sanchuang-server:~$ sudo mysql -uroot -psc123456 -h 192.168.0.19 访问本机ip地址的mysql
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MySQL connection id is 2
Server version: 5.7.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.
MySQL [(none)]>exit
在Ubuntu里启用root用户
cali@sanchuang-server:~$ sudo passwd root
New password:
Retype new password:
passwd: password updated successfully
cali@sanchuang-server:~$ su root
Password:
root@sanchuang-server:/home/cali#
root@sanchuang-server:/home/cali#
root@sanchuang-server:/home/cali#
root@sanchuang-server:/home/cali#
**centos8里不需要安装mariadb-server
**
1.在宿主机上安装mariadb的客户端软件,获得mysql命令**
**
yum install mariadb
2.启动mysql的容器
docker run --name sc-mysql-1 -e MYSQL_ROOT_PASSWORD='sc123456' -d -p 3306:3306 mysql:5.7.35
[root@sc-docker web]# docker run --name sc-mysql-1 -e MYSQL_ROOT_PASSWORD='sc123456' -d -p 3306:3306 mysql:5.7.35
a4078e1a7ea1bec6f752d29773292513aa5e5fd3ba8109024f53a3e3b47c6791
[root@sc-docker web]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4078e1a7ea1 mysql:5.7.35 "docker-entrypoint.s…" 5 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp,33060/tcp sc-mysql-1
b874459462b2 daocloud.io/nginx "/docker-entrypoint.…" About an hour ago Up 58 minutes 0.0.0.0:8092->80/tcp, :::8092->80/tcp sc-zhengbo-3
[root@sc-docker web]#
3.去访问 可以是其他的机器 -h 指定连接服务器的ip地址*
root@sanchuang-server:/home/cali# mysql -h 192.168.0.17 -uroot -psc123456
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MySQL connection id is 2
Server version: 5.7.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.
MySQL [(none)]> show databases; 查看有哪些数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
**
4. rows in set (0.002 sec)**
MySQL [(none)]> exit 退出
Bye
root@sanchuang-server:/home/cali#
在centos上测试连接到MySQL
[root@sc-docker web]# mysql -h 192.168.0.17 -uroot -psc123456
Welcome to the MariaDB monitor. Commands end with ; or \\g.
Your MySQL connection id is 7
Server version: 5.7.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.
MySQL [(none)]> exit
Bye
[root@sc-docker web]#
总结:
提示:这里对文章进行总结:
1.这是个nginx的小实验,可以拿来练练手,用来熟悉一下 docker 和 nginx之间的操作,非常不错,如果这个练习你全部会做,说明你针的很不戳!!!
2.如果有不懂的可以参考我的上一篇文章,这样你会好理解一点.
https://blog.csdn.net/qq_45737042/article/details/119800995
如果还是有问题可以留言或者私信哈,有问必答.
3.多练多练多练,或许有人说我啰嗦和废话,但真的希望你多敲一敲啦>–<
**
4.如果你看到这里了,麻烦👍 + 关注哈,感谢支持,码字不易,谢谢理解.
**
以上是关于docker nginx 和 images 的练习(centos 和 Ubuntu的操作步骤)-爆肝通宵也值得!的主要内容,如果未能解决你的问题,请参考以下文章
markdown build-docker-nginx-image.md #docker_training