I've talked a lot about the principles before, and I've basically explained the general principles of Docker once. This time, we'll run an example to use the Docker container.
Let's start by writing a simple web server program, hello, in Go. Go, and then run it with Docker. The source code for the Go program is as follows:
package main import ( "io" "net/http" ) func hello(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "Hello world!") } func main() { http.HandleFunc("/", hello) http.ListenAndServe(":8000", nil) }
This program is very simple. It starts a simple web server program and listens on port 8000. When you use the browser to access IP:8000/this address, the program will return the browser to Hello world!
To run the above program by Docker, you need to make a Docker image. The convenient way to make a Docker image is to use a Dockerfile. Dockerfile should do two main things here:
- Compile the Go program above
- Encapsulate the compiled Go program in a Docker image
The two steps mentioned above can be implemented directly through a Dockerfile, which is the multi-stage function of the Dockerfile, as follows:
# Compile our program using an official GO image FROM golang:1.17 # Switch working directory to/compile WORKDIR /compile # Will hello.go program copied to the / compile directory in the mirror COPY hello.go /compile # Compile Build hello Executable RUN GOOS=linux go build hello.go # Using alpine as the base image FROM alpine:3.15.0 # Switch working directory to/app WORKDIR /app # alpine needs to install a dependency first to execute the Go program RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2 # Copy the hello executable from the first stage to the current directory COPY --from=0 /compile/hello ./ # Allows outside access to the container's port 8000. Writing does not expose the port by default, which acts as a comment EXPOSE 8000 # Start hello executable CMD ["./hello"]
In the local working directory, perform a Dockerfile build to generate a new image:
$ ls Dockerfile hello.go $ docker build -t hello .
-t is to give the mirror a readable name, note that there is one at the end. Represents the directory where the Dockerfile is located.
Once the docker build executes correctly, you can see that a new image has been created:
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello latest 56173fb23f12 15 minutes ago 11.7MB
The following uses the docker run command to run this image:
$ docker run -d -p 8000:8000 56173fb23f12
Our Go program listens on port 8000 of a container, where -p specifies that port 8000 of a container is mapped to port 8000 of a host machine so that it can be accessed directly using my local browser.
Access using a browser:
So our mirrors have been tested. Usually we make mirrors that we want to share with others. Usually Harbor is used to build a mirror warehouse for internal staff. Here we can transfer the mirror to the DockerHub public mirror warehouse so that it can be seen and downloaded by everyone on the Internet. If you don't have a DockerHub account number, go to the official website to create an account number and your own warehouse: DockerHub
Log in to DockerHub using the docker login command:
$ docker login Enter user name and password Login Succeeded
The name of my Docker repository is hackercracker/test. To rename the local image based on this repository name:
$ docker tag hello hackercracker/test:v1
Push the mirror onto DockerHub:
$ docker push hackercracker/test:v1 The push refers to repository [docker.io/hackercracker/test] 24ba23593bae: Pushed 639949c6c224: Pushed c282b654e2d7: Pushed 8d3ac3489996: Mounted from library/alpine v1: digest: sha256:0d45ef82dd579e51fe81b30f7e43071a745a485fcef51867e018970ec205a73d size: 1153
At this point, the mirror was successfully pushed onto DockerHub.