Uninstall the local development environment and embrace containerized development

When I was at the company, I used the containerized environment prepared by my colleagues to develop directly in Docker. It was so cool. It was also at that time that I learned about containerized development. Unfortunately, that environment is no longer available. So I plan to set up a personal development environment locally, but because I have already set up a local development environment (I mainly use Go now and usually write some Python scripts, so I installed both of them locally), there is no Motivated to push myself to study. Because this idea has been put off for a long time, and if it continues to be put off, it will be impossible to do it again. Therefore, let’s be radical today, uninstall the local development environment directly, and force yourself to start a containerized development environment.

Uninstall the local development environment

However, I only uninstalled the Go development environment here, because Python is an interpreted language, and it is still necessary to have one locally.

Please add a picture description

Then open VSCode and you can find that the Go plug-in has reported an error, haha.

Please add an image description

Install Docker Desktop

Because we are using Windows now, not Linux, we first install Docker Desktop on Windows. However, I still prefer to develop on Linux. On Windows, there are no command line tools that can be used.


I only have one Docker here because I tried to enable Kubernetes on it, but it couldn’t be started. I don’t know what the problem is. Then I turned to Rancher Desktop, but the docker function of Docker Desktop is still good. If you use docker, use it. If it is k8s, use Rancher desktop.

Pull the image

It can be seen here that the size difference between the two images is quite large. In fact, you can tell by looking at the image tag of golang. It is based on the alpine Linux distribution. So what is alpine? Let me briefly mention it here. The name is the European Alps. You can see it by looking at the cover below. Its obvious feature is that it is small, only about 5 MB.
Therefore, almost all the contents of this golang image are from go itself. But Python is different. Because it is an interpreted language, a larger image is generally used as the base image.

Note: The golang image here was randomly selected by me from docker hub, it may be too rudimentary. If it is used as a development image, you can choose a richer image based on Ubuntu (I won’t say it is big here, because bigness is a disadvantage, and richness is an advantage).

Official usage of docker hub’s golang image

Some students may not have a good network connection. I have intercepted this content here. We can just follow the usage below.

PS: Why is bigness a disadvantage? Because the more things there are, the more potential loopholes there are; conversely, there are fewer loopholes and it is more secure. Here you can briefly understand CVE: What is a CVE

Hello World

docker run --rm -it python:3.12 python -c 'print("Hello World")'

This example is relatively simple, and it also illustrates the possibility of container-based development.

Let’s look at a more complicated example in golang. I created a main.go file under local hello.

Please add image description

Start the golang image, mount main.go in the local directory to /go/src/hello in the container, and start an interactive shell terminal.
Here is what you need to know about container mounting:


Then you may need to know more about UFS, but to make a long story short, mounting a directory is to mount a specific local directory to the container directory.

Go further

Some simple things were introduced earlier. The demonstration of directly starting a mirror is an excellent example, but it is still too simple for real development. If you need to develop within a container, it is better to build your own image. Moreover, the environment required for real development is still much more complicated. If you want to do your job well, you must first sharpen your tools. Since it is development, of course you need a handy tool. Here I use VSCode.

Install related plug-ins

It is highly recommended that you read the official documentation of VSCode: Developing inside a Container Developing within a container

Then pay attention to the second box selection above. It (Dev Containers extention) supports two basic operation models:

  • You can use a container as your full-time development environment
  • You can connect to a running container to inspect it.

Create a Dockerfile in the project (any project, even the hello just used for demonstration)

FROM golang:1.20.11-alpine3.18

WORKDIR /usr/src/app
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
COPY go.mod go.sum ./
RUN go mod download & amp; & amp; go mod verify

COPY . .

For specific content, you can see the VSCode official document recommended above, which has already been explained in detail. However, I used attaching to a running container before, but this time I used another method.

Then you will be asked to choose. I only chose to install one zsh (alpine), but the first startup is still a bit slow. If you feel bored, you can take a look at the log output, which can relieve the anxiety of waiting.
It’s great that zsh can be installed directly here!

Here I just started the project on the command line, because I did not install the relevant golang plug-in. It prompted me to install it, but I did not install it. You can also choose the plug-in you want to install.

VSCode is still very user-friendly. Port forwarding is automatically enabled and there is no need to set it manually (because you are now in the container and cannot be accessed directly from the outside).

I will give a simple demonstration here, because this golang image may not be suitable, so I still need to get my own customized development environment (that is, a Dockfile or a more complicated docker-compose.yaml).

PS

DevContainer