Home  >  Article  >  Backend Development  >  Local development and testing with Vagrant and Docker in Beego

Local development and testing with Vagrant and Docker in Beego

WBOY
WBOYOriginal
2023-06-22 17:13:52673browse

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.

What are Vagrant and Docker?

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.

Why use Vagrant Docker for local development and testing?

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.

Installing Vagrant and Docker

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:

  1. Enter [Vagrant official website](https://www.vagrantup.com/downloads ) and download Vagrant.
  2. According to the operating system you are currently using, download and install Docker from [Docker official website](https://docs.docker.com/engine/install/). Note that it is necessary to set up a Docker image.

Configuring the Docker environment in Vagrant

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:

  1. Use ubuntu/focal64 as the operation of the virtual machine The system
  2. maps the 8080 port of the virtual machine to the 8080 port of the host
  3. The memory size allocated to the virtual machine is 2048MB
  4. The CPU allocated to the virtual machine is 2 cores .

Start the Vagrant virtual machine

Now, 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

Use Docker to run the Beego application

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:

  1. Use the git clone command in the Vagrant virtual machine to download your Beego application source code.
  2. Create a file named 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

  1. build container. In the root directory of the Beego application, run the following command:
docker build --build-arg MY_APP_PATH=github.com/your-username/your-app -t my-app-name:v1 .
  1. Run container. In the root directory of the Beego application, run the following command:
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.

Summary

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn