1. golang:latest basic image
mkdir gotest touch main.go touch Dockerfile
Sample code:
package main import ( "fmt" "log" "net/http" ) func main() { http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { fmt.Fprint(writer, "Hello World") }) fmt.Println("3000!!") log.Fatal(http.ListenAndServe(":3000", nil)) }
Dockerfile configuration
#源镜像 FROM golang:latest #设置工作目录 WORKDIR $GOPATH/src/github.com/gotest #将服务器的go工程代码加入到docker容器中 ADD . $GOPATH/src/github.com/gotest #go构建可执行文件 RUN go build . #暴露端口 EXPOSE 3000 #最终运行docker的命令 ENTRYPOINT ["./gotest"]
Packaging Image
docker build -t gotest .
golang:latest The compilation process is actually building a go development environment in the container. This source image package is about 800M, which is relatively large.
2. alpine:latest basic image
The general process of using this image is to package the go program on the Linux machine first into a binary file, then throw it into the apine environment, and execute the compiled file.
By default, Go's runtime environment variable CGO_ENABLED=1, which means cgo is started by default, allowing you to call C code in Go code. CGO is disabled by setting CGO_ENABLED=0. So you need to execute: CGO_ENABLED=0 go build. That's it.
This basic image package is only 13M, which is very small.
#源镜像 FROM alpine:latest #设置工作目录 WORKDIR $GOPATH/src/github.com/common #将服务器的go工程代码加入到docker容器中 ADD . $GOPATH/src/github.com/common #暴露端口 EXPOSE 3002 #最终运行docker的命令 ENTRYPOINT ["./common"]
Packaging image:
docker build -t common .
Recommended tutorial: docker
The above is the detailed content of Docker deploys two basic images of go. For more information, please follow other related articles on the PHP Chinese website!

Using Docker on Linux can improve development efficiency and simplify application deployment. 1) Pull Ubuntu image: dockerpullubuntu. 2) Run Ubuntu container: dockerrun-itubuntu/bin/bash. 3) Create Dockerfile containing nginx: FROMubuntu;RUNapt-getupdate&&apt-getinstall-ynginx;EXPOSE80. 4) Build the image: dockerbuild-tmy-nginx. 5) Run container: dockerrun-d-p8080:80

Docker simplifies application deployment and management on Linux. 1) Docker is a containerized platform that packages applications and their dependencies into lightweight and portable containers. 2) On Linux, Docker uses cgroups and namespaces to implement container isolation and resource management. 3) Basic usages include pulling images and running containers. Advanced usages such as DockerCompose can define multi-container applications. 4) Debug commonly used dockerlogs and dockerexec commands. 5) Performance optimization can reduce the image size through multi-stage construction, and keeping the Dockerfile simple is the best practice.

Docker is a Linux container technology-based tool used to package, distribute and run applications to improve application portability and scalability. 1) Dockerbuild and dockerrun commands can be used to build and run Docker containers. 2) DockerCompose is used to define and run multi-container Docker applications to simplify microservice management. 3) Using multi-stage construction can optimize the image size and improve the application startup speed. 4) Viewing container logs is an effective way to debug container problems.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.