Moment For Technology

Docker basis

Posted on Dec. 2, 2022, 3:37 a.m. by Devin Padilla
Category: The back-end Tag: Spring Cloud
  • concept

Docker advantages: continuous integration, version control, portability, isolation. The Docker container is configured to keep all internal configurations and dependencies unchanged. Ultimately, you can use the same container throughout the development process to ensure that there are no differences or human intervention. Docker ensures that your application is separated from resources. The Docker container can standardize your environment by ensuring consistency across different development and product release lifecycles. Docker can easily roll back to the previous version of this image.

Reference: www.dockone.io/article/389

  • Docker environment installation

Docker requires that the CentOS kernel version be later than 3.10 uname -r 2. Yum remove docker docker-common docker-selinux docker-engine 4. Yum remove docker docker-common docker-selinux Docker-engine 4. Docker ll /etc/yum. Repos.d / 5 docker ll /etc/yum. Install the package, yum-util provides yum- config-Manager, Yum install-y yum-utils device-mapper-persistent-data lvm2 Yum install warehouse - config - manager - add - repo 7 https://download.docker.com/linux/centos/docker-ce.repo. Check the docker version yum list docker - ce - showduplicates | sort - r 8. Install docker yum install docker-ce 9. Start docker systemctl start docker 10. Verify that dokcer run hello-world is installedCopy the code
  • Docker Common image commands

Docker search: exp - docker search nginx docker pull: To download an image,exp - docker pull nginx docker RMi: search for an image,exp - docker rmi nginx docker buildCopy the code
  • Docker commonly used container commands

Docker run: start the container docker stop: stop the container Docker restart: restart the container docker exec -it: Exp - docker exec -it 9A12345ea980 /bin/bash docker rm: delete docker cp: Used for containers and copy the data between the host machine, exp -  docker cp a12345ea980: / etc/nginx/nginx. Conf/dockerData/nginx/conf/nginx. ConfCopy the code
  • DockerFile

The Dockerfile file is a text file that contains all the commands needed to build the image

  • The operation sample

The subsequent deployment environment is Linux. Therefore, the demo environment is Linux centos 8 and is not on the local MAC. Docker starts an EXP ng container:

1. docker run -d -p 8080:80 --name nginx --rm nginx
2. docker ps
3. docker exec -it 9A12345ea980 /bin/bash
4. docker cp nginx:/etc/nginx/nginx.conf /dockerData/nginx/conf/nginx.conf
5. docker cp nginx:/etc/nginx/conf.d /dockerData/nginx/conf/conf.d
6. docker stop 9A12345ea980
7. docker run -d -p 8080:80 --name nginx \
		-v /dockerData/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
		-v /dockerData/nginx/conf/conf.d:/etc/nginx/conf.d \
		-v /dockerData/nginx/www:/usr/share/nginx/html \
		-v /dockerData/nginx/logs:/var/log/nginx nginx
8. mkdir -p /dockerData/nginx/www
9. cd /dockerData/nginx/www
10. vi index.html
        Nginx docker test !
11. curl http://localhost:8080
Copy the code

Write a Dockerfile and start the exp container:

1. 本地mac idea打好的包拷贝到linux
scp /Users/XXXXXX/Documents/work/project/satan-life/life-customer/target/life-customer-0.0.1-SNAPSHOT.jar [email protected]:/satan/apps
2. 编写Dockerfile
cd /satan/apps  vim Dockerfile
FROM williamyeh/java8:latest
VOLUME /tmp
EXPOSE 8080
COPY life-customer-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
3. 基于Dockerfile构建镜像
docker build -t life-customer:v0.0.1 .
4. 基于镜像启动容器
docker run -v /app/docker/logs:/logs -p 80:8071 --rm --name customer ife-customer:v0.0.1
5. 验证
curl -d '{}' -H 'Content-Type: application/json' -X POST http://localhost/customer/info/basic
6. 附日志
[[email protected] apps]# docker build -t life-customer:v0.0.1 .
Sending build context to Docker daemon   28.5MB
Step 1/5 : FROM williamyeh/java8:latest
 --- 00bc163fa009
Step 2/5 : VOLUME /tmp
 --- Running in a13fab7f0f6f
Removing intermediate container a13fab7f0f6f
 --- a5fb91cf6eb9
Step 3/5 : EXPOSE 8080
 --- Running in c1e255ce1faf
Removing intermediate container c1e255ce1faf
 --- 67235ff31797
