Table of Contents
What are Vagrant and Docker?
Why use Vagrant Docker for local development and testing?
Installing Vagrant and Docker
Configuring the Docker environment in Vagrant
Start the Vagrant virtual machine
Use Docker to run the Beego application
Summary
Home Backend Development Golang Local development and testing with Vagrant and Docker in Beego

Local development and testing with Vagrant and Docker in Beego

Jun 22, 2023 pm 05:13 PM
docker vagrant beego

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
Copy after login

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
Copy after login

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
Copy after login

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}
Copy after login

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 .
Copy after login
  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
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

How to read the docker version How to read the docker version Apr 15, 2025 am 11:51 AM

To get the Docker version, you can perform the following steps: Run the Docker command "docker --version" to view the client and server versions. For Mac or Windows, you can also view version information through the Version tab of the Docker Desktop GUI or the About Docker Desktop menu.

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

How to change the docker image source in China How to change the docker image source in China Apr 15, 2025 am 11:30 AM

You can switch to the domestic mirror source. The steps are as follows: 1. Edit the configuration file /etc/docker/daemon.json and add the mirror source address; 2. After saving and exiting, restart the Docker service sudo systemctl restart docker to improve the image download speed and stability.

How to save docker image How to save docker image Apr 15, 2025 am 11:54 AM

To save the image in Docker, you can use the docker commit command to create a new image, containing the current state of the specified container, syntax: docker commit [Options] Container ID Image name. To save the image to the repository, you can use the docker push command, syntax: docker push image name [: tag]. To import saved images, you can use the docker pull command, syntax: docker pull image name [: tag].

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

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).

How to call docker lnmp How to call docker lnmp Apr 15, 2025 am 11:15 AM

Docker LNMP container call steps: Run the container: docker run -d --name lnmp-container -p 80:80 -p 443:443 lnmp-stack to get the container IP: docker inspect lnmp-container | grep IPAddress access website: http://<Container IP>/index.phpSSH access: docker exec -it lnmp-container bash access MySQL: mysql -u roo

See all articles