一、Dockerfile
Dockerfile可以构建容器。
通过指令
1、指令 1、依赖基础镜像的指令,必须在Dockerfile的顶端 FROM 2、执行命令的指令(RUN指令执行的命令,必须是基础容器中支持的命令),将执行的结果保存到镜像中 RUN [root@localhost docker]# vim Dockerfile [root@localhost docker]# cat Dockerfile FROM nginx RUN echo "123" > /usr/share/nginx/html/index.html [root@localhost docker]# docker build -t test:v1 . [root@localhost docker]# docker run -it -d -p 8090:80 test:v1 此时访问192.168.15.105:8090可访问到123 3、添加文件到镜像 ADD 注: 1、ADD支持解压功能(仅支持tar解压) 2、支持远程下载功能,但是如果是远程下载的文件,则不解压 [root@localhost docker]# vim Dockerfile [root@localhost docker]# cat Dockerfile FROM nginx RUN echo "123" > /usr/share/nginx/html/index.html ADD linux.tar.gz /opt COPY linux.tar.gz /mnt [root@localhost docker]# docker build -t test:v2 . [root@localhost docker]# docker run -it test:v2 bash root@3f1acafd8fd5:/# cd /opt drwxr-xr-x 4 root root 55 Dec 27 14:44 linux root@3f1acafd8fd5:/opt# cd /mnt root@3f1acafd8fd5:/mnt# ls -l total 8 -rw-r--r-- 1 root root 4097 Dec 29 11:40 linux.tar.gz 4、复制文件到镜像 COPY 5、暴露端口 EXPOSET 80 443 80/tcp 443/udp [root@localhost docker]# cat Dockerfile FROM nginx RUN echo "123" > /usr/share/nginx/html/index.html ADD linux.tar.gz /opt COPY linux.tar.gz /mnt EXPOSE 80/tcp 443/udp 6、工作目录 WORKDIR(运行容器的第一个目录) [root@localhost docker]# docker run --rm -it test:v5 bash root@bfe58391415c:/# [root@localhost docker]# vim Dockerfile WORKDIR /opt [root@localhost docker]# docker build -t test:v5 . [root@localhost docker]# docker run --rm -it test:v5 bash root@bfe58391415c:/opt# 7、设置容器启动时执行的命令 CMD [root@localhost docker]# vim Dockerfile FROM python:3.6 RUN pip install django ADD linux.tar.gz /opt EXPOSE 80 WORKDIR /opt/linux CMD python manage.py runserver 0.0.0.0:80 [root@localhost docker]# docker build -t test:v6 . [root@localhost docker]# docker run -d --name djangov2 -P test:v6 1b23dc47e7d1326a5b01ac8f68caa83bc77f86cbacb9d1a10050ca16528806cf [root@localhost docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1b23dc47e7d1 test:v6 "/bin/sh -c 'python …" 14 seconds ago Up 14 seconds 0.0.0.0:4000->80/tcp djangov2 此时访问192.168.15.105:4000出现django界面 8、定义Dockerfile变量的指令 ARG [root@localhost docker]# cat Dockerfile FROM python:3.6 RUN pip install django ADD linux.tar.gz /opt ARG package=pymysql RUN pip install $package #构建镜像会下载pymysql EXPOSE 80 WORKDIR /opt/linux CMD python manage.py runserver 0.0.0.0:80 [root@localhost docker]# docker build --build-arg=package=redis -t test:v7 . #另一种定义变量的方式,在外面定义,不需要修改Dockerfile 9、触发器指令(构建时不作任何操作,但是在作为基础镜像时触发) ONBUILD #后面跟指令 FROM test:v7 #作为基础镜像 ONBUILD RUN touch /opt/1.txt 2、构建镜像的命令 docker build -t [镜像名称] [Dockerfile的路径] [root@localhost docker]# docker build -t test:v1 . 注: 1、Dockerfile的文件名 2、一旦开始构建容器,当前目录会被虚拟到Docker容器中
二、利用dockerfile构建 Nginx + Django
[root@localhost ~]# cd work [root@localhost ~]# mkdir django [root@localhost ~]# mkdir nginx #1、Django [root@localhost work]# cd django/ [root@localhost django]# mv ~/docker/linux.tar.gz . [root@localhost django]# cat Dockerfile FROM python:3.6 ADD linux.tar.gz /opt EXPOSE 80 WORKDIR /opt/linux RUN pip install django CMD python manage.py runserver 0.0.0.0:80 [root@localhost django]# docker build -t django:v1 . #2、Nginx [root@localhost work]# cd nginx [root@localhost nginx]# vim default.conf [root@localhost nginx]# cat default.conf server { server_name _; listen 80; location / { proxy_pass http://django; } } [root@localhost nginx]# cat Dockerfile FROM nginx ADD default.conf /etc/nginx/conf.d/ EXPOSE 80 CMD nginx -g 'daemon off;' #前台启动nginx命令 这里不定义workdir也行,因为不需要启动什么命令 [root@localhost nginx]# docker build -t nginx:v1 . [root@localhost nginx]# docker run -d --name django1 --network tiantian django:v1 [root@localhost nginx]# docker run -d --name nginx1 -P --network tiantian nginx:v1
三、容器化BBS
#简版
1、mysql docker run -d --name mysql --network chenyang1 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 2、bbs 1、Dockerfile [root@localhost bbs]# cat bbs/Dockerfile FROM python:3.6 ADD ./ /opt/ WORKDIR /opt RUN pip install -Ur requirements.txt EXPOSE 80 CMD python manage.py runserver 0.0.0.0:80 2、数据迁移 3、nginx [root@localhost nginx]# cat Dockerfile FROM nginx ADD default.conf /etc/nginx/conf.d/ EXPOSE 80 CMD nginx -g 'daemon off;' [root@localhost nginx]# cat default.conf server { server_name _; listen 80; location / { proxy_pass http://bbs; } }
#详细版
[root@localhost ~]# mkdir bbs [root@localhost ~]# cd bbs [root@localhost bbs]# rz -E [root@localhost bbs]# unzip bbs.zip [root@localhost bbs]# cd bbs [root@localhost bbs]# vim requirements.txt django==1.11.11 pymysql [root@localhost bbs]# pip3 install -Ur requirements.txt [root@localhost bbs]# docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 [root@localhost bbs]# docker ps [root@localhost bbs]# vim bbs/settings ["*"] 123456 host:'mysql' #创建bbs [root@localhost bbs]# docker exec -it mysql bash root@1fe839f7ee82:/# mysql -uroot -p123456 root@1fe839f7ee82:/# create database bbs; exit #数据库迁移 [root@localhost bbs]# python3 manage.py makemigrations No changes detected [root@localhost bbs]# ll app01/ [root@localhost bbs]# rm -rf app01/migrations/ [root@localhost bbs]# mkdir app01/migrations/ [root@localhost bbs]# touch app01/migrations/__init__.py [root@localhost bbs]# ll [root@localhost bbs]# python3 manage.py makemigrations [root@localhost bbs]# python3 manage.py migrate [root@localhost bbs]# vim bbs/Dockerfile FROM python:3.6 ADD ./ /opt/ WORKDIR /opt RUN pip install -Ur requirements.txt EXPOSE 80 CMD python manage.py runserver 0.0.0.0:80 [root@localhost bbs]# docker build -t bbs:v1 . [root@localhost bbs]# docker ps [root@localhost bbs]# docker network ls #将mysql加入网桥 [root@localhost bbs]# docker network connect [root@localhost bbs]# docker run -d --name bbs --network tiantian bbs:v1 [root@localhost bbs]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 45d6df27abea bbs:v1 "/bin/sh -c 'python …" 30 seconds ago Up 29 seconds 80/tcp #服务启了,但是没有创建端口,创nginx [root@localhost bbs]# cd .. [root@localhost bbs]# ll total 424 drwxr-xr-x 7 root root 133 Dec 29 22:53 bbs -rw-r--r-- 1 root root 431029 Dec 29 16:42 bbs.zip [root@localhost bbs]# mkdir nginx [root@localhost bbs]# cd nginx/ [root@localhost nginx]# vim Dockerfile FROM nginx ADD default.conf /etc/nginx/conf.d/ EXPOSE 80 CMD nginx -g 'daemon off;' [root@localhost nginx]# vim default.conf server { server_name _; listen 80; location / { proxy_pass http://bbs; } } [root@localhost nginx]# docker build -t nginx:v2 . [root@localhost nginx]# docker run -d --name nginxv2 --network tiantian -p 8091:80 nginx:v2 [root@localhost nginx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 36f2694a8a93 nginx:v2 "/docker-entrypoint.…" 16 seconds ago Up 15 seconds 0.0.0.0:8091->80/tcp nginxv2 45d6df27abea bbs:v1 "/bin/sh -c 'python …" 14 minutes ago Up 14 minutes 80/tcp bbs 1fe839f7ee82 mysql:5.7 "docker-entrypoint.s…" 43 minutes ago Up 43 minutes 0.0.0.0:3306->3306