save image to .tar file:
docker save -o exportName.tar imageName
docker save imageName > exportName.tar
load .tar to image:
docker load -i image.tar
list images:
docker image ls
docker images
make container: 实例:制作自己的 Docker 容器
docker container run -p pcSidePort:containerSidePort -it imageName
check docker version:
docker version
check the state of docker:
docker ps -a
restart the container:
docker restart IDNumber
show front 100 line logs:
docker logs IDNumber -f --tail 100
start docker:
systemctl start docker
RHEL/CentOS 8 Preparation for Installation:
sudo dnf -y install iptables --nobest ||:
sudo dnf -y install iptables-services --nobest
sudo systemctl disable firewalld ||:
sudo systemctl disable nftables ||:
sudo systemctl enable --now iptables.service
sudo dnf rm -y podman
sudo dnf config-manager \
--add-repo=https://download.docker.com/linux/centos/docker-ce.repo
sudo sed -i '/ˆgpgkey=https:\/\/download.docker.com\/linux\/centos\/gpg/a module_hotfixes=True' \
/etc/yum.repos.d/docker-ce.repo
sudo dnf install -y --nobest docker-ce
yum erase podman buildah
sudo yum install docker-ce docker-ce-cli containerd.io
sudo systemctl enable --now docker
sudo systemctl status docker
Create docker user group:sudo groupadd dockerset group password and make user become group administrator:sudo gpasswd -a $USER dockerput user into a group:sudo usermod -aG docker $USERSwitch the current non-root user group to the docker user group:exec newgrp dockerveryfy it:docker run hello-world
Installation of docker-compose:
ver1:
binary="docker-compose-$(uname -s)-$(uname -m)"
compose=/usr/local/bin/docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/$binary" -o $compose
sudo chmod +x $compose
sudo restorecon -v $compose
ver2:
sudo curl -L "https://github.com/docker/compose/releases/download/v2.29.2/$binary" -o $compose
The restorecon command is only needed in an SELinux environment.
check all the output log of the containers in docker:
docker-compose logs
show the node in cluster:
docker node ls
show the network in docker:
docker network ls
show current stack:
docker stack ls
then show task in the stack:
docker stack ps stackName
show docker system information:
docker info
get container’s metadata:
docker inspect IDname
about docker plugin:
docker plugin COMMAND
how to change docker network setting(docker0):
check docker config:
docker config ls
get into the container:
docker exec -it ContainerID bash
set proxy for docker:
sudo vi /etc/systemd/system/docker.service.d/http-proxy.conf
Then fill in the proxy server content:
[Service]
Environment="HTTP_PROXY=http://xxx:PortNumber"
Environment="HTTPS_PROXY=http://xxx:PortNumber"
then restart docker and test the content:
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl show --property=Environment docker
How to save all Docker images and copy to another machine
start docker container, load from local otherwise from docker hub(-d means run background, -it means keep running):
docker run --restart=always --name containerName -p hostPort:containerPort -e firstEnvironmentVariable=xxx -it -d imageName:tagVersion
down load the container image: docker pull imageName
stop the container: docker stop containerID
start the container: docker start containerID
delete the container: docker rm containerID
delete the container image: docker rmi containerID:version
Dockerfile example(use ubuntu 20.04, as root, set env variable envVariableName, set work directory, use add to copy the file from internet, execute script.py then end container):
FROM ubuntu:20.04
USER root
RUN apt update
RUN apt install -y python3.9
RUN apt install -y python3-pip
COPY requirements.txt .
RUN python3.9 -m pip install -r requirements.txt
ENV envVariableName=xxx
WORKDIR /absolutePath
ADD https://xxx.xxx .
COPY script.py .
ENTRYPOINT ["python3.9", "script.py"]
create a docker image: docker build -t imageName:version -f Dockerfile .
Cannot install docker in a RHEL server —-> slirp4netns >= 0.4
check container automatically start status:
docker inspect -f "{{.Name}} {{.HostConfig.RestartPolicy.Name}}" $(docker ps -aq)
clear all the docker images:
docker stop $(sudo docker ps -aq)
docker rm $(sudo docker ps -aq)
docker rmi $(sudo docker images -q)
docker system prune -a
resource usage statistics:docker stats
docker stats container_id_or_name
clean unused Docker resources:
docker system prune -a