Docker--basic operations of containers, warehouses, and mirrors

2. Docker container operation

1. View

View started containers: docker ps

View all containers: docker ps -a

Check the status of started containers: docker stats

View the ids of started containers: docker ps -q

View the last created container: docker ps -l

2. Create a container

Create the container and enter the container: docker run -i -t ubuntu:15.10 /bin/bash

    • -t: Specify a pseudo terminal or terminal within the new container.

    • -i: Allows you to interact with the standard input (STDIN) within the container.

    • ubuntu:15.10 specifies the image to be run. Docker first checks whether the image exists on the local host. If it does not exist, Docker will download the public image from the image warehouse Docker Hub.

    • /bin/bash: The command is placed after the image name. Here we hope to have an interactive shell, so /bin/bash is used.

Create a container with a specified name (ubuntu-test) running in the background: docker run -itd --name ubuntu-test ubuntu /bin/bash

    • Adding the -d parameter will not enter the container by default
    • Container naming: docker run -d -P --name runoob training/webapp python app.py
      • --name flag to name the container

3. Start the container

Start the container: docker start (CONTAINER ID), for example docker start cf2cb52b7593

Restart the container: docker restart (CONTAINER ID)

4. Enter the container. When using  the -d  parameter, the container will enter the background after starting. If you want to enter the container at this time, you can enter through the following command:

Enter the container and stop the container after exiting the container terminal: docker attach (CONTAINER ID)

Enter the container and the container will not stop after exiting the container terminal: docker exec -it (CONTAINER ID) or (NAMES) /bin/bash

5. Stop the container

Stop the container: docker stop (CONTAINER ID)

6. View container logs

View container logs: docker logs (CONTAINER ID)

7. Delete the container

To delete a container, use the rm command, -f to force deletion. Running containers can also be deleted: docker rm -f (CONTAINER ID)

Clean up all terminated containers: docker container prune

8. Export and import containers

Export container: docker export (CONTAINER ID) > path/name.tar

Import container: cat path/name.tar | docker import - test/ubuntu:v1, you can check whether the image is imported successfully through docker images

3. Warehouse management Docker Hub

The docker warehouse is where images are stored

  1. Registration: Enter username, email, password, human-machine identity verification
  2. Log in to the warehouse: docker login
  3. Exit the warehouse: docker logout
  4. Search for images in the repository: docker search (image name)
  5. Pull the image from the warehouse: docker pull (image name)
  6. Push the image to the warehouse: docker push (image name) 
    1. 例子
      $ docker tag ubuntu:18.04 username/ubuntu:18.04
      $ docker image ls
      
      REPOSITORY      TAG        IMAGE ID            CREATED           ...  
      ubuntu          18.04      275d79972a86        6 days ago        ...  
      username/ubuntu 18.04      275d79972a86        6 days ago        ...  
      $ docker push username/ubuntu:18.04
      $ docker search username/ubuntu
      
      NAME             DESCRIPTION       STARS         OFFICIAL    AUTOMATED
      username/ubuntu

4. Use of Docker images

When running a container, if the image used does not exist locally, docker will automatically download it from the docker image warehouse. The default is to download from the Docker Hub public image source.

  1. List images

    1. docker images
      1. REPOSITOPY: Represents the warehouse source of the image
      2. TAG: The tag of the image
      3. IMAGE ID: Image ID
      4. CREATED: Image creation time
      5. SIZE: Image size 
    2. The same warehouse source can have multiple TAGs, representing different versions of this warehouse source, such as ubuntu15.10, 13.10 and other versions. 
      1. docker run -t -i ubuntu:15.10 /bin/bash
  2. Get a new image

    1. docker pull  ubuntu:13.10
  3. Find an image

    1. Docker Hub URL: https://hub.docker.com/
    2. docker search (image name)
  4. Delete image

    1. docker rmi (image name)
  5. Create image

    1. Update the image, update the image from the already created container, and submit the image
      1. First create a container: docker run -t -i ubuntu:15.10 /bin/bash
      2. Update the image: apt-get update
      3. Submit a copy of the container and create a new image: docker commit -m="has update" -a="runoob" (CONTAINER ID) runoob/ubuntu:v2
        1. -m: Submitted description information
        2. -a: Specify the image author
        3. runoob/ubuntu:v2: Specify the target image name to be created
    2. Build image
      1. First create a Dockerfile file
        1. Each command creates a new layer on the image, and the prefix of each command must be uppercase.
        2. The first FROM specifies which mirror source to use
        3. The RUN command tells docker to execute commands within the image and install what
        4. example:
          runoob@runoob:~$ cat Dockerfile 
          FROM    centos:7
          MAINTAINER      Fisher "[email protected]"
          
          RUN     /bin/echo 'root:123456' |chpasswd
          RUN     useradd runoob
          RUN     /bin/echo 'runoob:123456' |chpasswd
          RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
          EXPOSE  22
          EXPOSE  80
          CMD     /usr/sbin/sshd -D
      2. Create an image through the docker build command 
        1. docker build -t runoob/centos:7 .

  6. Create a container using an image

    1. docker run -t -i -d (specify image name) /bin/bash
  7. Set the image tag (set an easily identifiable tag)

    1. docker tag (CONTAINER ID)runoob/centos:dev

Guess you like

Origin blog.csdn.net/xch622114/article/details/130854817
Recommended