Dockerfile deployment jar package and docker-compose deployment jar package

1. Dockerfile deployment jar package

1. Create a new directory

mkdir pro1
cd pro1
touch Dockerfile

2. Edit Dockerfile

vim Dockerfile

As follows:

# 拉取一个jdk为1.8的docker镜像
FROM java:8
# 声明作者是haoke
MAINTAINER haoke
# 前一个 pro1.jar  为上传的jar包,后一个 dk-pro1.jar 是将服务器中jar包对应在容器中运行的jar包名称
ADD pro1.jar  dk-pro1.jar
# 容器暴露的端口号,需要与jar包在容器中运行使用端口号一致
EXPOSE 10002
# 容器启动之后执行的命令, java -jar  dk-pro1.jar
ENTRYPOINT ["java","-jar"," dk-pro1.jar"]

3. Build jar package image

docker build -f Dockerfile -t dk-pro1:v1.0 .

4. Run the jar container

docker run -d --name con_pro1_jar -p 10002:10002 dk-pro1:v1.0

5. Browser verification

http://ip:10002

Two, docker-compose deployment jar package

1. Create a new file

mkdir pro1
cd pro1
touch docker-compose.yml

2. Write docker-compose.yml

version: '3'

# 关于网络,如果用到mysql或者redis,并且希望在同一个网络,那么就可以直接使用同一个网络名
# docker network create pro1_bridge
networks:
  pro1_bridge:
    driver: bridge

services:
   dk-pro1:
	 #Compose的容器名称格式是:<项目名称><服务名称><序号>
	 #可以自定义项目名称、服务名称,但如果想完全控制容器的命名,可以使用标签指定:
     container_name: con_pro1_jar
	 #image是指定服务的镜像名称或镜像ID。如果镜像在本地不存在,Compose将会尝试拉取镜像。
     image: dk-pro1:v1.0
     restart: always
	 #ports用于映射端口的标签。使用HOST:CONTAINER格式或者只是指定容器的端口,宿主机会随机映射端口。
     ports:
       - 10002:10002
     networks:
       - pro1_bridge

3. Build and run

cd pro1
docker-compose up -d

4. View logs

docker-compose logs dk-pro1_con_pro1_jar_1

5. Other commands

docker-compose up -d nginx                     构建建启动nignx容器

docker-compose exec nginx bash            登录到nginx容器中

docker-compose down                              删除所有nginx容器,镜像

docker-compose ps                                   显示所有容器

docker-compose restart nginx                   重新启动nginx容器

docker-compose run --no-deps --rm php-fpm php -v  在php-fpm中不启动关联容器,并容器执行php -v 执行完成后删除容器

docker-compose build nginx                     构建镜像 。        

docker-compose build --no-cache nginx   不带缓存的构建。

docker-compose logs  nginx                     查看nginx的日志 

docker-compose logs -f nginx                   查看nginx的实时日志

 

docker-compose config  -q                        验证(docker-compose.yml)文件配置,当配置正确时,不输出任何内容,当文件配置错误,输出错误信息。 

docker-compose events --json nginx       以json的形式输出nginx的docker日志

docker-compose pause nginx                 暂停nignx容器

docker-compose unpause nginx             恢复ningx容器

docker-compose rm nginx                       删除容器(删除前必须关闭容器)

docker-compose stop nginx                    停止nignx容器

docker-compose start nginx                    启动nignx容器

3. The container communicates with the host mysql

Take common mysql as an example

  • First, two things need to be clarified:
  1. In the docker container, localhost does not refer to the host's localhost, so you cannot access the host's mysql through localhost:3306 in the container.
  2. When docker is running, the network is established on docker0, and you can see the docker0 information by checking the host IP:
    insert image description here
  3. The IP address of the host in the same LAN as the container is generally the first address of the IP address segment corresponding to docker0 (such as 172.17.0.1). Therefore, the container can access the mysql server of the host through 172.17.0.1:3306, modify mysql The connection code is the address of docker0.
    == Note: ==mysql allows 127.0.0.1 access by default, 172.17.0.1:3306 may not be accessible, you need to set the mysql access authority
# 登录mysql
mysql -u -p

 # @后写%为所有ip,
 mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '12345678' WITH GRANT OPTION;
 
 # @后写具体ip
 mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.17.0.1' IDENTIFIED BY '12345678' WITH GRANT OPTION;   
 
 # @前可以指定数据库dk
 mysql>GRANT ALL PRIVILEGES ON dk.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
 
 # grant后一定要执行刷新权限
 mysql>flush privileges;
 
#其中各字符的含义:
#*.* 对任意数据库任意表有效
#"root" "12345678" 是数据库用户名和密码
# '%' 允许访问数据库的IP地址,%意思是任意IP,也可以指定IP
# flush privileges 刷新权限信息

Fourth, the container communicates with the host redis

After querying the container docker0ip, modify the redis configuration.

1. Modify the redis.conf file

  • The content is as follows:
    (1) bind 127.0.0.1 -::1 is changed to bind 172.17.0.1 -::1
    (2) protected-mode is set to no
    (3) requirepass is set to password 123123

2. Stop the current process

ps -ef|grep redis
kill 9 进程id

3. Start redis according to the configuration file

./redis-server ../redis.conf

Guess you like

Origin blog.csdn.net/weixin_43676010/article/details/128136458