Docker¶
installation¶
MacOS¶
or
https://docs.docker.com/desktop/install/mac-install/
Ubuntu¶
official doc: https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository
update apt and install necessary packages
add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
set up the stable repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
install docker engine
test if is installed correctly
will download the image hello-world
and pring some messages
sudo docker ps -a
to check if it's running
Arch¶
install
start
image types¶
https://medium.com/swlh/62171ed4531d
- slim: smaller image
- alpine: even smaller image
init¶
make docker start on boot¶
https://docs.docker.com/engine/install/linux-postinstall/#configure-docker-to-start-on-boot
start docker¶
or
container related¶
show running containers¶
go into a docker container¶
or
https://github.com/docker/for-linux/issues/246#issuecomment-455010125
stop a docker container¶
remove a docker container¶
container log¶
Run a container - docker run¶
Run a docker container from an image¶
docker run --name <container_name> -d --rm <image_name/id>
# if it exits immediately, try
docker run --name <container_name> -dit --rm <image_name/id>
--rm
for auto remove when a container with the same name already exist (but only when it is also created with the --rm
flag)
https://stackoverflow.com/a/31143261/15493213
with port forwarding
with webcam
container resource control¶
- assign to specific cpu(s)
--cpuset-cpus <#cpu>
- e.g.
--cpuset-cpus 1
--cpuset-cpus 1,3
- limit cpu usage
--cpus <max # of cpu>
- e.g.
--cpus 0.5
- check the cpu related config of a container
docker inspect <container> | grep -i cpu
- set 1
cpus
-> 10^9NanoCpus
Image related¶
Store images on cloud repo¶
- Docker Hub
- Harbor
Show all docker images¶
remove a docker image¶
build a docker image¶
build a docker image following the Dockerfile
in the directory
rebuild
push image to dockerhub¶
Login to dockerhub if you haven't
Tag the image
Push the image
docker-compose¶
will use docker-compose.yml
build & start containers¶
build containers¶
start containers¶
stop containers¶
device¶
use your webcam in docker container¶
with docker run
add --device=/dev/video0:/dev/video0
when doing docker run
with docker-compose
add this to your service in docker-compose.yml
Dockerize a flask app¶
# syntax=docker/dockerfile:1
FROM python:3.8-slim-buster
WORKDIR /dir_name
ADD . /dir_name
RUN pip3 install -r requirements.txt
CMD python3 application.py
remove unused things¶
docker images & containers are stored in /var/lib/docker/
remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache
remove all images without at least one container associated to them
https://stackoverflow.com/questions/46672001/
Troubleshooting¶
---> [Warning] IPv4 forwarding is disabled. Networking will not work.¶
restard docker
https://stackoverflow.com/a/59135923/15493213
express app work when run direclty but not with docker¶
try listening on host 0.0.0.0
instead of localhost
python cv2 ImportError: libGL.so.1: cannot open shared object file: No such file or directory¶
Use opencv-python-headless
instead of opencv-python
https://stackoverflow.com/a/69655111/15493213
python app doesn't print things¶
Use python3 -u <python-file>
instead of python3 <python-file>
. If still nothing, add the PYTHONUNBUFFERED
variable.
with docker run
add PYTHONUNBUFFERED=1
when doing docker run
with Dockerfile
with docker-compose
in docker-compose.yml
, add this in your flask service
refs
docker: Error response from daemon: No command specified.¶
supply a command (e.g. /bin/bash
) after your docker run
command
qemu-x86_64: Could not open '/lib64/ld-linux-x86-64.so.2': No such file or directory¶
add --platform linux/amd64
when doing docker run
https://stackoverflow.com/a/71611002/15493213
cannot connect to docker daemon when using testcontainer in Mac¶
error message
fix
You may need to run this everytime you start docker.
See