Local development and testing with Vagrant and Docker in 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:
- Enter [Vagrant official website](https://www.vagrantup.com/downloads ) and download Vagrant.
- 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:
- Use
ubuntu/focal64
as the operation of the virtual machine The system - maps the 8080 port of the virtual machine to the 8080 port of the host
- The memory size allocated to the virtual machine is 2048MB
- 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:
- Use the
git clone
command in the Vagrant virtual machine to download your Beego application source code. - 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
- 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 .
- 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!

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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.

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.

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.

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

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)

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

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
