Docker container image and its use

Introduction: This article mainly explains the basic concept of Docker container image and its common commands.

For image download, domain name resolution and time synchronization, please click Alibaba open source mirror station

1, Basic concepts

1. Docker image

The image is a read-only docker template. Images can be used to create docker containers. Docker provides a very simple mechanism to create images or update existing images. Users can even download a ready image from others for direct use.

2. Docker container

Docker uses containers to run applications. A container is a running instance created from a mirror. It can be started, started, stopped and deleted. Each container is isolated from each other to ensure a safe platform.

3. Docker repository

The warehouse is a place for centralized storage of image files. Sometimes, warehouse and warehouse registration server are confused and not strictly distinguished. In fact, the warehouse registration server often stores multiple warehouses. Each warehouse contains multiple images, and each image has a different tag. The warehouse is divided into two forms: public warehouse and private warehouse. The largest public warehouse is Docker Hub, which stores a large number of images for users to download. Domestic public warehouses, including Docker Pool, can provide more stable and fast reading access for mainland users. After users create their own image, they can use the push command to upload it to the public or private warehouse. In this way, when downloading and using the image on another machine, they only need to pull it down from the warehouse.

2, Docker basic commands

  • Installation of Docker
sudo apt install
  • Docker startup and status query
sudo service docker start
sudo service docker status
  • View Docker version
sudo docker version
  • View local mirror
sudo docker images
  • Download Image File
sudo docker pull ubuntu
  • Run Ubuntu image
docker run -i -t ubuntu /bin/bash
  • Delete mirror file
sudo docker rmi -f hello-world
  • View the started Docker service (container)
sudo docker ps –a
  • Stop docker service
sudo docker stop image-name
  • Log in to Docker
sudo docker login
  • Save the modified Docker container
OPTIONS explain:
        -a :The author of the submitted image;
        -c :use Dockerfile Command to create an image;
        -m :Explanatory text at the time of submission;
        -p :stay commit Pause container when
  • docker can support attaching directories on a host to an image
docker run -it -v /home/dock/Downloads:/usr/Downloads ubuntu64 /bin/bash
docker run -it -v /home/dock/Downloads:/usr/Downloads ubuntu64 /bin/bash
  • Deletion of Docker container
docker rm CONTAINER ID
  • Deletion of Docker image
docker rmi
  • Docker container start/stop/restart
docker start/stop/restart
docker start Command use-i Option to turn on interactive mode
  • Query image and container details
docker inspect
docker info
  • Connect to a running container
docker attach CONTAINER ID/name
  • Exit container without closing
ctrl + d Exit and close the container, ctrl + p + q Exit container without closing
  • The container installed by Docker's Ubuntu image has no ifconfig command and ping command
apt-get update
apt install net-tools       # ifconfig 
apt install iputils-ping     # ping
  • Docker installing mysql for Ubuntu
apt-get update
apt-get install -y mysql-server mysql-client
  • Docker container and host file copy
1)Copy files from the container to the host
    docker cp <containerId>:/file/path/within/container /host/path/target
2)Copy files from the host to the container
    sudo docker cp host_path containerID:container_path
3)problem FATA[0000] Error: Path not specified
    Version 1.6.2 doesn't allow copying from host to container, you need to upgrade to at least 1.8 for that support
  • docker version upgrade
1)Docker Source installation specified version:
    sudo apt-get install apt-transport-https
    sudo apt-key adv --keyserver hkp:// --recv-keys     36A1D7869245C8950F966E92D8576A8BA88D21E9
    sudo bash -c "echo deb docker main >/etc/apt/sources.list.d/docker.list"
    sudo apt-get update  --Update software source
    sudo apt-cache search docker  --Find the that need to be updated docker edition
    sudo apt-get install lxc-docker-1.9.1
2)Error resolution
    docker Execute after installation sudo service docker start,Prompt error: Failed to start docker.service: Unit docker.service is masked.
    systemctl unmask docker.service
        systemctl unmask docker.socket
        systemctl start docker.service
  • docker container backup
1)generate docker image
   docker commit -p 30b8f18f20b4 container-backup
2)We want to be Docker To upload or back up the image in the registry, we only need to run it docker login Command to log in Docker Registration Center, and then push the required image
    (1)docker login
    (2)docker tag a25ddfec4d2a arunpyasi/container-backup:test
        (3)docker push arunpyasi/container-backup
3)If we don't want to back up to docker Instead, we want to save this image in the local machine for future use, so we can use it as tar Package backup. To complete this operation, we need to run the following docker save Command.
          docker save -o ~/container-backup.tar container-backup
          tar -zcvf ubuntu2-backup.tar.gz container-backup.ta
  • docker recovery container
1)After we successfully backed up our Docker After the container, let's resume these fabrication now Docker Container for mirroring snapshots. If we've pushed these in the registry Docker Mirror image, then we just need to put that Docker Drag the image back and run it directly.
    docker pull arunpyasi/container-backup:test
2)If we put these Docker Mirror as tar If the package file is backed up locally, we just need to use docker load Command, followed by tar The backup path of the package can be loaded Docker Mirrored.
        tar -zxvf ubuntu2-backup.tar.gz 
    docker load -i ~/container-backup.tar
3)use docker image see
  • extc command

Using the attach command often the card segment. You can use the exec command instead

docker exec -it ubuntu /bin/bash

If you enter with attach above, exit will exit the container. You must exit with ctrl P Q to continue running in the background. If you exit with exec, you will not really exit the container and continue running in the background. You can use name or id

  • Common commands
docker run --name ubuntu -it ubuntu:16.04 /bin/bash
docker exec -it 96740370a5da /bin/bash

This article is transferred from: Docker container image and its use - Alibaba cloud developer community

Keywords: Operation & Maintenance Docker Container

Added by Transwarp-Tim on Mon, 21 Feb 2022 16:28:32 +0200