Docker practice for developers


Show me the code and talk to me
This article source code, please click on learnsprinboot
I’m bulbl, your support is my motivation to share!

1、 Introduction

As anyone with development experience knows, almost every technology has a mascot representative. thatDockerWhat’s the mascot like? We can see it in the picture belowDockerIt’s a little whale that carries a lot of cargo on it.

Docker practice for developers

2、 Quick start

2.1 why should I use itDocker

So,DockerWhat on earth did it come from?DockerfromGoLanguage writing, is a free open source platform, can run in a number of mainstream platforms.DockerIt can be used to develop applications, deliver applications and run applications. In short,DockerIt’s like aLinuxInsideLinux。 So why do we need to use itDockerBecauseLinuxIn the use of high cost, configuration trouble. We usually useLinuxServers need to be purchased from Alibaba and Tencent, right. However, if we can, we prefer to go whoring for nothing.

So,DockerThe emergence of the pain point can be solved. As long as you have an idea, you canLinuxInstalled in the serverDockerAnd then in theDockerMultiple Linux servers are installed in it.

Docker practice for developers

becauseDockerhaveIsolationWe don’t have to worry about more than oneLinuxThey affect each other.

Another advantage is that when we install the components, theLinuxIt’s a lot of trouble up thereDockerComponents can be configured faster. We can also writeDockerfileScript to realize the synchronous configuration of production environment and development environment.

2.2 Dockerprinciple

We are hereLinuxInside installationDockerIn fact, we used to beWindowsInside installationvirtual machineThe same.DockerUsedLinuxThe physical characteristics of the server, likevirusThe same, parasitic in the server.

2.3 explanation of some names

DockerThere are some nouns:Image, container, dockerfile, docker hub

Docker HubIt’s like what we usegithub。 There are all kinds of themimage。 There are official and unofficial. We choose the official when pulling.

Docker practice for developers

Before we pull the mirror backDockerAfter that, we can run, just like the express home, we need to tear it by hand. And then the mirror image becomescontainer

In addition, we can also pass theDockerfileTurn local applications into mirrors.

as forDocker ComposeWhen we run the image, we need to write a push command, andDocker ComposeWe can useymlText form fast running mirror, to write once, run everywhere.

Docker practice for developers

3、 InstallationDocker

We can install and run on various platformsDocker。 What I’m demonstrating here isLinuxPlatform.

#1. Update the yum package to the latest
yum update

#2. Install the required software package. Yum utils provides the yum config manager function, and the other two are dependent on the devicemapper driver
yum install -y yum-utils device-mapper-persistent-data 1vm2

#3. Set the source of Yum
yum-config-manager -- add-repo

#4. Install docker and press y in the input interface
yum install -y docker-ce

#5. Check the docker version to verify whether the validation is successful
docker -V

Display after successful installation

Docker practice for developers

Of course, for well-known reasons, ourDockerYou need to accelerate, or you’ll have to put up with snail speed. So I recommendAlicloud’sDockerContainer image accelerationto configure.

4、 Common commands

dockerAs a classLinuxPlatform, of course, also has command line operation. Of course, I don’t like command line operation, so I recommend using interface operationportainerComponent.
Docker practice for developers

1. Start and stop

service docker start

service docker stop

2. View version

docker -v

We’d better install it here1.3Above version

3. Search image

docker search jdk

Docker practice for developers

4. Pull the mirror image

Docker pull image name

5. View image

docker images

Docker practice for developers

6. Running image

docker run -p 8083:8083 springio/gs-spring-boot-docker

The front port is the server port and the back port isDockerExposed port. The purpose of mapping port here is that we can access it remotelyDockerThe container inside. The ports are not necessarily the same.

Optional parameters

-D ා run container in background and print container ID: background run and print container ID
-Mount the container directory to the host (server) to modify the host file and synchronize it to the container file

7. Delete image

Docker RMI image ID

8. View the run container

docker ps 

Docker practice for developers

9. Check all containers, including, not running

docker ps  -a

10. Delete container

Docker RM container ID

11. View container operation log

Docker logs container ID

12. Enter the container, command line interface

Docker exec - it container ID / bin / Bash

In the process of starting the container, we may encounter the case that the container fails to start.

Instance installationNginx

1. SearchNginx

Docker practice for developers

2. Pull awayNginximage

Docker practice for developers

3. OperationNginximage

docker run --name my_nginx
-p 8081:80 
-v /srv/web/logs:/var/log/nginx  
-v /srv/web/conf/nginx.conf:/etc/nginx/nginx.conf
-v /srv/web/conf/default.conf:/etc/nginx/conf.d/default.conf
-v /srv/web/html:/usr/share/nginx/html


–name my_ Nginx gives the container a name

-D background operation

-P 8081:80 maps the container 80 port to the host (server) 8081 port

-v /srv/web/logs:/var/log/nginx
Mount directoryThe host directory is preceded by ‘:’, followed by the container file directory Original image name

4. ViewNginxcontainer

docker ps

Docker practice for developers

Instance deploymentSpringBoot

1. Write code

package com.example.demo2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Service;

public class Demo2Application {

    public static void main(String[] args) {
        E e = (E), args).getBean("ss");

    @Service(value = "ss")
    class E {
        public void test() {

2. Packingjar

mvn clean package -Dmaven.test.skip=true  

staytargetDirectory foundjar, duplicate namelsb-helloworld

3. CompilationDockerfilefile

COPY lsb-helloworld.jar  app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Then thejarandDockerfileFile upload toLinuxThe same directory of the server

4. OperationDockerfilefile

docker build -t springio/gs-spring-boot-docker .

The build is namedspringio/gs-spring-boot-dockerMirror image of

5. Running image

docker run -p 8083:8083 springio/gs-spring-boot-docker

Docker practice for developers

official account

Docker practice for developers