Docker

Docker

Install:

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

Uninstall:

dpkg -l | grep -i docker
sudo apt-get purge -y docker docker-ce docker-ce-cli containerd.io
sudo apt-get autoremove -y --purge docker docker-ce docker-ce-cli containerd.io
sudo apt-get purge -y docker-engine docker-buildx-plugin docker-ce-rootless-extras docker-compose-plugin
sudo apt-get autoremove -y --purge docker-engine docker-buildx-plugin docker-ce-rootless-extras docker-compose-plugin
sudo rm -rf /var/lib/docker /etc/docker
sudo rm -rf /etc/apparmor.d/docker
sudo groupdel docker
sudo rm -rf /var/run/docker.sock
sudo rm -rf ~/.docker
sudo rm -rf /var/lib/containerd
sudo rm -rf /usr/bin/docker /usr/libexec/docker /usr/share/man/man1/docker.1.gz

Run without sudo:

sudo groupadd docker
sudo gpasswd -a $USER docker
sudo service docker restart

if not working:

sudo chmod 666 /var/run/docker.sock

Install docker-compose:

sudo curl -L "https://github.com/docker/compose/releases/download/v2.17.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Uninstall docker-compose:

sudo rm /usr/local/bin/docker-compose
sudo apt remove docker-compose
sudo apt autoremove

Common commands:

docker ps -a
docker rm container_ID
docker rmi image_ID
docker system prune -a
docker volume prune
docker run --restart always ...

Exec image/container:

docker exec -it 1e33b26152e1 /bin/sh

docker exec -it 1e33b26152e1 bash

docker exec -it 1e33b26152e1 sh

Inside image/container use apk to manage packages if base image is Alpine.

docker build

docker build [OPTIONS] PATH | URL | -
cd /home/me/myapp/some/dir/really/deep
docker build -f /home/me/myapp/dockerfiles/debug /home/me/myapp
docker build -f ../../../../dockerfiles/debug /home/me/myapp

You can apply multiple tags to an image. For example, you can apply the latest tag to a newly built image and add another tag that references a specific version. For example, to tag an image both as whenry/fedora-jboss:latest and whenry/fedora-jboss:v2.1, use the following:

docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 .
docker build -t vieux/apache:2.0 .

The example below shows the equivalent when using the short-hand syntax. In this case, - is specified as destination, which automatically selects the tar type, and writes the output tarball to standard output, which is then redirected to the out.tar file:

 docker build -o - . > out.tar

docker export & docker import

使用 docker export 命令根据容器 ID 将镜像导出成一个文件。

docker export f299f501774c > hangger_server.tar

使用 docker import 命令则可将这个镜像文件导入为 new_hangger_server

docker import - new_hangger_server < hangger_server.tar

docker save & docker load

使用 docker save 命令根据 ID 将镜像保存成一个文件。

docker save 0fdf2b4c26d3 > hangge_server.tar

还可以同时将多个 image 打包成一个文件,比如下面将镜像库中的 postgres 和 mongo 打包:

docker save -o images.tar postgres:9.6 mongo:3.4

使用 docker load 命令则可将这个镜像文件载入进来。

docker load < hangge_server.tar

docker run

docker build -f Dockerfile . -t speedtest:latest
docker run --name speedtest speedtest:latest

Examples

docker-compose example

version: '3'
services:
  brickserver-postgres:
    container_name: brickserver-postgres
    image: "jbkoh/brickserver-postgresql:latest"
    environment: # Below secrets should be matched with the information in `configs.json` too.
      - PGDATA=/var/lib/postgresql/data/pgdata
      - POSTGRES_USER=bricker
      - POSTGRES_PASSWORD=brick-demo
      - POSTGRES_DB=brick
    ports:
      - "33773:5432"
    volumes:
      - /mnt/data/brick-server/docker_volume/postgresql:/var/lib/postgresql

  brickserver-virtuoso:
    container_name: brickserver-virtuoso
    image: "tenforce/virtuoso:virtuoso7.2.5"
    environment:
      - SPARQL_UPDATE=true
    ports:
      - "34773:1111"
      - "35773:8890"

  brickserver-mongo:
    container_name: brickserver-mongo
    image: "mongo:4.2.3-bionic"
    ports:
      - "32773:27017"
    volumes:
      - /mnt/data/brick-server/docker_volume/mongodb:/data
  brickserver:
    container_name: brickserver
    image: "jbkoh/brickserver:0.1"
    ports:
      - "8000:80"
    depends_on:
      - brickserver-virtuoso
      - brickserver-postgres
      - brickserver-mongo
    volumes:
      #- /etc/letsencrypt/:/etc/letsencrypt/ # A typical location of the host machine's certificate files.
      - /mnt/data/brick-server/brick-server/configs:/app/configs # You have to bind a directory having `configs.json` like this.
    environment:
      - CERTFILE=/PATH/TO/YOUR/CERT_FILE # This will be ignored if ENABLE_SSL=false
      - KEYFILE=/PATH/TO/YOUR/KEY_FILE # This will be ignored if ENABLE_SSL=false
      - ENABLE_SSL=false # Chante this to `false` if you do not want to use SSL inside the container directly.
      - LOG_LEVEL="debug"
    command: >
      /bin/bash -c "
        sleep 4;
        echo 'Waiting for others';
        sleep 4;
        echo 'Waiting for others';
        sleep 4;
        echo 'Waiting for others';
        /app/docker/start.sh
      "

Dockerfile Example

#Deriving the latest base image
FROM python:3.7.16-slim-bullseye


#Labels as key value pair
LABEL Maintainer="jimmyl1"
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

# Any working directory can be chosen as per choice like '/' or '/home' etc
# i have chosen /usr/app/src
WORKDIR /usr/app/src

#to COPY the remote file at working directory in container
COPY test_PythonScript.py ./
# Now the structure looks like this '/usr/app/src/test_PythonScript.py'


# CMD [ "python", "./test_PythonScript.py"]

The requirements.txt should include the following packages:

pandas==1.3.5
numpy==1.21.1
kafka-python==2.0.2
paho-mqtt==1.6.1

Errors

http: server gave HTTP response to HTTPS client

nano /etc/docker/daemon.json

{
    "insecure-registries": [
        "http://my-registry.nexus.internal:1234"
    ]
}

宝塔+Sakura

# docker部署bt
docker run -d --restart always --name baota -p 8888:8888 -p 8022:22 -p 8020:20 -p 8306:3306 -p 9443:443 -p 8090:80 -p 888:888 btpanel/baota:lnmp
# docker部署sakura
ip addr show docker0 |grep inet
# docker启动sakura
docker run \
    -d \
    --restart=on-failure:5 \
    --pull=always \
    --name=sakura1 \
    natfrp/frpc \
    -f <启动参数>
Previous
Next