Docker series two create your own docker and Dockerfile syntax

Docker series two create your own docker and Dockerfile syntax

Create your own docker

  1. The test server has docker installed
  2. The main code for writing the test project is as follows
    : DockerApplication: Service class
@SpringBootApplication
@RestController
public class DockerApplication {

    @RequestMapping("/")
    public String home() {
        return "Hello Docker World";
    }

    public static void main(String[] args) {
        SpringApplication.run(DockerApplication.class, args);
    }
} 

For the code details, see github
3. Execute pom.xml to generate docker-0.0.1-SNAPSHOT.jar and
copy the docker-0.0.1-SNAPSHOT.jar generated above to the working directory such as:/docker/simplebuild
4. In the working directory, Add Dockerfile, the content is as follows

FROM openjdk
MAINTAINER hryou0922
ENV JAVA_HOME/usr/local/java
RUN echo $JAVA_HOME
ADD docker-0.0.1-SNAPSHOT.jar/home/hry/docker/simplebuild/contain/app.jar
#  
RUN useradd hryou0922
RUN echo "hryou0922:hryou0922" | chpasswd 
#    
USER hryou0922
#  
VOLUME/home/hry/docker/simplebuild
#  
WORKDIR/home/hry/docker/simplebuild/contain/

EXPOSE 8080
ENTRYPOINT ["java","-jar" ,"/home/hry/docker/simplebuild/contain/app.jar"]

 

Introduction This chapter describes the syntax behind Dockerfile
5. Establish imange and run the
command in the current directory execute the command to create image

```
 docker build -t cnbao/myjdk .
```
 

"." at the end of the command indicates that the Dockerfile is in the current path

Check the current image list and find that the image has been generated:

docker images
    REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
    cnbao/myjdk               latest              9c69de534f81        3 days ago          655.8 MB 

Create a container and run

hry@hry-VirtualBox:/home/share$ docker run -d --name test4 -p 8080:8080  cnbao/myjdk
57702e0c5c60252ffac2b8832c2cb455aeab62957e63013daeb74a0e47bb2a63 

Access the service interface, indicating that the service has been started successfully

hry@hry-VirtualBox:/home/share$ curl http://127.0.0.1:8080/
Hello Docker Worldhry 

Dockerfile syntax

Dockerfile instructions are case-ignoring, it is recommended to use uppercase, use # as a comment, each line only supports one instruction

  1. FROM:
    destination: specify the basic image, which means the new program is based on which image to build the new image
    syntax:
    FROM <image>:<tag>
    tag: version number, if not, use the latest version

  2. MAINTAINER Specifies the information of the creator of the mirror

       MAINTAINER <name>   
  3. RUN
    RUN basic image can be run any supported command
    syntax:

    RUN <command> (the command is run in a shell - `/bin/sh -c`)  
     
    RUN ["executable", "param1", "param2" ... ]  (exec form)   

  4. The operation that runs when the CMD container is started. This instruction can only exist once in the file, if there are more than one, only the last one will be executed

    CMD ["executable","param1","param2"] (like an exec, this is the preferred form)

    CMD command param1 param2 (as a shell)
  5. ENTRYPOINT sets the operation to be performed when the container is started. This instruction can only exist once in the file, if there are more than one, only the last one will be executed

     ENTRYPOINT ["executable", "param1", "param2"] (like an exec, the preferred form)  
       
    ENTRYPOINT command param1 param2 (as a shell)   

    The use of this instruction is divided into two situations, one is to use it alone. When used alone, if you also use the CMD command and CMD is a complete executable command, then the CMD command and ENTRYPOINT will overwrite each other. Only the last CMD or ENTRYPOINT is valid

    
    # CMD ENTRYPOINT   
    
    CMD echo  Hello, World!   
    ENTRYPOINT ls -l  
     

    Another usage is used in conjunction with the CMD command to specify the default parameters of ENTRYPOINT. At this time, the CMD command is not a complete executable command, only the parameter part; the ENTRYPOINT command can only use JSON to specify the execution command, but not the parameters.

    FROM ubuntu  
    CMD ["-l"]  
    ENTRYPOINT ["/usr/bin/ls"]   
  6. USER Set the user who starts the container, the default is the root user. Specify the running user daemon of memcached

    
    #  memcached   
    
    ENTRYPOINT ["memcached"]  
    USER daemon  
       
    ENTRYPOINT ["memcached", "-u", "daemon"]   
  7. EXPOSE specifies that the container needs to be mapped to the port of the host machine

     EXPOSE <port> [<port>...] 
    
    #    
    
    EXPOSE port1  
    
    #    
    
    
    docker run -p port1 image  
    =====================================
    
    
    #    
    
    EXPOSE port1 port2 port3  
    
    #    
    
    docker run -p port1 -p port2 -p port3 image  
    
    #    
    
    docker run -p host_port1:port1 -p host_port2:port2 -p host_port3:port3 image   
  8. ENV sets an environment variable in the image.

      ENV <key> <value> 
    
     ENV JAVA_HOME/usr/local/java8  
  9. ADD copies files from src to the container's dest path

     :
            ADD <src> <dest>  
    
    <src> /
    <dest>   
  10. VOLUME specifies the mount point.

     :
        VOLUME ["DIR"]  
    
    VOLUME ["/tmp/data"]   

    Run the container that generates the image through the Dockerfile, and the data in the/tmp/data directory will still exist after the container is closed.

  11. WORKDIR Switch directories. You can switch the working directory multiple times (equivalent to the cd command)

     :
        WORKDIR/path/to/workdir