Step 4/5 : COPY life-customer-0.0.1-SNAPSHOT.jar app.jar
 --- 59617cb57b87
Step 5/5 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
 --- Running in bcda4a2c1153
Removing intermediate container bcda4a2c1153
 --- f9f3a39fd119
Successfully built f9f3a39fd119
Successfully tagged life-customer:v0.0.1
[[email protected] apps]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
life-customer      v0.0.1    f9f3a39fd119   12 seconds ago   622MB
nginx              latest    4cdc5dd7eaad   3 days ago       133MB
hello-world        latest    d1165f221234   4 months ago     13.3kB
williamyeh/java8   latest    00bc163fa009   3 years ago      593MB
[[email protected] apps]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[[email protected] apps]# docker run -v /app/docker/logs:/logs -p 80:8071 --rm --name customer ife-customer:v0.0.1
Unable to find image 'ife-customer:v0.0.1' locally
docker: Error response from daemon: Head https://registry-1.docker.io/v2/library/ife-customer/manifests/v0.0.1: read tcp 172.19.80.210:57156-35.175.91.243:443: read: connection reset by peer.
See 'docker run --help'.
[[email protected] apps]# docker run -v /app/docker/logs:/logs -p 80:8071 --rm --name customer life-customer:v0.0.1

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.8)

2021-07-10 05:58:01,901 [main][] INFO  [com.satan.customer.CustomerApplication] [StartupInfoLogger.java:55] - Starting CustomerApplication v0.0.1-SNAPSHOT using Java 1.8.0_161 on 18671fc0499f with PID 1 (/app.jar started by root in /)
2021-07-10 05:58:01,911 [main][] INFO  [com.satan.customer.CustomerApplication] [SpringApplication.java:664] - No active profile set, falling back to default profiles: default
2021-07-10 05:58:03,070 [main][] INFO  [org.springframework.cloud.context.scope.GenericScope] [GenericScope.java:288] - BeanFactory id=0406820a-c994-37f7-8001-8ea7c867386d
2021-07-10 05:58:03,475 [main][] INFO  [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [TomcatWebServer.java:108] - Tomcat initialized with port(s): 8071 (http)
2021-07-10 05:58:03,498 [main][] INFO  [org.apache.coyote.http11.Http11NioProtocol] [DirectJDKLog.java:173] - Initializing ProtocolHandler ["http-nio-8071"]
2021-07-10 05:58:03,500 [main][] INFO  [org.apache.catalina.core.StandardService] [DirectJDKLog.java:173] - Starting service [Tomcat]
2021-07-10 05:58:03,500 [main][] INFO  [org.apache.catalina.core.StandardEngine] [DirectJDKLog.java:173] - Starting Servlet engine: [Apache Tomcat/9.0.48]
2021-07-10 05:58:03,573 [main][] INFO  [org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]] [DirectJDKLog.java:173] - Initializing Spring embedded WebApplicationContext
2021-07-10 05:58:03,573 [main][] INFO  [org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext] [ServletWebServerApplicationContext.java:289] - Root WebApplicationContext: initialization completed in 1527 ms
2021-07-10 05:58:04,522 [main][] INFO  [org.apache.coyote.http11.Http11NioProtocol] [DirectJDKLog.java:173] - Starting ProtocolHandler ["http-nio-8071"]
2021-07-10 05:58:04,551 [main][] INFO  [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [TomcatWebServer.java:220] - Tomcat started on port(s): 8071 (http) with context path ''
2021-07-10 05:58:04,586 [main][] INFO  [com.alibaba.cloud.nacos.registry.NacosServiceRegistry] [NacosServiceRegistry.java:75] - nacos registry, DEFAULT_GROUP life-customer 172.17.0.2:8071 register finished
2021-07-10 05:58:04,603 [main][] INFO  [com.satan.customer.CustomerApplication] [StartupInfoLogger.java:61] - Started CustomerApplication in 3.61 seconds (JVM running for 4.755)

[[email protected] ~]# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS                                             NAMES
18671fc0499f   life-customer:v0.0.1   "java -Djava.securit…"   6 minutes ago   Up 6 minutes   8080/tcp, 0.0.0.0:80-8071/tcp, :::80-8071/tcp   customer
[[email protected] ~]# curl -d '{}' -H 'Content-Type: application/json' -X POST http://localhost/customer/info/basic
{"code":"00","msg":"ok","data":{"name":"satan","customerId":null}}[[email protected] ~]# 
Copy the code
  • The resources

www.dockone.io/article/389

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.