通过Dockerfile来进行Docker的镜像制作,本文章只演示镜像打包
- FROM 使用的镜像,如果没有则会拉取
- MAINTAINER 作者
- COPY 拷贝文件至镜像指定位置
- RUN 镜像制作时执行的命令
- ADD:将文件复制到容器中,支持URL和解压缩
- CMD:指定容器启动时要执行的命令
- ENTRYPOINT:指定容器启动时要执行的命令,与CMD不同的是,ENTRYPOINT不会被docker run命令行参数覆盖
- ENV:设置环境变量
- ARG:定义构建参数
准备基础环境
拉取centos7镜像:
[root@master docker]# docker pull centos:7
pull下载过慢可下载tar包并进行导入
[root@master ~]# wget https://file.lolicp.com/docker/images/centos_7.tar
[root@master ~]# docker load -i centos_7.tar
构建环境
Mysql
编辑Dockerfile文件内容
[root@master mysql]# vim Dockerfile
FROM docker.io/centos:7
MAINTAINER <admin@lolicp.com>
COPY mysql_57/ /usr/local/mysql_57/
RUN yum -y install openssl-libs
开始build构建Mysql镜像
[root@master mysql]# docker build -t mysql:v5.7.29 ./
Sending build context to Docker daemon 1.694 GB
Step 1/4 : FROM docker.io/centos:7
---> b5b4d78bc90c
Step 2/4 : MAINTAINER <admin@lolicp.com>
---> Using cache
---> 06fa3abfc1cf
Step 3/4 : COPY mysql_57/ /usr/local/mysql_57/
---> Using cache
---> 7e7a1a1a78c4
Step 4/4 : RUN yum -y install openssl-libs
---> Using cache
---> 7d9d2bac5b99
Successfully built 7d9d2bac5b99
Nginx
编辑Dockerfile文件内容
[root@master nginx]# vim Dockerfile
FROM docker.io/centos:7
MAINTAINER <admin@lolicp.com>
COPY nginx/ /usr/local/nginx/
RUN yum -y install gd-devel pcre-devel openssl openssl-devel
开始build构建Nginx镜像
[root@master nginx]# docker build -t nginx:v1.17.10 ./
PHP
编辑Dockerfile文件内容
[root@master php]# vim Dockerfile
FROM docker.io/centos:7
MAINTAINER <admin@lolicp.com>
COPY php72/ /usr/local/php72/
RUN yum -y install epel-release && yum -y install libpng autoconf libxslt libjpeg-turbo freetype oniguruma-devel
开始build构建PHP镜像
[root@master php]# docker build -t php:v7.2 ./
查看结果
查看images列表
[root@master nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v1.18 20dabe80ad0c 2 minutes ago 306 MB
php v7.2 1d279f0043d9 31 minutes ago 520 MB
mysql v5.7.29 7d9d2bac5b99 49 minutes ago 1.97 GB
centos 7 b5b4d78bc90c 4 weeks ago 203 MB
docker.io/centos 7 b5b4d78bc90c 4 weeks ago 203 MB
通过docker-compose启动容器
配置docker-compose
[root@master ~]# wget -O /usr/local/bin/docker-compose https://file.lolicp.com/docker/sbin/docker-compose
[root@master ~]# chmod +x /usr/local/bin/docker-compose
编辑启动文件并进行配置,根据实际需要进行配置
由于配置是根据自己实际需要进行修改的,此教程仅供参考。如果根据教程来你会缺少配置文件及启动脚本。
[root@master conf]# vim WebSite_All.yaml
version: '2.3'
services:
nginx:
image: nginx:v1.18
container_name: nginx
cpu_count: 1
cpu_percent: 50
cpus: 0.5
cpu_shares: 73
cpuset: 0,1
mem_limit: 512m
memswap_limit: 1024m
mem_reservation: 125m
shm_size: 64M
hostname: nginx-server
command: /bin/bash /usr/local/bin/nginxd start
ports:
- "80:80"
- "443:443"
volumes:
- /data/nginx/sbin/nginxd.sh:/usr/local/bin/nginxd
- /data/nginx/wwwroot:/wwwroot/
- /data/nginx/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf
- /data/nginx/ssl/:/usr/local/nginx/ssl/
- /data/nginx/vhost/:/usr/local/nginx/vhost/
- /dev/shm/php-fpm.sock:/dev/shm/php-fpm.sock
- /etc/hosts:/etc/hosts
networks:
website:
ipv4_address: 10.173.68.88
mysql_db:
container_name: mysql_57
image: mysql:v5.7.29
cpu_count: 1
cpu_percent: 50
cpus: 1
cpu_shares: 73
cpuset: 0,1
mem_limit: 1024m
memswap_limit: 2049m
mem_reservation: 248m
shm_size: 64M
command: /bin/bash /usr/local/mysql_57/sbin/mysqld.sh
hostname: mysql-server
volumes:
- /data/mysql/data:/usr/local/mysql_57/data
- /data/mysql/conf:/usr/local/mysql_57/conf
- /data/mysql/logs:/usr/local/mysql_57/logs
- /data/mysql/sbin:/usr/local/mysql_57/sbin
- /data/mysql/backup:/usr/local/mysql_57/backup
networks:
website:
ipv4_address: 10.173.68.77
php_fpm:
container_name: php
image: php:v7.2
cpu_count: 1
cpu_percent: 50
cpus: 1
cpu_shares: 73
cpuset: 0,1
mem_limit: 1024m
memswap_limit: 1621m
mem_reservation: 248m
shm_size: 64M
hostname: php-server
command: /bin/bash /usr/local/bin/phpd start
volumes:
- /data/php/sbin/phpd:/usr/local/bin/phpd
- /data/php/php.ini:/usr/local/php72/php.ini
- /data/php/ext/:/usr/local/php72/lib/php/extensions/no-debug-non-zts-20170718/
- /data/php/etc/:/usr/local/php72/etc/
- /data/nginx/wwwroot:/wwwroot/
- /dev/shm/php-fpm.sock:/dev/shm/php-fpm.sock
- /etc/hosts:/etc/hosts
networks:
website:
ipv4_address: 10.173.68.99
networks:
website:
ipam:
config:
- subnet: 10.173.68.0/24
gateway: 10.173.68.1
启动容器
[root@master conf]# docker-compose -f /data/docker/conf/WebSite_All.yaml up -d
Creating network "conf_website" with the default driver
Creating nginx ... done
Creating php ... done
Creating mysql_57 ... done
上传镜像至dockerhub
登陆docker
[root@master conf]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one
Username: ymcc
Password:
Login Succeeded
更改tag为用户名开头
[root@master conf]# docker tag mysql:v5.7.29 ymcc/mysql:v5.7.29
[root@master conf]# docker tag php:v7.2 ymcc/php:v7.2
[root@master conf]# docker tag nginx:v1.18 ymcc/nginx:v1.18
上传镜像
[root@master conf]# docker push ymcc/mysql:v5.7.29
[root@master conf]# docker push ymcc/php:v7.2
[root@master conf]# docker push ymcc/nginx:v1.18