1. 准备工作
将jar包放在文件夹下,这里以app为例,jar包名称为ac-contact-0.0.1-SNAPSHOT.jar
2. 创建dockerfile
执行命令 vim dockerfile-contact
内容如下
FROM jdk11:2019-10-12
COPY ac-contact-0.0.1-SNAPSHOT.jar /ac-contact-0.0.1-SNAPSHOT.jar
cmd ["nohup","java", "-jar", "/ac-contact-0.0.1-SNAPSHOT.jar","&"]
3. 创建镜像
执行命令 docker build -t contact . -f dockerfile-contact
出现类似下面内容则表示执行成功
root@shenlq-virtual-machine:/home/shenlq/docker/app# docker build -t contact . -f Dockerfile-contact
Sending build context to Docker daemon 85.95MB
Step 1/3 : FROM jdk11:2019-10-12
---> e89f841ccda3
Step 2/3 : COPY ac-contact-0.0.1-SNAPSHOT.jar /ac-contact-0.0.1-SNAPSHOT.jar
---> becf6589c913
Step 3/3 : cmd ["nohup","java", "-jar", "/ac-contact-0.0.1-SNAPSHOT.jar","&"]
---> Running in fecabc77c461
Removing intermediate container fecabc77c461
---> a7bfc5218653
Successfully built a7bfc5218653
Successfully tagged contact:latest
4. 准备 docker-compose.yml 文件
执行命令vi docker-compose.yml
内容如下(最简单的方式,后续展开来讲)
version: '1.0'
services:
video:
image: contact
container_name: contact
ports:
- "5111:5111"
restart: always
保存以后,再把 run.sh 放在同级目录下,下面是run.sh内容,可直接复制
#!/bin/sh
#
# Run docker-compose in a container
#
# This script will attempt to mirror the host paths by using volumes for the
# following paths:
# * $(pwd)
# * $(dirname $COMPOSE_FILE) if it's set
# * $HOME if it's set
#
# You can add additional volumes (or any docker run options) using
# the $COMPOSE_OPTIONS environment variable.
#
set -e
VERSION="1.22.0"
IMAGE="docker/compose:$VERSION"
# Setup options for connecting to docker host
if [ -z "$DOCKER_HOST" ]; then
DOCKER_HOST="/var/run/docker.sock"
fi
if [ -S "$DOCKER_HOST" ]; then
DOCKER_ADDR="-v $DOCKER_HOST:$DOCKER_HOST -e DOCKER_HOST"
else
DOCKER_ADDR="-e DOCKER_HOST -e DOCKER_TLS_VERIFY -e DOCKER_CERT_PATH"
fi
# Setup volume mounts for compose config and context
if [ "$(pwd)" != '/' ]; then
VOLUMES="-v $(pwd):$(pwd)"
fi
if [ -n "$COMPOSE_FILE" ]; then
COMPOSE_OPTIONS="$COMPOSE_OPTIONS -e COMPOSE_FILE=$COMPOSE_FILE"
compose_dir=$(realpath $(dirname $COMPOSE_FILE))
fi
# TODO: also check --file argument
if [ -n "$compose_dir" ]; then
VOLUMES="$VOLUMES -v $compose_dir:$compose_dir"
fi
if [ -n "$HOME" ]; then
VOLUMES="$VOLUMES -v $HOME:$HOME -v $HOME:/root" # mount $HOME in /root to share docker.config
fi
# Only allocate tty if we detect one
if [ -t 0 ]; then
if [ -t 1 ]; then
DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -t"
fi
else
DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -i"
fi
exec docker run --rm $DOCKER_RUN_OPTIONS $DOCKER_ADDR $COMPOSE_OPTIONS $VOLUMES -w "$(pwd)" $IMAGE "$@"
5. 创建容器
-
授予可执行权限
chmod +x /home/docker/xxxxxx/run.sh
-
创建 jdk_video-network网络,并构建 jdk 镜像
./run.sh -f docker-compose.yml up -d
6. 验证是否成功
执行命令docker logs -f --details contact
如果一直在跳,说明已经成功了。