As cloud computing and virtualization technology continue to mature, more and more developers are beginning to use virtualization technology for local development and testing. Virtualization technology helps isolate development environments and makes it more convenient and flexible to manage and develop multiple environments. This article will introduce you how to set up a Vagrant Docker development environment when using the Beego framework for local development and testing.
Vagrant is a virtualization technology that allows you to quickly build and manage a virtualized development environment. Vagrant can support different virtual engines (for example, VirtualBox, VMware, Hyper-V, etc.), so you can choose the engine that suits your current development environment to run your virtual development environment.
Docker is a containerization technology that helps you quickly create, deploy, and run applications and services. Docker containers can run in any environment and provide powerful environment isolation and version control mechanisms.
When we need to test our code in multiple environments, we need to develop and test as accurately as the production environment. In particular, the differences and configurations of each environment need to be consistent with the production environment. In this case, using Vagrant Docker can help developers complete complex testing and maintenance work in multiple environments without worrying about inconsistencies in environment parameter settings and errors caused by misoperation.
Before you start using Vagrant Docker, you need to install these two tools on your machine. Both tools have good documentation and community support, so during the installation process, you need to pay attention to the following steps:
Through the above steps, you have successfully installed Vagrant and Docker, and now you can use Vagrant to run a new virtual machine that will There is a Docker operating environment. Before using Vagrant to run a virtual machine, we need to configure the Vagrantfile:
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/focal64" config.vm.network "forwarded_port", guest: 8080, host: 8080 config.vm.provider "virtualbox" do |vb| vb.memory = "2048" vb.cpumax = "2" vb.cpus = "1" end config.vm.provision "docker" end
The above configuration file means:
ubuntu/focal64
as the operation of the virtual machine The systemNow, execute the following command to start the Vagrant virtual machine:
vagrant up
After executing this command, Vagrant will automatically download and install the Ubuntu virtual machine machine, this process may take some time. Once the installation is complete, you will be able to log in to the Vagrant virtual machine via SSH using the following command:
vagrant ssh
With the above operations, you have successfully created a Docker runtime environment virtual machine. Next, you can use Docker to run your Beego application, you need to follow the following steps:
git clone
command in the Vagrant virtual machine to download your Beego application source code. Dockerfile
in the root directory of your Beego application. The Dockerfile will contain some instructions to build a Docker container. The following is an example: FROM golang:1.16 ENV GOPATH /go ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH RUN apt-get update && apt-get -y install build-essential git-all RUN mkdir -p $GOPATH/src/${MY_APP_PATH}/ ADD . $GOPATH/src/${MY_APP_PATH}/ WORKDIR $GOPATH/src/${MY_APP_PATH}/ RUN go install ${MY_APP_PATH} ENTRYPOINT ${GOPATH}/bin/${MY_APP}
In this Dockerfile, we use Golang-1.16 as the base image, and download and install some necessary dependencies. We then copy all the source code into the image and build and install your Beego application using golang’s go install
command. When the container starts, the Beego application serves as the entry point to start the
docker build --build-arg MY_APP_PATH=github.com/your-username/your-app -t my-app-name:v1 .
docker run --rm -it -v $(pwd):/go/src/github.com/your-username/your-app -p 8080:8080 my-app-name:v1
In this command, we bind the current directory to /go/src/github.com/ in the container your-username/your-app
directory, so that code changes will be updated synchronously within the container. At the same time, map the container's 8080 port to the host's 8080 port so that we can access the Beego application in the container from the browser.
The above steps can help you understand how to use Vagrant Docker to configure a debugging environment to facilitate the same development and testing as in the production environment. The right local development environment can speed up your development and process, break limitations, and innovate more fantastic ideas.
The above is the detailed content of Local development and testing with Vagrant and Docker in Beego. For more information, please follow other related articles on the PHP Chinese website!