Services/Docker: Difference between revisions

From Fundamental Ramen
Jump to navigation Jump to search
Line 5: Line 5:
|-
|-
| Run debian image on Windows ||
| Run debian image on Windows ||
<source lang="bash">
<syntaxhighlight lang="bash">
# Git Bash
# Git Bash
winpty docker run --name=debian0 -it debian:12
winpty docker run --name=debian0 -it debian:12
Line 11: Line 11:
# Power Shell
# Power Shell
docker run --name=debian0 -it debian:12
docker run --name=debian0 -it debian:12
</source>
</syntaxhighlight>
|-
|-
| Open shell of running container ||
| Open shell of running container ||
<source lang="bash">
<syntaxhighlight lang="bash">
docker exec -it mwdev-mediawiki-1 /bin/bash
docker exec -it mwdev-mediawiki-1 /bin/bash
</source>
</syntaxhighlight>
|-
|-
| Copy file from running container ||
| Copy file from running container ||
<source lang="bash">
<syntaxhighlight lang="bash">
docker cp mwdev-mediawiki-1:/var/www/data/my_wiki.sqlite .
docker cp mwdev-mediawiki-1:/var/www/data/my_wiki.sqlite .
</source>
</syntaxhighlight>
|-
|-
| Build image ||
| Build image ||
<source lang="bash">
<syntaxhighlight lang="bash">
docker build .
docker build .
docker build -t gdtile:2.1 .
docker build -t gdtile:2.1 .
docker tag c8abdb693116 gdtile:2.1
docker tag c8abdb693116 gdtile:2.1
</source>
</syntaxhighlight>
|-
|-
| Remove exited containers ||
| Remove exited containers ||
<source lang="bash">
<syntaxhighlight lang="bash">
docker rm $(docker ps -f 'status=exited' -q)
docker rm $(docker ps -f 'status=exited' -q)


sudo docker rm $(sudo docker ps -f 'status=exited' -q)
sudo docker rm $(sudo docker ps -f 'status=exited' -q)
</source>
</syntaxhighlight>
|-
|-
| Remove unused images ||
| Remove unused images ||
<source lang="bash">
<syntaxhighlight lang="bash">
docker rmi $(docker images -f "dangling=true" -q)
docker rmi $(docker images -f "dangling=true" -q)


sudo docker rmi $(sudo docker images -f "dangling=true" -q)
sudo docker rmi $(sudo docker images -f "dangling=true" -q)
</source>
</syntaxhighlight>
|-
|-
| run and start it again ||
| run and start it again ||
<source lang="docker">
<syntaxhighlight lang="docker">
docker run --name sandbox0 \
docker run --name sandbox0 \
   --volume a:b \
   --volume a:b \
   -it alpine:3.11
   -it alpine:3.11
docker start -i sandbox0
docker start -i sandbox0
</source>
</syntaxhighlight>
|-
|-
| Make apt faster ||
| Make apt faster ||
<source lang="docker">
<syntaxhighlight lang="docker">
FROM ubuntu:20.04
FROM ubuntu:20.04


RUN sed -i 's/\(archive\|security\).ubuntu.com/free.nchc.org.tw/' /etc/apt/sources.list
RUN sed -i 's/\(archive\|security\).ubuntu.com/free.nchc.org.tw/' /etc/apt/sources.list
RUN apt update
RUN apt update
</source>
</syntaxhighlight>


<source lang="bash">
<syntaxhighlight lang="bash">
# In host
# In host
docker run -it ubuntu:20.04
docker run -it ubuntu:20.04
Line 66: Line 66:
sed -i 's/\(archive\|security\).ubuntu.com/free.nchc.org.tw/' /etc/apt/sources.list
sed -i 's/\(archive\|security\).ubuntu.com/free.nchc.org.tw/' /etc/apt/sources.list
apt update
apt update
</source>
</syntaxhighlight>
|-
|-
| Run ||
| Run ||
<source lang="docker">
<syntaxhighlight lang="docker">
# Mapping directory on Windows, do not use ~
# Mapping directory on Windows, do not use ~
docker run -v C:\Users\User\repos\something:/docroot -it someimage bash
docker run -v C:\Users\User\repos\something:/docroot -it someimage bash
docker run -v %USERPROFILE%\repos\webserv:/docroot -it webserv-compose_web bash
docker run -v %USERPROFILE%\repos\webserv:/docroot -it webserv-compose_web bash
</source>
</syntaxhighlight>
|-
|-
| test ||
| test ||
<source lang="docker">
<syntaxhighlight lang="docker">
docker ps --size --all
docker ps --size --all
</source>
</syntaxhighlight>
|-
|-
| logs ||
| logs ||
<source lang="docker">
<syntaxhighlight lang="docker">
docker ps logs -ft container
docker ps logs -ft container
</source>
</syntaxhighlight>
|}
|}



