docker image 概述
官方的docker image 仓库源码地址
https://github.com/docker-library
对应开源的image 源码项目这里都有
DockerFile 文件举例
https://github.com/docker-library/mysql/blob/master/5.7/Dockerfile
FROM debian:stretch-slim
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r mysql && useradd -r -g mysql mysql
RUN apt-get update && apt-get install -y --no-install-recommends gnupg dirmngr && rm -rf /var/lib/apt/lists/*
# add gosu for easy step-down from root
ENV GOSU_VERSION 1.7
RUN set -x \
&& apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \
&& wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \
&& wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \
&& export GNUPGHOME="$(mktemp -d)" \
&& gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \
&& gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \
&& gpgconf --kill all \
&& rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc \
&& chmod +x /usr/local/bin/gosu \
&& gosu nobody true \
&& apt-get purge -y --auto-remove ca-certificates wget
RUN mkdir /docker-entrypoint-initdb.d
RUN apt-get update && apt-get install -y --no-install-recommends \
# for MYSQL_RANDOM_ROOT_PASSWORD
pwgen \
# for mysql_ssl_rsa_setup
openssl \
# FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db:
# File::Basename
# File::Copy
# Sys::Hostname
# Data::Dumper
perl \
&& rm -rf /var/lib/apt/lists/*
RUN set -ex; \
# gpg: key 5072E1F5: public key "MySQL Release Engineering <mysql-build@oss.oracle.com>" imported
key='A4A9406876FCBD3C456770C88C718D3B5072E1F5'; \
export GNUPGHOME="$(mktemp -d)"; \
gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
gpg --batch --export "$key" > /etc/apt/trusted.gpg.d/mysql.gpg; \
gpgconf --kill all; \
rm -rf "$GNUPGHOME"; \
apt-key list > /dev/null
ENV MYSQL_MAJOR 5.7
ENV MYSQL_VERSION 5.7.28-1debian9
RUN echo "deb http://repo.mysql.com/apt/debian/ stretch mysql-${MYSQL_MAJOR}" > /etc/apt/sources.list.d/mysql.list
# the "/var/lib/mysql" stuff here is because the mysql-server postinst doesn't have an explicit way to disable the mysql_install_db codepath besides having a database already "configured" (ie, stuff in /var/lib/mysql/mysql)
# also, we set debconf keys to make APT a little quieter
RUN { \
echo mysql-community-server mysql-community-server/data-dir select ''; \
echo mysql-community-server mysql-community-server/root-pass password ''; \
echo mysql-community-server mysql-community-server/re-root-pass password ''; \
echo mysql-community-server mysql-community-server/remove-test-db select false; \
} | debconf-set-selections \
&& apt-get update && apt-get install -y mysql-server="${MYSQL_VERSION}" && rm -rf /var/lib/apt/lists/* \
&& rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld \
&& chown -R mysql:mysql /var/lib/mysql /var/run/mysqld \
# ensure that /var/run/mysqld (used for socket and lock files) is writable regardless of the UID our mysqld instance ends up having at runtime
&& chmod 777 /var/run/mysqld \
# comment out a few problematic configuration values
&& find /etc/mysql/ -name '*.cnf' -print0 \
| xargs -0 grep -lZE '^(bind-address|log)' \
| xargs -rt -0 sed -Ei 's/^(bind-address|log)/#&/' \
# don't reverse lookup hostnames, they are usually another container
&& echo '[mysqld]\nskip-host-cache\nskip-name-resolve' > /etc/mysql/conf.d/docker.cnf
VOLUME /var/lib/mysql
COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 3306 33060
CMD ["mysqld"]
DockerFile 属性
From
功能:指定基础的镜像,这些镜像的版本,也都是image
FROM ubuntu:14.04
上面是使用ubuntu的镜像(image) 14.04版本
ENV
功能:设置变量的值,ENVMYSQL_MAJOR5.7,可以通过docker run --e key=value修改,后面可以直接使用${MYSQL_MAJOR}
ENV GOSU_VERSION 1.7
RUN
功能:运行指定的命令
这里的Run执行的命令主要是linux系统的一些内部的一些命令,如安装软件以及配置环境变量等等;
样例:
RUN groupadd -r mysql && useradd -r -g mysql mysql
CMD
功能:容器启动(执行docker run 命令)的时候默认会执行的命令,若有多个CMD命令,则最后一个生效
RUN 可以配置多个,依次执行
同时,这里注意下,这个也可以在docker run的时候,重新指定,覆盖之前的
(比如DockerFile中设置了端口80端口,我们可以通过docker run的时候重新指定端口)
ENTRYPOINT
VOLUME
功能:数据存储的目录
样例:
VOLUME /var/lib/mysql
EXPOSE
功能:暴露的端口,这里注意下,第一个是虚拟机里的端口,第二个是宿主主机
EXPOSE 3306 33060
COPY
功能:将主机的文件复制到镜像内,如果目录不存在,会自动创建所需要的目录,注意只是复制,不会提取和解压
COPY ${主机的文件} ${镜像内文件目录}
COPY /user/docker/file.sh /usr/local/bin/
ADD
功能:将主机的文件复制到镜像内,和COPY类似,只是ADD会对压缩文件提取和解压
ADD application.yml /etc/file/
WORKDIR
功能:指定镜像的工作目录,之后的命令都是基于此目录工作,若不存在则创建
WORKDIR /usr/local
WORKDIR tomcat
RUN touch test.txt
如何理解DockerFile文件的步骤
1.对于DockerFile里面的步骤,我们可以根据上面的这些属性命令去计算,一个属性命令,我们可以认为是一个步骤;
2.DockerFile里面的属性步骤,实际上就是对依赖环境的搭建;
DockerFile 第一个DockerFile的使用
整体步骤
1.准备一个SpringBoot项目, XXX.java的文件
当然,对于Springboot项目我们可以通过 java -jar的形式去在本地或者服务器上启动;
2.打包:mvn clean package
3.创建DockerFile文件,根据DockerFile文件的相关配置生成Image
4.运行我们的Image,创建容器Container
具体步骤
创建SpringBoot项目
1.这里我们自己准备了一个Springboot项目,参考下面的git地址
git地址
https://gitee.com/gaoxinfu_admin/demo-tool
打包:mvn clean package
在项目当前的目录下面执行下面的命令
localhost:demo-tool gaoxinfu$ pwd
/Users/gaoxinfu/demo-tool
localhost:demo-tool gaoxinfu$ mvn clean package
打包完成,如下:
/Users/gaoxinfu/demo-tool/target/demo-tool-1.0.0-SNAPSHOT.jar
创建DockerFile文件
# 指定基础镜像的依赖,我们的springboot项目依赖于jdk1.8
FROM openjdk:8
# 设置一下维护者
MAINTAINER gaoxinfu
# 设置一下标签,这里的LABEL 可以认为是key-value的形式
LABEL name="docker-first-demo" version="1.0.0" author='gaoxinfu'
# 将当前目录下面的我们项目打出来的jar包复制到docker跟目录下面,我们也叫 docker-first-demo-1.0.0.snapshot.jar名字
COPY demo-tool-1.0.0-SNAPSHOT.jar demo-tool-1.0.0-SNAPSHOT.jar
# 在我们执行docker run 启动的时候 自动执行下面的名字 java -jar demo-tool-1.0.0-SNAPSHOT.jar
CMD ["java","-jar","demo-tool-1.0.0-SNAPSHOT.jar"]
根据Dockerfile 编译image
[root@localhost ~]# docker build -t demo-tool-docker .
Sending build context to Docker daemon 17.59MB
Step 1/5 : FROM openjdk:8
8: Pulling from library/openjdk
844c33c7e6ea: Pull complete
ada5d61ae65d: Pull complete
f8427fdf4292: Pull complete
f025bafc4ab8: Pull complete
67b8714e1225: Pull complete
64b12da521a3: Pull complete
2e38df533772: Pull complete
Digest: sha256:e1415d5d2a41e2f4f44c1a48f2f268a9da5d7fcd1e9b2773d32f4645b30ec69b
Status: Downloaded newer image for openjdk:8
---> 09df0563bdfc
Step 2/5 : MAINTAINER gaoxinfu
---> Running in afa820d1419b
Removing intermediate container afa820d1419b
---> 3239bcc43262
Step 3/5 : LABEL name="docker-first-demo" version="1.0.0" author='gaoxinfu'
---> Running in ab98141bf820
Removing intermediate container ab98141bf820
---> de012c126826
Step 4/5 : COPY docker-first-demo-1.0.0.snapshot.jar docker-first-demo-1.0.0.snapshot.jar
COPY failed: stat /var/lib/docker/tmp/docker-builder146757200/docker-first-demo-1.0.0.snapshot.jar: no such file or directory
[root@localhost ~]# docker build -t demo-tool-docker .
Sending build context to Docker daemon 17.59MB
Step 1/5 : FROM openjdk:8
---> 09df0563bdfc
Step 2/5 : MAINTAINER gaoxinfu
---> Using cache
---> 3239bcc43262
Step 3/5 : LABEL name="docker-first-demo" version="1.0.0" author='gaoxinfu'
---> Using cache
---> de012c126826
Step 4/5 : COPY ./docker-first-demo-1.0.0.snapshot.jar docker-first-demo-1.0.0.snapshot.jar
COPY failed: stat /var/lib/docker/tmp/docker-builder398471284/docker-first-demo-1.0.0.snapshot.jar: no such file or directory
[root@localhost ~]# docker build -t demo-tool-docker .
Sending build context to Docker daemon 17.59MB
Step 1/5 : FROM openjdk:8
---> 09df0563bdfc
Step 2/5 : MAINTAINER gaoxinfu
---> Using cache
---> 3239bcc43262
Step 3/5 : LABEL name="docker-first-demo" version="1.0.0" author='gaoxinfu'
---> Using cache
---> de012c126826
Step 4/5 : COPY demo-tool-1.0.0-SNAPSHOT.jar demo-tool-1.0.0-SNAPSHOT.jar
---> 1e3fcc21182c
Step 5/5 : CMD ["java","-jar","demo-tool-1.0.0-SNAPSHOT.jar"]
---> Running in 9204a3d7536e
Removing intermediate container 9204a3d7536e
---> 79e08d8bb28b
Successfully built 79e08d8bb28b
Successfully tagged demo-tool-docker:latest
[root@localhost ~]#
1.可以看到一共5步(主要是Dockfile属性的命令)
2.通过下面docker images 我们可以看到我们的images已经编译完成
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
demo-tool-docker latest 79e08d8bb28b About a minute ago 506MB
openjdk 8 09df0563bdfc 2 days ago 488MB
tomcat jdk8-adoptopenjdk-hotspot 50d274ef4fb3 2 weeks ago 324MB
tomcat latest 882487b8be1d 5 weeks ago 507MB
hello-world latest fce289e99eb9 10 months ago 1.84kB
[root@localhost ~]#
启动demo-tool-docker这个image
[root@localhost ~]# docker run -d --name demo-tool-docker-01 demo-tool-docker
bc6f42106fda0312ea6644ec3d3e55a5fcd86d402bd8365b662495b6cf3f93a6
[root@localhost ~]# ip a
查看项目启动日志
[root@localhost ~]# docker logs demo-tool-docker-01
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.1.RELEASE)
2019-11-26 04:53:13.210 INFO 1 --- [ main] c.g.demo.tool.DemoToolApplication : Starting DemoToolApplication v1.0.0-SNAPSHOT on bc6f42106fda with PID 1 (/demo-tool-1.0.0-SNAPSHOT.jar started by root in /)
2019-11-26 04:53:13.215 INFO 1 --- [ main] c.g.demo.tool.DemoToolApplication : No active profile set, falling back to default profiles: default
2019-11-26 04:53:15.711 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-11-26 04:53:15.774 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-11-26 04:53:15.775 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-26 04:53:15.979 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-11-26 04:53:15.979 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2660 ms
2019-11-26 04:53:16.979 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-26 04:53:17.447 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-11-26 04:53:17.453 INFO 1 --- [ main] c.g.demo.tool.DemoToolApplication : Started DemoToolApplication in 4.991 seconds (JVM running for 6.135)
[root@localhost ~]#
访问
进入容器进行访问
[root@localhost ~]# docker exec -it demo-tool-docker-01 sh
# ls -la
total 17156
drwxr-xr-x. 1 root root 17 Nov 26 04:53 .
drwxr-xr-x. 1 root root 17 Nov 26 04:53 ..
-rwxr-xr-x. 1 root root 0 Nov 26 04:53 .dockerenv
drwxr-xr-x. 1 root root 179 Nov 23 14:33 bin
drwxr-xr-x. 2 root root 6 Sep 8 10:51 boot
-rw-r--r--. 1 root root 17566826 Nov 26 04:28 demo-tool-1.0.0-SNAPSHOT.jar
drwxr-xr-x. 5 root root 340 Nov 26 04:53 dev
drwxr-xr-x. 1 root root 66 Nov 26 04:53 etc
drwxr-xr-x. 2 root root 6 Sep 8 10:51 home
drwxr-xr-x. 1 root root 30 Nov 18 00:00 lib
drwxr-xr-x. 2 root root 34 Nov 18 00:00 lib64
drwxr-xr-x. 2 root root 6 Nov 18 00:00 media
drwxr-xr-x. 2 root root 6 Nov 18 00:00 mnt
drwxr-xr-x. 2 root root 6 Nov 18 00:00 opt
dr-xr-xr-x. 101 root root 0 Nov 26 04:53 proc
drwx------. 1 root root 24 Nov 23 14:34 root
drwxr-xr-x. 3 root root 30 Nov 18 00:00 run
drwxr-xr-x. 1 root root 20 Nov 23 00:13 sbin
drwxr-xr-x. 2 root root 6 Nov 18 00:00 srv
dr-xr-xr-x. 13 root root 0 Nov 26 04:22 sys
drwxrwxrwt. 1 root root 115 Nov 26 04:53 tmp
drwxr-xr-x. 1 root root 19 Nov 18 00:00 usr
drwxr-xr-x. 1 root root 19 Nov 18 00:00 var
# curl localhost:8080/hello
Hello Docker!#
当然我们也可以通过虚拟机的ip去访问
http://10.56.34.234:8080/hello
1.这里注意下,我们没有指定端口,所以springboot 默认端口是8080