
docker on wsl2
installation:
curl -sSL https://get.docker.com/ | sh
start/stop/restart docker daemon:
sudo service docker start
sudo service docker stop
sudo service docker restart
access docker with no sudo
sudo groupadd docker
sudo usermod -aG docker $USER
# check: groups $USER | grep docker
check
check version
docker version --format '{{.Server.Version}}'
# dump raw JSON data:
docker version --format '{{json .}}'
hello-world
docker run --rm hello-world
registry configuration:
# check registry mirrors
docker info | tail
/etc/docker/daemon.json
:
{
"registry-mirrors": [
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
start the tutorial:
# -p host-port:container-port
docker run -d -p 80:80 docker/getting-started
# then open a web browser and navigate to http://localhost:80 .
build container image:
docker build -t image-name .
remove container
# get the id of the container
docker ps
# stop the container
docker stop <the-container-id>
# remove container
docker rm <the-container-id>
# or use a single command: docker rm -f <the-container-id>
push image
# login to the Docker Hub
docker login -u YOUR-USER-NAME
# use the docker tag to give the image a new name
docker tag image-name YOUR-USER-NAME/image-name
# push image
docker push YOUR-USER-NAME/image-name
enter container environment then execute command
docker exec <container-id> command
docker volume
volume type comparison
Named Volumes | Bind Mounts | |
---|---|---|
Host Location | Docker chooses | You control |
Mount Example (using -v) | my-volume:/usr/local/data | /path/to/data:/usr/local/data |
Populates new volume with container contents | Yes | No |
Supports Volume Drivers | Yes | No |
named volume
# create a volume
docker volume create <volume-name>
# use the named volume and mount it to mount-dir.
docker run [option] -v <volume-name>:<mount-dir> <image-name>
# actual location
docker volume inspect <volume-name>
bind mounts
docker run [option] -v <host-dir>:<mount-dir> <image-name>
docker logs
docker logs -f <container-id>
docker network
# create the network
docker network create <network-name>
# join network and assign network alias
docker run --network <network-name> --network-alias <network-alias> <...>
docker scan
# see the layers in the image
docker scan <image-name>
docker image
docker image history [--no-trunc] <image-name>
docker-compose
# version info
docker-compose version
# start up the application stack
docker-compose up
# logs
docker-compose logs -f <service>
# tearing down
docker-compose down