1.将容器直接提交成镜像
[root@localhost ~]# docker commit 8ecc7f6b9c12 nginx:1.1
sha256:9a2bb94ba6d8d952527df616febf3fbc8f842b3b9e28b7011b50c743cd7b233b
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.1 9a2bb94ba6d8 7 seconds ago 236MB
rockylinux 9.3 583a894199be 5 hours ago 181MB
将容器的文件挂载到宿主机目录下,容器删除掉,它的文件也会存在
[root@lvs-server ~]# docker run -itd -e MYSQL_ROOT_PASSWORD=QianFeng@123 -v /opt/data:/var/lib/mysql mysql:8.0.38-oraclelinux9
fa63565b79532d028a09fc731496a02a0e442b2367b23c932ee6286e5e39f68d
前面的路径是宿主机的,后面的是容器的
构建容器
[root@lvs-server opt]# docker build -t dagou-rocky:1.0.7 .
[+] Building 0.1s (8/8) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 750B 0.0s
=> WARN: MaintainerDeprecated: Maintainer instruction is deprecated in favor of using label (line 3) 0.0s
=> [internal] load metadata for docker.io/library/rockylinux:9.3 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/3] FROM docker.io/library/rockylinux:9.3 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 92B 0.0s
=> CACHED [2/3] RUN yum -y install wget 0.0s
=> CACHED [3/3] COPY ./nginx.conf /etc/nginx/nginx.conf 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:870ae144b4b45549edffbc53d16182d0445d8466934b256ac6302e321c3bf30e 0.0s
=> => naming to docker.io/library/dagou-rocky:1.0.7
创建容器
[root@lvs-server opt]# docker run -itd nginx:latest
36151386fa9e9ea2c060936a1b6199f95371059c5a63e4744c387ba02de00a1f
nginx.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html/wordpress;
index index.php;
}
location ~ \.php$ {
root /usr/share/nginx/html/wordpress;
fastcgi_pass 127.0.0.1:9000;
# fastcgi_pass unix:///run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
Dockerfile内容:
#写在第一行,定义使用的夫镜像是哪一个
FROM docker.io/library/rockylinux:9.3
#指定作者描述信息
MAINTAINER dagou
# RUN yum install -y epel-release
# RUN yum install -y nginx
#指定变量
# ARG VERSION=1.0
#ADD souce dest
#拷贝文件到镜像内
#1.支持网络资源拷贝,后面要加域名,如:http://www.example.com/file.tar.gz
#2.如果拷贝的是tar包,会自动的将tar包内的东西一同拷贝到镜像内,会自动解压
#1和2不可以同时使用
#给镜像打标签
LABEL MAINTAINER="dagou youxiang:192.168@qq.com build time='7-20'"
#构建镜像执行的命令
RUN yum -y install wget
#拷贝文件到镜像内
COPY ./nginx.conf /etc/nginx/nginx.conf
#暴露端口
EXPOSE 80 443
#如果是官网的nginx,首先创建一个以.repo结尾的文件,把官网的nginx源放进去
#COPY ./nginx.repo /etc/yum.repos.d/
#声明一个数据目录或者是配置目录,供容器使用(映射)
VOLUME [ "/usr/share/nginx/html"]
CMD [ "nginx","-g","daemon off;"]
php的配置文件
[www]
user = apache
group = apache
listen = 9000
listen.acl_users = apache,nginx
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache
dockerfile脚本
FROM rockylinux:9.3
LABEL MAINTAINER="tom 2996215658@qq.com project="wordpress""
RUN yum install -y http://rpms.remirepo.net/enterprise/remi-release-9.rpm
RUN yum install php80-php-xsl php80-php php80-php-cli php80-php-devel php80-php-gd php80-php-pdo php80-php-mysql php80-php-fpm -y
COPY ./nginx.repo /etc/yum.repos.d/
# 安装nginx
# RUN yum install -y epel-release
RUN yum install -y nginx
ADD ./wordpress-5.9.1-zh_CN.tar.gz /usr/share/nginx/html
RUN chmod 777 -R /usr/share/nginx/html/wordpress
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80 443
COPY ./docker-srart.sh /
CMD [ "sh","docker-srart.sh;" ]
# CMD [ "nginx","-g daemon off;"]
RUN /opt/remi/php80/root/usr/sbin/php-fpm
注意:一定要授权
chmod 777 /var/opt/remi/php80/run/php-fpm/www.sock
构建映射端口
[root@lvs-server wordpress]# docker run -itd --name word -p 80:80 word:v2.2
8f83a122abe1662f88d6eafe7088883da9fbe4029f340c277e1d0d3251af2b8b
[root@lvs-server wordpress]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8f83a122abe1 word:v2.2 "sh docker-srart.sh" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp word
登录数据库
[root@lvs-server wordpress]# docker run -itd -e MYSQL_ROOT_PASSWORD=qQ111111. -e MYSQL_DATABASE=wordpress mysql:8.0.38-oraclelinux9
a076f210120d87d25c04c7a690b2e35aec7231c34cdfc77ec645fd09ed43cbb0
[root@lvs-server wordpress]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a076f210120d mysql:8.0.38-oraclelinux9 "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 3306/tcp, 33060/tcp relaxed_hopper
a7488c27d87a word:v2.1 "sh docker-srart.sh" 11 minutes ago Up 11 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp word
[root@lvs-server wordpress]# docker exec -it a0 /bin/bash
bash-5.1# mysql -pqQ111111.
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 9
Server version: 8.0.38 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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> show databses;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databses' at line 1
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
mysql>
[1]+ Stopped mysql -pqQ111111.