Environment: OS X 10.10.5
Maven 3.3.9
Docker version 1.12.2
Docker-machine version 0.8.2
The program example is the service Discovery program mentioned in http://www.cnblogs.com/modprobe/p/6001333.html
Related code: Https://github.com/li-hong/spring-cloud-demo
First, the preparation work, installs the Docker-toolbox
Method: Http://www.docker.com/products/docker-toolbox
After installation, use Docker-machine to create a virtual machine with a Docker runtime environment, or run Toolbox build directly
Command:
Docker-machine Create--driver VirtualBox default
Start a virtual machine
Docker-machine Start Default
Stop it
Docker-machine Stop Default
Log in to default using SSH
SSH Default
More commands to view using the following command
Docker-machine Help
View Docker Environments
Env
That's when the prep work is done.
Ii. Preparation of Dockerfile documents
From Java:8 #基础镜像VOLUME/tmp #将本地文件挂载到容器ADD Server-discovery-1.0-snapshot.jar app.jar #拷贝文件到容器RUN bash-C'Touch/app.jar' #运行命令EXPOSE8761 # Specifies the port that the container needs to map to the host machineentrypoint ["Java","-djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] #容器启动后执行的命令
Third, build the image
Using Docker-manven-plugin to build MAVEN images, you can see how docker-maven-plugin is used in GitHub
Here's an overview:
Add a plugin to the POM
<plugin> <groupId>Com.spotify</groupId> <Artifactid>Docker-maven-plugin</Artifactid> <version>${docker.plugin.version}</version> <executions> <Execution> <Phase>Package</Phase> <Goals> <goal>Build</goal> </Goals> </Execution> </executions> <Configuration> <ImageName>Lhdocker.com/${project.artifactid}</ImageName> <Baseimage>Java</Baseimage> <dockerdirectory>${project.basedir}/src/main/docker</dockerdirectory> <Dockerhost>https://192.168.99.100:2376</Dockerhost> <Dockercertpath>/users/lihong/.docker/machine/machines/default</Dockercertpath> <Resources> <Resource> <TargetPath>/</TargetPath> <Directory>${project.build.directory}</Directory> <include>${project.build.finalname}.jar</include> </Resource> </Resources> </Configuration> </plugin>
Notice what time
1, Dockerhost in the configuration to use the Docker-machine EVN command output docker_host
2, imagename in the lhdocker.com for their own private warehouse building, construction method see: Docker Registry-v2 build a private warehouse
3, if you want to upload to the private warehouse, and enable the user name password login, then you need to connect to the default in the login
SSHlogin lhdocker.com
After the configuration is complete, you can run the following command to build the image
MVN Clean Package Docker:build
If you succeed, you will see the following output
Build the image and upload it to the private warehouse
MVN Clean Package Docker:build -dpushimage
After the success you will see the following additional output
Using Docker-images to view mirrors
Perform pull Tests
Now we're done using Docker-maven-plugin to build the Docker image and upload it to the private repository.
Iv. the pits encountered
1, not good environment preparation, direct operation
MVN Clean Package Docker:build
Encountered the following error output
[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:0.4. A 127.0. 0.1:4243 [/127.0. 0.11]
Solution, install the Docker-toolbox, and in the Pom species configuration Dockerhost
< Dockerhost >https://192.168.99.100:2376</dockerhost>< Dockercertpath>/users/lihong/.docker/machine/machines/default</ Dockercertpath>
2, run MVN Clean package Docker:build-dpushimage and encountered the following error
[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:0.4.12:build (default) on Project Server-discovery:exception Caught:an HTTPS URI for Docker_host must is provided to use DOCKER client certificates, [Help 1]
Cause: Because the registry private warehouse is configured with its own certificate, Docker is not recognized, so in the default virtual machine type also need to import the certificate, the method is as follows
Where Lhdocker.com is doing the hosts map
SCP [Email protected]:/etc/docker/certs.d/lhdocker.com/lhdocker.com.crt/etc/docker/certs.d/lhdocker.com/
3. Read more official documents and think more
V. See the documentation
Https://www.gitbook.com/book/wzlee/spring-cloud/details
People have problems can leave a message, solve together, study together
Use Dockerfile to build images and push to a private warehouse