A, commit mirror package
We ubuntu mirroring an example:
First, there is a mirror image of ubentu
[root@server1 ~]# docker images ubuntu
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 07c86167cdc4 3 years ago 188MB
We first establish vm1 container, and do some operation, then ctrl + p + q quit
[root@server1 ~]# docker run -it --name vm1 ubuntu
root@6074aca22100:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
root@6074aca22100:/# touch file{1..10}
root@6074aca22100:/# ls
bin etc file2 file5 file8 lib mnt root srv usr
boot file1 file3 file6 file9 lib64 opt run sys var
dev file10 file4 file7 home media proc sbin tmp
Whether on the basis of the above we remove vm1 container, and then re-create, view documents created there
[root@server1 ~]# docker rm -f vm1
vm1
[root@server1 ~]# docker run -it --name vm1 ubuntu
root@94a7b7a6ffc0:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
We can not find the file we created, our operations have not been saved.
So we need to let it continue to exist if it is necessary to re-establish and re-commit to the package
root@94a7b7a6ffc0:/# touch file{1..10}
root@94a7b7a6ffc0:/# ls
bin etc file2 file5 file8 lib mnt root srv usr
boot file1 file3 file6 file9 lib64 opt run sys var
dev file10 file4 file7 home media proc sbin tmp
ctrl + d exit release resources, mirror package
root@94a7b7a6ffc0:/# exit
[root@server1 ~]# docker commit -m "add files" vm1 ubuntu:v1
sha256:387e3d445215ca2e177b4c8f0ae3807edffd919a13be237ebcc5e69c4f1523b8
We then use our resealable container mirroring operation.
[root@server1 ~]# docker run -it --name vm2 ubuntu:v1
root@a5fe8bd9c26b:/# ls
bin etc file2 file5 file8 lib mnt root srv usr
boot file1 file3 file6 file9 lib64 opt run sys var
dev file10 file4 file7 home media proc sbin tmp
We can find our previous documents still being preserved
Two, Dockerfile preparation, the package mirror
Environmental clean-up before the experiment before
docker rm -f vm1
docker rm -f vm2
docker ps -a
docker rmi ubuntu:v1
- Write Dockerfile realization install httpd
1. The introduction of the mirror rhel7
[root@server1 ~]# docker load -i rhel7.tar
e1f5733f050b: Loading layer 147.1MB/147.1MB
2. Write Dockerfile
[root@server1 ~]# docker load -i rhel7.tar
e1f5733f050b: Loading layer 147.1MB/147.1MB
[root@server1 ~]# cd /tmp/docker/
[root@server1 docker]# vim Dockerfile
[root@server1 docker]# cat Dockerfile
FROM rhel7 #源镜像是rhel7,最好将rhel7的镜像放在本地
COPY yum.repo /etc/yum.repos.d/yum.repo ##复制一个yum源
RUN rpmdb --rebuilddb && yum install -y httpd ##执行命令安装httpd并清除yum缓存
##rpmdb命令用于初始化和重建rpm数据库 --rebuilddb 从已安装的包头文件,反向重建RPM数据库
EXPOSE 80 ##定义端口
CMD ["/usr/sbin/httpd","-D","FOREGROUND"] #打开apache服务-D是全局文件/etc/sysconfig/httpd中打开的参数
3. Write yum.repo (the current directory)
[root@server1 docker]# vim yum.repo
[root@server1 docker]# ca yum.repo
-bash: ca: command not found
[root@server1 docker]# cat yum.repo
[rhel7.3]
name=rhel7.3
baseurl=http://172.25.66.250/rhel7.3
gpgcheck=0
4. The mirror package, and test whether the use of
[root@server1 docker]# docker build -t rhel7:v1 .
注意后面的点,表示当前目录,我们设置其标签为v1
Finally, we can find to write successful
[root@server1 docker]# docker images rhel7:v1
REPOSITORY TAG IMAGE ID CREATED SIZE
rhel7 v1 41e959659080 2 minutes ago 193MB
5. We use the new image to run a new container, and the default publishing httpd directory where the file to the default httpd mount the directory publishing
[root@server1 ~]# cd /tmp/docker/
[root@server1 docker]# ls
Dockerfile index.html yum.repo
[root@server1 docker]# mkdir web
[root@server1 docker]# mv index.html web/
[root@server1 docker]# docker run -d --name vm1 -p 80:80 -v /tmp/docker/web/:/var/www/html rhel7:v1
3f1d0e90e349c373c879f361476e81ed2922637035de56962041053bb423d80d
[root@server1 docker]# curl 172.25.66.1
hello
hello
hello
6. We modify the default publishing files, and then continue to access the test
[root@server1 docker]# cd web/
[root@server1 web]# echo www.ljz.org > index.html
[root@server1 web]# curl 172.25.66.1
www.ljz.org
We can see if we can modify the default publishing interface
Three, Dockerfile write frequently used instructions
- ADD
usage and COPY is similar, except that the src can be compressed archive file, the file will be automatically extracted to dest, can also automatically download and copy the URL to the mirror:
ADD html.tar / var / the WWW
ADD HTTP: // ip / HTML .tar / var / WWW - ENV
setting environment variables, which can be a subsequent instruction uses:
ENV HOSTNAME sevrer1.example.com - EXPOSE
If you run an application service container can be exposed to the service port:
EXPOSE 80 - VOLUME
stated data volume, the data is usually specified in the application hanging point:
VOLUME [ "/ var / WWW / HTML"] - WORKDIR
RUN, CMD, EntryPoint, and the ADD COPY command set is mirrored in the current work
for the directory, if the directory does not exist will be created automatically. - RUN
Run command in the container and create a new image layer, commonly used in the installation package:
RUN yum install -y vim - CMD differs ENTRYPOINT:
These two instructions are executed by a command for setting the container after the start, but the CMD is the command line docker run behind the cover, and will not be ignored EntryPoint, will be performed - Docker run behind the arguments can be passed as an argument to the command ENTRYPOINT.
- Dockerfile can only specify a ENTRYPOINT, if you specify a number, only the last one is valid.
Examples
1. First we import a character image output
[root@server1 ~]# ls
busybox.tar docker game2048.tar nginx.tar rhel7.tar ubuntu.tar
[root@server1 ~]# docker load -i busybox.tar
8a788232037e: Loading layer 1.37MB/1.37MB
Loaded image: busybox:latest
2. Write Dockerfile file
[root@server1 docker]# ls
Dockerfile web yum.repo
[root@server1 docker]# vim Dockerfile
[root@server1 docker]# cat Dockerfile
FROM busybox
ENV name world #设置环境变量
ENTRYPOINT echo "hello,$name"
3. create mirror container operation
[root@server1 docker]# docker run --rm busybox:v1 #运行容器后删除
hello,world
4. Here it is necessary to explain a problem, we just used when output is the echo, he is actually a shell format, of course, we can also use the output format exec, will call the bottom shell format / bin / sh -c to execute the command, you can resolve the variable, and can not resolve the variables exec
[root@server1 docker]# vim Dockerfile
[root@server1 docker]# cat Dockerfile
FROM busybox
ENV name world
ENTRYPOINT ["/bin/echo","hello,$name"]
So if we must use exec format it, so can we
[root@server1 docker]# vim Dockerfile
[root@server1 docker]# cat Dockerfile
FROM busybox
ENV name world
ENTRYPOINT ["/bin/sh","-c","echo hello,$name"]
5. Use ENTRYPOINT and CMD
[root@server1 docker]# vim Dockerfile
[root@server1 docker]# cat Dockerfile
FROM busybox
ENTRYPOINT ["/bin/echo","hello"]
CMD ["world"]
Of course, we cmd content can also be covered