docker image creation example

This paper will rustdesk-server-demo Take docker as an example to describe the production process of docker image.

1 compile the executable binary file

After installing the trust language, compile the rustdesk server Demo:

cd ~/github/rustdesk-server-demo
mkdir -p target/releae
cargo build --release

The direct executable binary file path is. / target / release / rustdesk server

You can verify that the executable is correct:

IP=127.0.0.1 ./target/release/rustdesk-server

2. Write Dockerfile

server.Dockerfile:

FROM ubuntu:bionic
LABEL maintainer="jingpingyin@xxx.com"

WORKDIR /root
RUN apt update -y && \
    apt install -y --no-install-recommends \
    curl wget nasm yasm unzip zip sudo jq vim less

COPY ./target/release/rustdesk-server /root
COPY ./entrypoint.sh /root

RUN sudo chmod 777 entrypoint.sh
CMD bash /root/entrypoint.sh
  • FROM -- basic image. This example creates this image based on Unbutu image.
  • WORKDIR -- set the working directory in the image, in this case, / root.
  • RUN - execute commands in docker. Multiple RUN commands are combined into one to reduce the image size. This example successively uses this command to install some tools and modify script permissions.
  • COPY -- COPY the directory or file on the host to the docker image. In this example:
    • Copy the compiled executable binary. / target / release / rustdesk server to the / root directory in the docker image
    • Copy the script file. / entrypoint.sh to be executed after the docker is started to the / root directory in the docker image
  • CMD --- execute the container startup command.

The script entrypoint.sh to execute after the container is started:

#!/bin/bash -e

mkdir -p /root/logs
/root/rustdesk-server >> "/root/logs/server_$(date +%Y%m%d-%H%M).log" 2>&1

Execute the binary rustdsk server and redirect its log to the / root/logs directory in the container.

3. Create docker image

To create a docker image:

docker build -t Mirror warehouse/xremote-server:demo -f server.Dockerfile .
  • -t --- specify a name for the image: label. In this example, the image name is image warehouse / xrremote server, and the label is demo
  • -f --- specify Dockerfile, this example is server.Dockerfile

Upload the docker image to the specified warehouse:

docker push Mirror warehouse/xremote-server:demo

The above two commands do not specify the image warehouse, but will be uploaded to the official warehouse by default.

4 run docker container

docker run will automatically pull the image and start the container:

IMAGE_FULL_TAG=Mirror warehouse/xremote-server:demo
CONTAINER_NAME="rustdesk-server-demo"
HOST_SIGNALING_PROT=21116
HOST_RELAY_PROT=21117
CONTAINER_SIGNALING_PORT=21116
CONTAINER_RELAY_PORT21117
IP=127.0.0.1

docker run -itd --restart=always --net bridge \
          --name "${CONTAINER_NAME}" \
          -p "${HOST_SIGNALING_PROT}:${CONTAINER_SIGNALING_PORT}/udp" \
          -p "${HOST_SIGNALING_PROT}:${CONTAINER_SIGNALING_PORT}" \
          -p "${HOST_RELAY_PROT}:${CONTAINER_RELAY_PORT}" \
          -e IP="${IP}" \
          -v "${ROOT_DIR}/logs/${CONTAINER_NAME}/:/root/logs" \
          ${IMAGE_FULL_TAG}
  • -i. -- interactive, keep STDIN open
  • -t. -- TTY, assign a pseudo TTY
  • -d. -- detach, run in the background and print the container ID
  • --Restart, container restart policy. This example is infinite restart
  • --net, the network settings of the container. This example is a bridge mode. Port mapping can be performed only when the bridge mode is specified
  • --Name, the name of the container
  • -p. -- publish, port mapping, host port: container port. Note that for udp ports, / udp should be added
  • -e. -- Env, set the environment variable in the container. In this example, IP=127.0.0.1 is set
  • -v. -- volume, mount, Host Directory: container directory

Keywords: Docker

Added by titangf on Sun, 21 Nov 2021 07:54:53 +0200