essay | tech | year-summary | about

返回上级菜单

Docker tips


日期:2024-05-16T00:00:00Z

basic

docker ps
docker ps -a

docker image ls

docker pull xxxx

docker create --name al alpine
docker run --name al alpine ## run is the same as create

docker start al
docker stop al

run docker in a shell

docker run -it python sh
docker run -it python bash
docker run --name py3 -it python bash

docker run -it --rm alpine /bin/ash

create a virtual env for you dev/compile/run

https://hub.docker.com/_/rust

FROM rust:1.67

WORKDIR /usr/src/myapp
COPY . .

RUN cargo install --path .

CMD ["myapp"]

other example

FROM rust:1.67 as builder
WORKDIR /usr/src/myapp
COPY . .
RUN cargo install --path .

FROM debian:bullseye-slim
RUN apt-get update && apt-get install -y extra-runtime-dependencies && rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/local/cargo/bin/myapp /usr/local/bin/myapp
CMD ["myapp"]

why alpine exit automatically?

it is normal. because it does not have a process running continually.
https://stackoverflow.com/questions/30209776/docker-container-will-automatically-stop-after-docker-run-d
https://joshhu.gitbooks.io/dockercommands/content/Containers/DockerRunMore.html

docker run --entrypoint "/bin/sh" -it alpine/git
docker -it alpine sh

workaround

ENTRYPOINT ["tail"]
CMD ["-f","/dev/null"]

or

docker run -d centos tail -f /dev/null

run docker from docker file

docker build --tag 'isearch' .
docker run --rm isearch cool

copy docer file to another machine

https://stackoverflow.com/questions/23935141/how-to-copy-docker-images-from-one-host-to-another-without-using-a-repository

docker save -o <path for generated tar file> <image name>
docker load -i <path to image tar file>

example

docker save -o c:/myfile.tar centos:16
docker save -o C:\path\to\file.tar repository/imagename

docker proxy

since docker is in docker NAT env, so it's hard to access host's proxy server.
there is a solution.

https://stackoverflow.com/questions/53510864/docker-build-error-could-not-connect-to-server-behind-proxy
https://stackoverflow.com/questions/38101749/building-a-docker-image-for-a-node-js-app-fails-behind-proxy/38901128#38901128

{
  "proxies": {
    "default": {
      "httpProxy": "http://host.docker.internal:7890",
      "httpsProxy": "http://host.docker.internal:7890",
      "noProxy": "*.test.example.com,.example.org,127.0.0.0/8"
    }
  }
}

docker permission in linux

https://linuxhandbook.com/docker-permission-denied/

sudo groupadd docker
sudo usermod -aG docker $USER

groups

newgrp docker

docker with gpu

https://stackoverflow.com/questions/75118992/docker-error-response-from-daemon-could-not-select-device-driver-with-capab

curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey |sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
&& curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list \
&& sudo apt-get update

sudo apt-get install -y nvidia-container-toolkit

sudo nvidia-ctk runtime configure --runtime=docker

sudo systemctl restart docker

refer to

https://docs.docker.com/get-started/docker_cheatsheet.pdf
https://github.com/jedisct1/minisign/blob/master/Dockerfile
https://stackoverflow.com/questions/48190305/docker-cant-start-container-correctly-on-mac
https://stackoverflow.com/questions/36075525/how-do-i-run-a-docker-instance-from-a-dockerfile
https://stackoverflow.com/questions/25230812/when-should-i-use-dockers-container-name
https://joshhu.gitbooks.io/dockercommands/content/Containers/concepts.html
https://docs.docker.com/network/proxy/
https://qiita.com/koizumi_naoto/items/17533c425cf33ff0235d
https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach
https://stackoverflow.com/questions/35689628/starting-a-shell-in-the-docker-alpine-container