Revision as of 07:42, 8 April 2025

Clean Docker

TODO Command
Run debian image on Windows
# Git Bash
winpty docker run --name=debian0 -it debian:12

# Power Shell
docker run --name=debian0 -it debian:12
Open shell of running container
docker exec -it mwdev-mediawiki-1 /bin/bash
Copy file from running container
docker cp mwdev-mediawiki-1:/var/www/data/my_wiki.sqlite .
Build image
docker build .
docker build -t gdtile:2.1 .
docker tag c8abdb693116 gdtile:2.1
Remove exited containers
docker rm $(docker ps -f 'status=exited' -q)

sudo docker rm $(sudo docker ps -f 'status=exited' -q)
Remove unused images
docker rmi $(docker images -f "dangling=true" -q)

sudo docker rmi $(sudo docker images -f "dangling=true" -q)
run and start it again
docker run --name sandbox0 \
  --volume a:b \
  -it alpine:3.11
docker start -i sandbox0
Make apt faster
FROM ubuntu:20.04

RUN sed -i 's/\(archive\|security\).ubuntu.com/free.nchc.org.tw/' /etc/apt/sources.list
RUN apt update
# In host
docker run -it ubuntu:20.04
# In container
sed -i 's/\(archive\|security\).ubuntu.com/free.nchc.org.tw/' /etc/apt/sources.list
apt update
Run
# Mapping directory on Windows, do not use ~
docker run -v C:\Users\User\repos\something:/docroot -it someimage bash
docker run -v %USERPROFILE%\repos\webserv:/docroot -it webserv-compose_web bash
test
docker ps --size --all
logs
docker ps logs -ft container

Problems on Windows

Manipulating directory of container in GitBash (MSYS2)

$ docker exec hix-local-server_db_1 ls /
# or with single quotes
$ docker exec hix-local-server_db_1 ls '/'
# or with double quotes
$ docker exec hix-local-server_db_1 ls "/"
ls: cannot access 'C:/Program Files/Git/': No such file or directory

The best alternative way to solve this problem is WSL integration.

Require two steps.

  • Install Windows embedded Ubuntu 20.04.
  • Change settings of Docker Desktop.

File:Wsl-docker.jpg

Using volume in GitBash (MSYS2)

Valid host directory

winpty docker run --name sandbox0 -v "C:\Users\Raymond\repos\twnews":/vol -it alpine:3.11
winpty docker run --name sandbox0 -v 'C:\Users\Raymond\repos\twnews':/vol -it alpine:3.11
winpty docker run --name sandbox0 -v C:\\Users\\Raymond\\repos\\twnews:/vol -it alpine:3.11

Unexpected error

$ winpty docker run --name sandbox0 -v C:\Users\Raymond\repos\twnews:/vol -it alpine:3.11
docker: Error response from daemon: The system cannot find the file specified.
See 'docker run --help'.

$ winpty docker run --name sandbox0 -v C:/Users/Raymond/repos/twnews:/vol -it alpine:3.11
docker: Error response from daemon: invalid mode: \Program Files\Git\vol.
See 'docker run --help'.

$ winpty docker run --name sandbox0 -v ~:/vol -it alpine:3.11
docker: Error response from daemon: mkdir C:\Users\Raymond;C: Access is denied.
See 'docker run --help'.

Bad guest path

$ winpty docker run --name sandbox0 -v /C/Users/Raymond/repos/twnews:/vol -it alpine:3.11
or
$ winpty docker run --name sandbox0 -v "/C/Users/Raymond/repos/twnews":/vol -it alpine:3.11

/ # ls -l
total 56
drwxrwxrwx    1 root     root             0 Sep 30 07:29 \Program Files\Git\vol
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 bin
drwxr-xr-x    5 root     root           360 Sep 30 08:46 dev
drwxr-xr-x    1 root     root          4096 Sep 30 08:46 etc
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 home
drwxr-xr-x    5 root     root          4096 Apr 23 06:25 lib
drwxr-xr-x    5 root     root          4096 Apr 23 06:25 media
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 mnt
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 opt
dr-xr-xr-x  154 root     root             0 Sep 30 08:46 proc
drwx------    1 root     root          4096 Sep 30 08:46 root
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 run
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 sbin
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 srv
dr-xr-xr-x   12 root     root             0 Sep 30 08:46 sys
drwxrwxrwt    2 root     root          4096 Apr 23 06:25 tmp
drwxr-xr-x    7 root     root          4096 Apr 23 06:25 usr
drwxr-xr-x   12 root     root          4096 Apr 23 06:25 var

System comparison

Ubuntu 20.04 Alpine 3.11
bash O X
netstat X O
ifconfig X O