dockr容器lnmp
nginx
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@829947547f38 /]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@829947547f38 /]# yum -y group mark install "Development Tools"
Last metadata expiration check: 0:02:03 ago on Thu 02 Dec 2021 08:29:33 AM UTC.
Dependencies resolved.
==================================================================================
Package Architecture Version Repository Size
==================================================================================
Installing Groups:
Development Tools
Transaction Summary
==================================================================================
Complete!
[root@829947547f38 /]# useradd -r -M -s /sbin/nologin nginx
[root@829947547f38 ~]# mkdir -p /var/log/nginx
[root@829947547f38 local]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
--2021-12-02 08:33:59-- http://nginx.org/download/nginx-1.20.1.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1061461 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.20.1.tar.gz’
nginx-1.20.1.tar.gz 100%[====================>] 1.01M 224KB/s in 4.6s
2021-12-02 08:34:04 (224 KB/s) - ‘nginx-1.20.1.tar.gz’ saved [1061461/1061461]
[root@829947547f38 local]# ls
bin games lib libexec sbin src
etc include lib64 nginx-1.20.1.tar.gz share
[root@829947547f38 local]# tar xf nginx-1.20.1.tar.gz
[root@829947547f38 local]# ls
bin games lib libexec nginx-1.20.1.tar.gz share
etc include lib64 nginx-1.20.1 sbin src
[root@829947547f38 local]# cd nginx-1.20.1
[root@829947547f38 nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
checking for OS
+ Linux 4.18.0-257.el8.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
[root@829947547f38 nginx-1.20.1]# make && make install
[root@829947547f38 nginx-1.20.1]# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
[root@829947547f38 local]# /usr/local/nginx/sbin/nginx
[root@829947547f38 local]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
在创建镜像时,不能关闭容器,让它处于运行状态,所以要另起一个终端,然后执行
[root@localhost ~]# docker commit -p c1
sha256:4b2af8fdacc7b0046922b715eb4a5b3b4098e8bcf77046c78c59cb2e17249d99
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 4b2af8fdacc7 28 seconds ago 550MB
busybox latest d23834f29b38 2 days ago 1.24MB
httpd latest ad17c88403e2 13 days ago 143MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker tag 4b2af8fdacc7 best2001/c1:v0.1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
best2001/c1 v0.1 4b2af8fdacc7 2 minutes ago 550MB
busybox latest d23834f29b38 2 days ago 1.24MB
httpd latest ad17c88403e2 13 days ago 143MB
centos latest 5d0da3dc9764 2 months ago 231MB
使用新生成的镜像创建容器
[root@localhost ~]# docker run --name t1 -it best2001/c1:v0.1
[root@49676d942358 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@49676d942358 /]# /usr/local/nginx/sbin/nginx
[root@49676d942358 /]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
由此可见,新生成的镜像中是包含了新增的内容的,但是此时有一个问题,那就是容器默认要启动的进程是什么?在这里,默认情况下是启动的sh进程,但我们是要启动一个http站点,所以我们要在创建镜像时将容器默认启动的进程设为httpd,这样一来我们就可以通过新生成的镜像来快速构建一个简单的http站点了。
使用命令查看b1容器启动的默认进程是什么docker inspect
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49676d942358 best2001/c1:v0.1 "/bin/bash" 7 minutes ago Up 7 minutes t1
[root@localhost ~]# docker inspect 49676d942358
·····省略部分·····
"EndpointID": "3add13e115a71559c4392b6aa02d284ec27dce733909d1d3dd63d02b053cea24",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
使用镜像创建容器
[root@localhost ~]# docker run --name n3 -p 80:80 -dt best2001/c1:v0.1
2651ba6d2d5fd1e3eba909440e792142dbd0ae7d4ccf8e45d264ce82109f5e70
[root@localhost ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2651ba6d2d5f best2001/c1:v0.1 "/bin/bash" About a minute ago Up About a minute n3
访问测试nginx
[root@localhost ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2651ba6d2d5f best2001/c1:v0.1 "/bin/bash" 3 minutes ago Up 3 minutes n3
[root@localhost ~]# curl 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
mysql
做mysql镜像并使用容器模式的网络
[root@localhost ~]# docker run -it --name mysql01 --network container:2651ba6d2d5f centos:latest /bin/bash
//重新打开一个终端查看
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2102e9a88ce8 centos:latest "/bin/bash" 34 seconds ago Up 33 seconds mysql01 # mysql容器正在运行
2651ba6d2d5f yunjisuanlp/nginx:v1 "/usr/local/nginx/sb…" 13 minutes ago Up 13 minutes confident_wing
上传MySQL包到本地上传到centos容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2102e9a88ce8 centos:latest "/bin/bash" 34 seconds ago Up 33 seconds mysql01 # mysql容器正运行
2651ba6d2d5f yunjisuanlp/nginx:v1 "/usr/local/nginx/sb…" 13 minutes ago Up 13 minutes confident_wing
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug kernels mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# docker cp mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz 2102e9a88ce8:/usr/src # cp到mysql01容器ID下的/usr/src
MySQL容器下查看
[root@3367881fd446 ~]# cd /usr/src
[root@3367881fd446 src]# ls
debug kernels mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
# 下载
yum clean all # 清理缓存
yum makecache # 生成新缓存
yum -y install which numactl-libs ncurses-compat-libs # 安装
# 创建用户
[root@3367881fd446 src]# useradd -r -M -s /sbin/nologin mysql
[root@3367881fd446 src]# id mysql
uid=998(mysql) gid=996(mysql) groups=996(mysql)
# 解压MySQL包
[root@3367881fd446 src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@3367881fd446 src]# cd /usr/local/
[root@3367881fd446 local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.7.34-linux-glibc2.12-x86_64 share
# 创建软连接并修改属主属组
[root@3367881fd446 local]# ln -sv mysql-5.7.34-linux-glibc2.12-x86_64 mysql
'mysql' -> 'mysql-5.7.34-linux-glibc2.12-x86_64'
[root@3367881fd446 local]# ls -l
total 0
………………
lrwxrwxrwx 1 root root 35 Dec 3 08:24 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 9 root root 129 Dec 3 08:21 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 2 root root 6 Nov 3 2020 sbin
drwxr-xr-x 5 root root 49 Sep 15 14:17 share
drwxr-xr-x 2 root root 6 Nov 3 2020 src
[root@3367881fd446 local]# chown -R mysql.mysql mysql*
[root@3367881fd446 local]# ls -l
total 0
……………………
lrwxrwxrwx 1 mysql mysql 35 Dec 3 08:24 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 9 mysql mysql 129 Dec 3 08:21 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 2 root root 6 Nov 3 2020 sbin
drwxr-xr-x 5 root root 49 Sep 15 14:17 share
drwxr-xr-x 2 root root 6 Nov 3 2020 src
# 添加环境变量
[root@3367881fd446 local]# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
[root@3367881fd446 local]# bash
[root@3367881fd446 bin]# which mysql
/usr/local/mysql/bin/mysql
# 创建数据存放目录
[root@3367881fd446 local]# mkdir /opt/data
[root@3367881fd446 local]# ls -l /opt
total 0
drwxr-xr-x 2 root root 6 Dec 3 08:29 data
[root@3367881fd446 local]# chown -R mysql.mysql /opt/data
[root@3367881fd446 local]# ls -l /opt
total 0
drwxr-xr-x 2 mysql mysql 6 Dec 3 08:29 data
# 初始哈数据库不要密码
[root@3367881fd446 local]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
2021-12-03T08:42:57.345198Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-12-03T08:42:57.466893Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-12-03T08:42:57.489097Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-12-03T08:42:57.542400Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 03bd41c9-5415-11ec-ae6b-0242ac110002.
2021-12-03T08:42:57.543336Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-12-03T08:42:58.297624Z 0 [Warning] CA certificate ca.pem is self signed.
2021-12-03T08:42:58.547329Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
# 生成配置文件
[root@3367881fd446 local]# vi /etc/my.cnf
[mysqld]
port = 3306
datadir = /opt/data
basedir = /usr/local/mysql
socket = /tmp/mysql.sock
pid-file = /opt/data/mysql.pid
log-error = /opt/data/mysql.err
skip-name-resolve
# 修改文件
[root@3367881fd446 local]# vi /usr/local/mysql/support-files/mysql.server
……………………
basedir=/usr/local/mysql
datadir=/opt/data
……………………
# 启动
[root@3367881fd446 local]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
[root@3367881fd446 local]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/opt/data/mysql.err'.
SUCCESS!
[root@3367881fd446 local]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
# 登录设置密码
[root@3367881fd446 local]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
[root@3367881fd446 local]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> exit
Bye
将MySQL提交为一个镜像
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2102e9a88ce8 centos:latest "/bin/bash" About an hour ago Up 42 minutes mysql01
2651ba6d2d5f yunjisuanlp/nginx:v1 "/usr/local/nginx/sb…" About an hour ago Up About an hour confident_wing
[root@localhost ~]# docker commit -p mysql01 # 暂停mysql01
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 77281e1fab7e 3 seconds ago 3.78GB # 新提交的镜像
yunjisuanlp/nginx v1 ac3319cbbafb 19 hours ago 550MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker tag 77281e1fab7e best2001/mysql:v2 # 起名
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
best2001/nginx v0.3 148fe5cb817d 32 minutes ago 569MB
best2001/mysql v2 77281e1fab7e About a minute ago 3.78GB # 成功
best2001/nginx v0.2 943ccc9a6b39 8 hours ago 550MB
best2001/c1 v0.1 4b2af8fdacc7 30 hours ago 550MB
busybox latest d23834f29b38 3 days ago 1.24MB
centos latest 5d0da3dc9764 2 months ago 231MB