First of all, you should understand the basic concept of docker
- Mirror image (Image)
- Container
- Repository
docker installation and startup
yum install -y epel-release yum install docker-io # install docker # Configuration file /etc/sysconfig/docker chkconfig docker on # join boot service docker start # start the docker service # Basic information view docker version # View the version number of docker, including client, server, dependent Go, etc. docker info # View system (docker) level information, including managed images, number of containers, etc. docker pull centos download docker images [ centos ] view docker run -i -t centos /bin/bash
Mirror acquisition and use
# search mirror docker search <image> # Search for image in docker index # download mirror docker pull <image> # pull image from docker registry server # view mirror docker images: # list images docker images -a # list all images (including history) docker rmi <image ID>: # delete one or more images
# Create container with image docker run -i -t sauloal/ubuntu14.04 docker run -i -t sauloal/ubuntu14.04 /bin/bash # Create a container, let the bash application run in it, and close the container after exiting docker run -itd --name centos_aways --restart=always centos #Create a container named centos_aways and restart automatically # --restart parameter: always restart; restart when on-failure exit status is not 0; default is no, no restart # view container docker ps : list all currently running containers docker ps -l : list the most recently started container docker ps -a : List all containers (including history, that is, containers that have been run) docker ps -q : list the container ID of the last run # start the container again docker start/stop/restart <container> #: start/stop/restart container docker start [container_id] #: Run a container again (including historical containers) #Enter the running docker container docker exec -it [container_id] /bin/bash docker run -i -t -p <host_port:contain_port> #: Map the HOST port to the container to facilitate external access to services in the container. The host_port can be omitted. Omitting indicates that the container_port is mapped to a dynamic port. # delete container docker rm <container...> #: delete one or more containers docker rm `docker ps -a -q` #: delete all containers docker ps -a -q | xargs docker rm #: same as above, delete all containers
Container Resource Limit Parameters
-m 1024m --memory-swap=1024m # Limit the maximum memory usage (bug: the process is killed after it exceeds) --cpuset-cpus="0,1" # Limit the container to use CPU
Docker container auto-start parameters with the system
docker run --restart=always redis
- no – the default value, if the container hangs, it will not restart automatically
- on-failure - restart the container when the container exits with a non-zero code
- Also accepts an optional maximum number of restarts parameter (eg on-failure: 5).
- always - reboot regardless of exit code
docker run -itd --name test01 -p IP:sport:dport -m 1024m --memory-swap=1024m --cpuset-cpus="0,1" --restart=always <image ID> docker exec -it test01 bash # You can also use the exec command to enter the container
Persistent containers and images
3.1 Generate a new image through the container
A running image is called a container. You can make changes to the container (such as deleting a file), but these changes will not affect the image. However, you can turn a running container into a new image using the docker commit command.
docker commit <container> [repo:tag] # Solidify a container into a new image, and the following repo:tag is optional.
3.2 Endurance container
The export command is used to persist the container
docker export <CONTAINER ID> > /tmp/export.tar
3.3 Persistent image
Save command is used to persist the image
docker save image ID > /tmp/save.tar
3.4 Import persistent container
remove container 2161509ff65e
docker rm 2161509ff65e
Import export.tar file
cat /tmp/export.tar | docker import - export:latest
3.5 Import persistent image
delete image daa11948e23d
docker rmi daa11948e23d
Import the save.tar file
docker load < /tmp/save.tar
tag the image
docker tag daa11948e23d load:tag
3.6 Difference between export-import and save-load
Images that are exported and then imported (export-import) lose all history, while images that are saved and then loaded (save-load) do not lose history and layers. This means that by exporting and then importing, you cannot roll back to the previous layer. At the same time, by persisting the entire image by saving and then loading, you can achieve layer rollback. (You can execute docker tag to roll back the previous layer).
some common commands
docker logs $CONTAINER_ID #View the running log of the docker instance to ensure normal operation docker inspect $CONTAINER_ID #docker inspect <image|container> View the underlying information of an image or container docker build <path> Find the configuration file named Dockerfile under the path path, and use this configuration to generate a new image docker build -t repo[:tag] Same as above, you can specify repo and optional tag docker build - < <dockerfile> Use the specified dockerfile configuration file, docker gets the content in stdin, and use this configuration to generate a new image docker port <container> <container port> Check which local port is mapped to the specified port of the container. You can also see it with docker ps