In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Docker to build a development environment". In daily operation, I believe many people have doubts about how to use Docker to build a development environment. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Docker to build a development environment". Next, please follow the editor to study!
We all encounter such a problem in the development: after the local development of the function, deploy to the server, or other people pull to the local development, the function will not be used.
Most of the time, these exceptions are due to differences in dependencies caused by different systems. Therefore, in order to solve this problem, there is a need to build a unified development environment based on Docker.
1. Benefits of using Docker
Easy to deploy
It often takes a long time for us to set up the environment. For teamwork, every time a new person comes in, you need to waste this avoidable time. And when building the environment, there are often a variety of problems, resulting in abnormal operation of the project code. If you use Docker, you only need to write the development container at the beginning, and others only need to pull down to complete the construction of the project environment, which can effectively avoid meaningless waste of time.
Isolation
We often deploy multiple project environments on one computer, which may interfere with each other if we install them directly. For example, a project requires Node.js 14 and some require Node.js 12. If we deploy directly on this computer, we cannot always co-exist, but if we use Docker, we can avoid this problem. Docker also ensures that each application uses only the resources allocated to it (including CPU, memory, and disk space). A particular software will not use all of your available resources, otherwise it will degrade performance or even cause other applications to stop working completely.
two。 Install Docker1) Linux install Docker
Take Arch Linux as an example, other distributions are more or less the same, except for their package management tools.
# set up a domestic mirror station, which is used for speed up in China. Optional operation $sudo pacman-mirrors-I-c China-m rank# use Pacman to install Docker$ sudo pacman- S docker# to establish a docker user group. By default, the docker command uses Unix socket to communicate with the Docker engine. Only root users and users of the docker group can access the Unix socket of the Docker engine. For security reasons, root users are not directly used on Linux systems. Therefore, it is better to add users who need to use docker to the docker user group. $sudo groupadd docker# joins the current user in the docker group, exits the current terminal and logs in again. $sudo usermod-aG docker $USER# test whether the installation is successful or not. $docker run-- rm hello-world2) Windows 10
It is relatively easy to install docker under Windows 10. There are several ways to install it:
Download and install manually:
Download Docker Desktop for Windows. As the official website is slow to download, if you need to download locally, you can click this link.
After downloading, double-click Docker Desktop Installer.exe to start the installation.
Install using winget:
$winget install Docker.DockerDesktop
Run Docker:
Type Docker in the Windows search bar and click Docker Desktop to start running.
After Docker starts, the whale icon appears in the Windows taskbar.
Wait a moment, when the whale icon is still, it means that Docker started successfully, and then you can open PowerShell/CMD/Windows Terminal and use Docker.
3) macOS
Install using Homebrew:
Homebrew's Cask already supports Docker Desktop for Mac, so it's easy to install using Homebrew Cask:
$brew install-cask docker
Download and install manually:
If you need to download manually, click download Docker Desktop for Mac. As the official website is slow to download, if you need to download locally, you can click this link.
Please note that downloading the corresponding chip type of software, M1 and Intel chips corresponding to the corresponding version is not universal.
Like other macOS software, installation is easy, double-click the downloaded .dmg file, and then drag the whale icon named Moby to the Application folder (you need to enter a user password).
Run Docker:
Find the Docker icon in the application and click run.
After running, you will see an extra whale icon in the menu bar in the upper right corner, which indicates the running status of Docker.
After the installation is completed and started, we can check the installed version of Docker at the terminal through a command.
$docker-version3. Docker source switching
The default source of docker is foreign, and the speed of domestic access is relatively slow, so it can be replaced with domestic source to improve the image pull speed.
1) Linux source switching
It is relatively simple under Linux. Just create a deamon.json file and write down the configuration:
$vi / etc/docker/deamon.json# input mirror source {# it is possible to change only one source, using a string instead of an array. "registry-mirrors": ["https://registry.docker-cn.com"," http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn"],} #: restart docker$ systemctl restart docker2 after wq saves and exits) Windows and Mac switch sources
Both Windows and Mac use Docker Desktop, so you can configure it directly in GUI.
Open the Docker interface and click Docker Engine:
In the output box on the right, enter the mirror source:
{"registry-mirrors": ["https://registry.docker-cn.com"," http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn"],} 4. Write Dockerfile
After installing Docker, we can then write our own project development environment. This article takes the front-end development environment as an example to build Dockerfile.
Contains the environment:
Node.js 14.17
Npm 6.14
Yarn 1.22
# in front-end development, you often need to use the shell command, and it is important to have a more complete environment, so you choose to use ubuntu as the basis, if you care about the container size You can choose the applicable basic image FROM ubuntuLABEL org.opencontainers.image.authors= "codebaokur@codebaoku.com" # set the environment variable ENV DEBIAN_FRONTEND noninteractive# to set the time zone ARG TZ=Asia/ShanghaiENV TZ ${TZ} RUN ln-snf / usr/share/zoneinfo/$TZ / etc/localtime & & echo $TZ > / etc/timezone# replace Ali Cloud Source with root user operation USER root# RUN sed-I "s/security.ubuntu.com/mirrors.aliyun.com/" / etc/apt/sources.list & &\ sed-I "s/archive.ubuntu.com/mirrors.aliyun.com/" / etc/apt/sources.list & &\ sed-I "s/security-cdn.ubuntu.com/mirrors.aliyun.com/" / etc/apt/sources.listRUN apt-get clean# update sources can be accelerated in China. Install the appropriate tool RUN apt-get update & & apt-get install-y\ zsh\ vim\ wget\ curl\ python\ git-core# install zsh and later enter the container It is more convenient to use shellRUN git clone https://github.com/robbyrussell/oh-my-zsh.git ~ / .oh-my-zsh & &\ cp ~ / .oh-my-zsh/templates/zshrc.zsh-template ~ / .zshrc & &\ git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom} / plugins/zsh-autosuggestions & &\ git clone https:/ / github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom} / plugins/zsh-syntax-highlighting & &\ sed-I's / ^ plugins = (/ plugins= (zsh-autosuggestions zsh-syntax-highlighting z /'~ / .zshrc & &\ chsh-s / bin/zsh# create me user RUN useradd-- create-home-- no-log-init-- shell / bin/zsh-G sudo me & &\ adduser me sudo & &\ echo 'me:password' | chpasswd# installs omzUSER meRUN git clone https://github.com/robbyrussell/oh-my-zsh.git ~ / .oh-my-zsh & &\ cp ~ / .oh-my-zsh/templates/zshrc.zsh-template ~ / .zshrc & &\ git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my for me -zsh/custom} / plugins/zsh-autosuggestions & &\ git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom} / plugins/zsh-syntax-highlighting & &\ sed-I's / ^ plugins = (/ plugins= (zsh-autosuggestions zsh-syntax-highlighting z /'~ / .zshrc # install nvm and nodeENV NVM_DIR=/home/me/.nvm\ NODE_VERSION=v14RUN mkdir -p $NVM_DIR & &\ curl-o-https://gitee.com/mirrors/nvm/raw/master/install.sh | bash &\. $NVM_DIR/nvm.sh & &\ nvm install ${NODE_VERSION} & &\ nvm use ${NODE_VERSION} & &\ nvm alias ${NODE_VERSION} & &\ ln-s `npm bin-- global` / home/me/.node-bin & &\ npm install-- global nrm & &\ nrm use taobao & &\ echo'> > ~ / .zshrc & &\ echo 'export NVM _ DIR= "$HOME/.nvm" > ~ / .zshrc & &\ echo'[- s "$NVM_DIR/nvm.sh"] & &. "$NVM_DIR/nvm.sh" # This loads nvm' > > ~ / .zshrc # install yarnRUN curl-o-L https://yarnpkg.com/install.sh | bash \ echo'> ~ &\ echo 'export PATH= "$HOME/.yarn/bin:$PATH" > > ~ / .zshrc # Add NVM binaries to root's .bashrcUSER rootRUN echo' > > ~ / .zshrc & &\ echo 'export NVM_DIR= "/ home/me/.nvm" > ~ / .zshrc & &\ echo' [- s "$NVM_DIR/nvm.sh"] & &. "$NVM_DIR/nvm.sh" # This loads nvm' > > ~ / .zshrc & &\ echo'> > ~ / .zshrc & &\ echo 'export YARN_DIR= "/ home/me/.yarn" > ~ / .zshrc & &\ echo' export PATH= "$YARN_DIR/bin:$PATH" > > ~ / .zshrc # Add PATH for node & YARNENV PATH $PATH:/home/me/.node-bin:/home/me/.yarn/bin# Delete apt/lists You can reduce the final image size RUN rm-rf / var/lib/apt/lists/*WORKDIR / var/www. After writing the Dockerfile, you can build it: docker build-t frontend/react:v1. After building, you can run it directly: # run as me, and the recommended way is docker run-- user=me-it frontend/react:v1 / bin/zsh# to run docker run-it frontend/react:v1 / bin/zsh5 as root. Write docker-compose.yml
In development, we usually need multiple containers to use together, for example, when we need to work with mysql or other containers, using docker-compose.yml can better organize them.
Version: '2'services: react: build: context:. Dockerfile: react/Dockerfile tty: true ports:-30000 volumes: -. / react/www:/var/www networks:-frontend mysql: image: mysql:5.7 ports:-33060 volumes: -. / mysql/data:/var/lib/mysql -. / mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d environment :-MYSQL_ROOT_PASSWORD=password networks:-frontend# put the container in the same networks and you can access networks: frontend: driver: bridge6. Start the container
After writing the above Dockerfile and docker-compose.yml, you can happily start development!
# enter the directory where the docker-compose.yml is located: $cd frontend# backend to launch all containers in the docker-compose.yml. If the container is not built, it will first build $docker-compose up-d # into the react container for command line interaction $docker-compose exec-- user=me react / bin/zsh
To test whether containers can access each other, you can write the following files, and the database needs to be created by yourself:
/ / index.jsconst mysql = require ('mysql') const connection = mysql.createConnection ({host:' mysql', user: 'root', password:' password', database: 'test',}) connection.connect (); connection.query (`SELECT * FROM users`, function (error, results, fields) {if (error) throw error; console.log (results)}) connection.end ()
Then run it, and you can see the results:
Node index.js [RowDataPacket {id: 1, name: 'Caster'}] at this point, the study on "how to use Docker to build a development environment" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.