Dockerfile contains all commands required to create an image. Based on the commands in Dockerfile, we can use the Docker build command to create an image. Simplify deployment by reducing the process of creating images and containers
The basic syntax of Dockerfile is
Use # to annotate
The FROM Command tells Docker which image is used as the basis. Here I get it FROM my own private Image Library.
MAINTAINER: Set the creator of the image.
Commands starting with RUN will be RUN during creation. For example, if you install a software package, you can use yum to install some software and execute the commands in the shell or exec environment. The RUN command adds a new level to the newly created image, and the result submitted is used in the next command of Dockerfile.
ADD: Command for copying files. It has two parameters: <source> and <destination>. Destination is the path in the container. Source can be a URL or a file in the startup configuration context.
CMD: provides default command execution for containers. Dockerfile only allows one CMD command. Using multiple CMD will offset all previous commands. Only the last command takes effect.
EXPOSE: specifies the port that the container listens to at runtime.
ENTRYPOINT: configure an executable command for the container, which means that a specific application can be set as the default program each time an image is used to create a container. It also means that the image can only run the specified application each time it is called. Similar to CMD, Docker only allows one ENTRYPOINT. Multiple entrypoints offset all previous commands and only execute the last ENTRYPOINT command.
WORKDIR: specifies the working directory of RUN, CMD, and ENTRYPOINT commands.
ENV: set environment variables. They use key-value pairs to increase program flexibility.
USER: when the image is running, set a UID
VOLUME: authorized access to the directory from the container to the host
This is a Dockfile for deploying the beego environment. The container can use this image to automatically start a web application of the beego framework.
[Root @ docker data] # cat Dockerfile
# Install beego
# Version 1.0
FROM 127.0.0.1: 5000/centos: latest
MAINTAINER ivonlee "ldtrain@gmail.com"
Env gopath/data/golang
Env path $ GOPATH/bin: $ PATH
RUN \
Mkdir-p/data/golang &&\
Yum install golang vim golang-vim git supervisor-y &&\
Go get github.com/astaxie/beego &&\
Go get github.com/beego/bee
ADD./hello. go/data/golang/hello. go
RUN go build-o/data/golang/hello. go
RUN chmod 755/data/golang/hello
WORKDIR/data/golang
EXPOSE 22 8080
CMD ["/data/golang/hello"]
Beego framework test file hello. go, which is in the same directory as Dockerfile
[Root @ docker data] # cat hello. go
Package main
Import (
"Github.com/astaxie/beego"
)
Type MainController struct {
Beego. Controller
}
Func (this * MainController) Get (){
This. Ctx. WriteString ("hello world ")
}
Func main (){
Beego. Router ("/", & MainController {})
Beego. Run ()
}
Bulid Image. Here we can see whether the build process is similar to the RPM package.
[Root @ docker data] # docker build-t ivonlee/beego: 0.2.
Sending build context to Docker daemon 121.8 MB
Sending build context to Docker daemon
Step 0: FROM 127.0.0.1: 5000/centos: latest
---> 7322fbe74aa5
Step 1: MAINTAINER ivonlee "ldtrain@gmail.com"
---> Using cache
---> 5e0641b86a2e
Step 2: env gopath/data/golang
---> Using cache
---> A24c7200756f
Step 3: env path $ GOPATH/bin: $ PATH
---> Using cache
---> 5c3318517537
Step 4: RUN mkdir-p/data/golang & yum install golang vim golang-vim git supervisor-y & go get github.com/astaxie/beego & go get github.com/beego/bee
---> Using cache
---> E3d7183077bd
Step 5: ADD./hello. go/data/golang/hello. go
---> Using cache
---> 382.16c802079
Step 6: RUN go build-o/data/golang/hello. go
---> Using cache
---> Ff18e9b3001a
Step 7: RUN chmod 755/data/golang/hello
---> Using cache
---> 3d047ec8add6
Step 8: WORKDIR/data/golang
---> Running in 358fddd90750
---> D8c363846643
Removing intermediate container 358fddd90750
Step 9. EXPOSE 22 8080
---> Running in ac57f3f00c8a
---> 032dae53209b
Removing intermediate container ac57f3f00c8a
Step 10: CMD/data/golang/hello
---> Running in 85f17b350eba
---> Ef5ba42cf19c
Removing intermediate container 85f17b350eba
Successfully built ef5ba42cf19c
-T: create a tag for the created image to facilitate memory/indexing.
.: Specifies that the Dockerfile is in the current directory.
View the generated image. The bulid version above is 0.2.
[Root @ docker data] # docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Ivonlee/beego 0.2 ef5ba42cf19c 4 minutes ago 539.1 MB
Ivonlee/beego 0.1 6b4c8db1ee6d 10 minutes ago 539.1 MB
To delete an image, use docker rmi ef5ba42cf19c.
Run a container based on the generated image
[Root @ docker data] # docker run-d-P-I -- name beegotest2 ivonlee/beego: 0.2
Bytes
-Name indicates the container name for self-identification.
-P is automatically mapped to the port in the container. The host will map a port randomly.
View containers
[Root @ docker data] # docker ps-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
A69634223a4c ivonlee/beego: 0.2 "/data/golang/hello" 11 seconds ago Up 10 seconds 0.0.0.0: 32788-> 22/tcp, 0.0.0.0: 32789-> 8080/tcp beegotest2
6553663b32ed ivonlee/beego: 0.1 "/bin/bash" 5 minutes ago Up 5 minutes 0.0.0.0: 32786-> 22/tcp, 0.0.0.0: 32787-> 8080/tcp beegotest1
If you want to delete a container, you can use docker rm a69634223a4c. If there is only one a6 at the beginning, you can even use docker rm a6 to forcibly delete it. You can use-f
The container opens ports 22 and 8080. The host machine maps the local port 32788 to port 22 of the container and Port 32789 to port 8080 of the container. If you want to manually specify the ing port, you can use
Docker run-d-p 8080: 8080-I -- name beegotest2 ivonlee/beego: 0.2
Then our browser accesses the beego in the container.
92E7DD82-08F4-4A28-BB2E-DAEAD1C5C36B
To view the running logs in the container, use docker logs.
[Root @ docker data] # docker logs a69634223a4c
03:53:39 [config. go: 314] [W] open/data/golang/conf/app. conf: no such file or directory
03:53:39 [asm_amd64.s: 2232] [I] http server Running on: 8080
03:54:07 [router. go: 854] [D] | GET |/| 69.504 & micro; s | match |/|
03:54:11 [router. go: 854] [D] | GET |/| 56.677 & micro; s | match |/|
04:01:53 [router. go: 854] [D] | GET |/| 257.78 & micro; s | match |/|
06:36:26 [router. go: 854] [D] | GET |/| 472.511 & micro; s | match |/|
To stop or restart a container, use docker stop and docker restart.
[Root @ docker data] # docker stop a69634223a4c
A69634223a4c
[Root @ docker data] # docker ps-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
A69634223a4c ivonlee/beego: 0.2 "/data/golang/hello" 2 hours ago Exited (2) 2 seconds ago beegotest2
6553663b32ed ivonlee/beego: 0.1 "/bin/bash" 2 hours ago Up 2 hours 0.0.0.0: 32786-> 22/tcp, 0.0.0.0: 32787-> 8080/tcp beegotest1
We can see that the container a69634223a4c is already in the exit status.
If you want to save the container as an image, you can use docker commit
Docker commit a69634223a4c newbeego
If you want to further modify the container, you can use ssh, provided that you have set an ssh password in your image or add
RUN echo "root: 123456" | chpasswd
Or use docker exec to enter without a password
Docker exec-it a69634223a4c/bin/bash
The container id is in the middle. You can see it through docker ps-.
After the test is passed, we submit the image to our private Image Library.
[Root @ docker data] # docker label ef5ba42cf19c 127.0.0.1: 5000/beego
[Root @ docker data] # docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Ivonlee/beego 0.2 ef5ba42cf19c 29 minutes ago 539.1 MB
127.0.0.1: 5000/beego latest ef5ba42cf19c 29 minutes ago 539.1 MB
Ivonlee/beego 0.1 6b4c8db1ee6d 35 minutes ago 539.1 MB
Docker. io/registry latest ddfbba49d0b6 2 days ago 423.6 MB
Docker. io/centos latest 7322fbe74aa5 4 weeks ago 172.2 MB
127.0.0.1: 5000/centos latest 7322fbe74aa5 4 weeks ago 172.2 MB
[Root @ docker data] # docker push 127.0.0.1: 5000/beego
The push refers to a repository [127.0.0.1: 5000/beego] (len: 1)
Sending image list
Pushing repository 127.0.0.1: 5000/beego (1 tags)
Image f1b10cd84249 already pushed, skipping
Image 7322fbe74aa5 already pushed, skipping
Image c852f6d61e65 already pushed, skipping
5e0641b86a2e: Image successfully pushed
A24c7200756f: Image successfully pushed
5c3318517537: Image successfully pushed
E3d7183077bd: Image successfully pushed
382.16c802079: Image successfully pushed
Ff18e9b3001a: Image successfully pushed
3d047ec8add6: Image successfully pushed
D8c363846643: Image successfully pushed
032dae53209b: Image successfully pushed
Ef5ba42cf19c: Image successfully pushed
Pushing tag for rev [ef5ba42cf19c] on {http: // 127.0.0.1: 5000/v1/repositories/beego/tags/latest}
[Root @ docker data] # curl 127.0.0.1: 5000/v1/search
{"Num_results": 2, "query": "", "results": [{"description": "", "name": "library/centos "}, {"description": "", "name": "library/beego"}]}