Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build Docker environment

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to build the Docker environment". In the daily operation, I believe that many people have doubts about how to build the Docker 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 build the Docker environment". Next, please follow the editor to study!

I. Environmental preparation

My personal test environment is virtualbox,centos7 and the latest version of spring boot. First install centos in a minimized manner:

It is recommended to open the network link by default during the installation phase. Then wait 20 minutes for the virtual machine to be installed.

Since the installation is minimized, it is recommended to install several basic commands after completion:

1. Yum update

2. Yum install-y net-tools vim curl wget

Even if the above test environment is complete, let's start installing docker. We went to the official website and they have kindly prepared the installation tutorial for us. At present, I can go to the official website of docker, and the centos-based installation instructions can also be seen here. But just in case, I'll write down in my blog that if you find you can't access or have some difficulty in reading English, just follow the following process:

1. Uninstall an earlier version of the system

$sudo yum remove docker\ docker-client\ docker-client-latest\ docker-common\ docker-latest\ docker-latest-logrotate\ docker-logrotate\ docker-selinux\ docker-engine-selinux\ docker-engine

two。 Installation tool

$sudo yum install-y yum-utils\ device-mapper-persistent-data\ lvm2

3. Configure the original installation yum of docker

$sudo yum-config-manager\-add-repo\ https://download.docker.com/linux/centos/docker-ce.repo

4. Install docker ce

$sudo yum install docker-ce

5. Start docker

$sudo systemctl start docker

6. Run hello world

$sudo docker run hello-world

7. Configure domestic image acceleration

Curl-ssl | sh-s

II. Operation Guide

There are many orders about docker on the Internet, for detailed instructions, you can go to a more authoritative place to consult. Here I would like to list and explain only a few of the most important commands:

1. Check the list of images: docker images

You shouldn't have so many images, but there should be at least one hello-world. Yes, this is the test image you just downloaded.

two。 Check the list of containers: docker ps-a

You should only see one hello-world container.

Here we have touched on the first important concept of docker: mirroring and containers. But for the time being, I'm not going to explain these two concepts, we can run them over and over again.

$sudo docker run hello-world

Then look at the list of containers:

Yes, you now find that running the same image multiple times will only cause one image download, but there will be multiple containers. More precisely, several containers will be generated after you run hello-world several times.

3. Stop and delete containers: docker stop/rm [container_id]

We deleted all the hello-world containers and instantly the whole world became a better place.

4. Delete image: docker rmi [image_id]

We delete the hello-world image, and the world is finally silent again.

With perceptual knowledge, you can now explain the concepts of mirrors and containers:

The image is like an iso file, and docker needs to use the image as the source. An image can be used repeatedly, and each time the docker is started by the image is a container that is isolated from each other. If the container is deleted, all changes in the container will be lost.

Use the most classic picture to show the relationship between the mirror and the container:

Yes, a complete docker project should be like this, there may be multiple image on the local system (boosfs), there is an inheritance relationship between them, and the top layer in the running environment is the container (container). All modifications are done only in the container. Data will be lost if the container is closed, unless you burn the container to a new mirror layer. But there are other better ways to save the data, but not in our discussion today.

5. Get the image: docker pull [image_name]

*

Please make sure that you have configured domestic image acceleration:

Vim / etc/docker/daemon.json

Configuration:

{"registry-mirrors": ["http://57326c54.m.daocloud.io"]}"

Restart docker:systermctl restart docker

*

First query the images related to centos.

Docker search centos

The first one looks "official".

Docker pull centos:latest

After the download is completed, check to make sure there is no problem with the image, docker images.

6. Start the image interactively: docker run-it [image_name] [args...]

Docker run-it centos / bin/bash

Huh? There seems to be no change. Take a closer look to see if the command prompt has changed.

Yes, you have entered the container, and now you are in the container's centos interface.

7. Install mysql

$sudo yum install-y wget$ sudo wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm$ sudo rpm-ivh mysql-community-release-el7-5.noarch.rpm$ sudo yum install mysql-community-server

Configuration

$sudo mysql_install_db-user=mysql-ldata=/var/lib/mysql

Start

$sudo mysqld_safe

8. Leave the container: ctrl + Q, ctrl + p

This key combination is used to exit the container and keep the container running in the background

9. Re-login container: docker exec-it [container_id] [args...]

Docker exec-it 21f99e364fb1 bash

Give remote connection rights to root users of mysql

# mysql-u rootmysql > update mysql.user set password = password ('12345678') where user = 'root';mysql > grant all privileges on *. * to' root'@'%' identified by '12345678' with grant option;mysql > flush privileges

10. Submit image: docker commit [container_id] [myimage_name:tag]

To submit your own image, you first need to exit the current container:

Exitdocker commit 21f99e364fb1 centos:mysql

11. Port mapping: docker run-p [local_port]: [docker_port]-it [image_name] [args...]

Docker run-p 3307 bin/bash 3306-it centos:mysql / bin/bash

In this way, we can access the mysql service we deployed in docker by accessing port 3307 of the virtual machine in the local operating system.

3. Build the docker image of the springboot project

So far, if all goes well with your operation, you have mastered the basic usage of docker. In fact, the above installation of mysql is just to get you familiar with the basic commands of docker. In fact, we rarely deploy mysql in docker in development. A more common development scenario is to generate a docker image of a springboot project, so let's go to the actual combat session.

1. Build a springboot project locally

Generate a basic project for us through springboot's official website:

Then import and open the project through your favorite ide, and add dependencies to the pom.xml file:

Org.springframework.boot spring-boot-starter-web com.spotify docker-maven-plugin 1.0.0 ${docker.image.prefix} / ${project.artifactid} src/main/docker / ${project.build.directory} ${project.build.finalname} .jar

Write a controller file:

@ restcontrollerpublic class dockercontroller {@ requestmapping ("/") public string hello () {return "hello docker!";}}

Now run it locally to see if there is any problem. If all goes well, move on to the next step.

two。 Next, we need to put the project into the docker environment to package.

(1) install jdk in the virtual machine

Yum-y install java-1.8.0-openjdk*

(2) configure environment variables as follows

Vim / etc/profileexport java_home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64 export path=$path:$java_home/bin

(3) download the latest version of maven from apache's official website

Tar vxf apache-maven-3.5.4-bin.tar.gzmv apache-maven-3.5.4 / usr/local/maven3

(4) configure environment variables as follows

Export maven_home=/usr/local/maven3export path=path:maven_home/bin

(5) make the environmental variable effective.

Source / etc/profile

(6) write dockerfile files

Add a docker directory under the main directory and create a dockerfile file with the following contents

From openjdk:8-jdk-alpinevolume / tmpadd helloboot-0.0.1-snapshot.jar app.jarentrypoint ["java", "- djava.security.egd=file:/dev/./urandom", "- jar", "/ app.jar"]

(7) then use maven to package under the project path

Mvn package docker:build

If the reason for the build failure is that your project name is different from mine, please check the dockerfile file carefully. You may encounter all kinds of error messages during the first I build. Just put it on the Internet and check it.

After the construction is completed, we can query the image list through docker images:

Yes, the first mirror image is our project. Let's start the image and map it through port 80 of the server:

Docker run-p 80R 8080-t springboot/helloboot

Finally, we directly access the ip of the virtual machine through the browser locally, if we can see "hello docker!"

It indicates that using docker to deploy the spring boot project is successful!

At this point, the study on "how to build a Docker environment" is over. I hope to be able to 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report