Install docker

Reference https://yeasy.gitbooks.io/docker_practice/install/centos.html

Create docker group

1
2
3
4
5
6
$ sudo groupadd docker
$ sudo usermod -aG docker $USER

or

$ sudo gpasswd -a $USER docker

Start docker

1
2
# systemctl start docker
# systemctl enable docker

Setup mirrors

1
2
3
4
5
# cat /etc/docker/daemon.json
{
        "registry-mirrors": ["https://registry.docker-cn.com","https://nrbewqda.mirror.aliyuncs.com","https://dmmxhzvq.mirror.aliyuncs.com"]
}

Pull image

1
2
3
4
5
$ docker run hello-world
$ docker pull ubuntu:latest
$ docker pull fedora:29
$ docker pull dokken/ubuntu-20.10
$ docker images

Start container

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$ docker run -dit --name ubuntu-20.10 dokken/ubuntu-20.10
$ docker exec -it ubuntu-20.10 /bin/bash
$ apt-get update
$ apt-get install vim

$ docker run unbutu:latest /bin/echo 'Hello World'
$ docker run -t -i ubuntu:latest /bin/bash

$ docker run -dit bash
$ docker container ls
$ docker attach $id	<- container will stop after exit stdin
$ docker exec -it $id bash

$ docker container stop $id

Show netns with ip

1
2
3
$ docker inspect -f '{{.State.Pid}}' $id
$ sudo mkdir -p /var/run/netns
$ sudo ln -s /proc/$pid/ns/net /var/run/netns/$pid
1
2
3
4
5
$ docker run -dit --cap-add=NET_ADMIN bash <- add net admin permisson

$ docker network create -d macvlan --subnet=192.168.100.0/24 --gateway=192.168.100.1 -o parent=p4p1 pub_net
$ docker run -dit --cap-add=NET_ADMIN --net=pub_net centos
$ docker exec -it $id bash

save containers

1
2
$ docker ps
$ docker commit $id new_name

remove container

1
2
$ docker container ls -a
$ docker container rm $id

issues

Q: docker: Error response from daemon: OCI runtime create failed: this version of runc doesn’t work on cgroups v2: unknown. A: This is because the cgroup version on fedora is too high, we need to downgrade to small version by

1
2
dnf install -y grubby
grubby --update-kernel=ALL --args="systemd.unified_cgroup_hierarchy=0"

Reference: https://www.runoob.com/docker/docker-tutorial.html