Docker installation and common operations of images and containers

Table of contents

1. Docker installation

1.1. Prerequisites

1.2.yum install docker

1.3.Docker start and stop operations

1.4.Docker image source change 

1.5. Verify Docker startup image

2. Docker common operations

2.1 Mirror related operations

List images

Find an image

Pull image

Set image label

Delete image

2.2 Container related operations

Make a container

View container

View container network port mappings

Container start/stop/restart 

Enter the container

View container logs

Delete container


1. Docker installation

1.1. Prerequisites

Docker is only supported by the kernel in the CentOS distribution.

Docker runs on CentOS 7, which requires the system to be 64-bit and the system kernel version to be 3.10 or above.

Docker runs on CentOS-6.5 or higher, which requires the system to be 64-bit and the system kernel version to be 2.6.32-431 or higher.

Use  uname -r to view the current system kernel version. The current demo version is CentOS7.

uname -r

1.2.yum install docker

CentOS comes with Docker and can be installed directly.

yum -y install docker

After the installation is completed, as shown in the figure below

1.3.Docker start and stop operations

#启动Docker
systemctl start docker
#停止Docker
systemctl stop docker
#重启Docker
systemctl restart docker
#查看Docker运行状态
systemctl status docker
#查看Docker版本
docker version

1.4.Docker image source change 

Create or modify the /etc/docker/daemon.json file to the following form

vim /etc/docker/daemon.json

Modify the daemon.json file to the domestic mirror source, currently set to NetEase mirror source

{"registry-mirrors": ["http://hub-mirror.c.163.com"] }

Reload the configuration file and restart Docker

#重载配置文件
systemctl daemon-reload
#重启docker
systemctl restart docker

Attached is the domestic acceleration address:

1.5. Verify Docker startup image

docker run hello-world

Since there is no hello-world image locally, a hello-world image will be downloaded and run in the container.

2. Docker common operations

2.1 Mirror related operations

  • List images

docker images

 Parameter Description

  • REPOSTITORY: Represents the warehouse source of the image

  • TAG: The tag of the image

  • IMAGE ID: Image ID

  • CREATED: Image creation time

  • SIZE: Image size

  • Find an image

Syntax docker search [OPTIONS] , such as finding nginx

docker search nginx

 Parameter description

  • NAME: The name of the mirror warehouse source
  • DESCRIPTION: Description of the image
  • STARS: Number of likes/collections
  • OFFICIAL: Is docker officially released?
  • AUTOMATED: Automatically built.
  • Pull image

 Syntax docker pull [OPTIONS] , such as pulling nginx, pulling the default version image (the first one at the top of the default query)

docker pull nginx

If you need to specify a version or a specified image, you can pull it based on the version or specified image.

#拉取指定版本镜像
docker pull nginx:1.18
#拉取指定镜像,参数即search的NAME
docker pull docker.io/bitnami/nginx

  • Set image label

Syntax docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] to add a new tag to the image

docker tag 9c7a54a9a43c hello-world.test

  •  Delete image

Syntax docker rmi [OPTIONS] IMAGE [IMAGE...] , delete one or more images, the parameter can be image ID or NAMES, multiple images are separated by spaces

When the parameter is REPOSTITORY, the REPOSTITORY with the same name will delete latest by default. If you need to delete the specified version, add the version number.

When the parameter is IMAGE ID, the specified image will be deleted directly.

2.2 Container related operations

  • Make a container

Syntax docker run [OPTIONS] IMAGE [COMMAND] [ARG...] , create a new container through the run command

Create an nginx with port 80

#简易启动一个nginx
docker run -p 80:80 -d nginx

Description of common options

-d, --detach=false, specifies whether the container runs in the foreground or background, the default is false
-i, --interactive=false, opens STDIN for console interaction
-t, --tty=false, allocates tty devices, This can support terminal login, the default is false
-u, --user="", specify the user of the container
-a, --attach=[], log in to the container (must be a container started with docker run -d)
-w, --workdir="", specify the working directory of the container
-c, --cpu-shares=0, set the container CPU weight, use
-e, --env=[] in the CPU sharing scenario, specify environment variables, which can be used in the container Use this environment variable
-m, --memory="", specify the memory limit of the container
-P, --publish-all=false, specify the port exposed by the container
-p, --publish=[], specify the port exposed by the container
-h, --hostname="", specify the host name of the container
-v, --volume=[], mount the storage volume to the container, mount it to a directory of the container
--volumes-from=[], give The container mounts volumes on other containers, mounts them to a directory in the container
--cap-add=[], and adds permissions. For a detailed list of permissions, see: http://linux.die.net/man/7/capabilities
- -cap-drop=[], delete permissions. For a detailed list of permissions, see: http://linux.die.net/man/7/capabilities
--cidfile="", after running the container, write the container PID value in the specified file, a typical monitoring system usage
--cpuset="", set which CPUs the container can use, this parameter can be used by the container to monopolize the CPU
--device=[], add the host device to the container, which is equivalent to device pass-through
--dns=[], specify the dns server of the container
--dns-search=[], specify the dns search domain name of the container, and write it to the container /etc/resolv.conf file
--entrypoint="", override the entry point of the image
--env-file=[], specify the environment variable file, the file format is one environment variable per line
--expose=[], specify the container Exposed port, that is, modify the exposed port of the image
--link=[], specify the association between containers, use the IP, env and other information of other containers
--lxc-conf=[], specify the configuration file of the container, only when specified Use --name="" when --exec-driver=lxc
, specify the container name. You can later use the name to manage the container. The links feature requires the name
--net="bridge". Container network settings:
bridge is specified using the docker daemon. Bridge
host //Container uses the host's network
container:NAME_or_ID >//Uses the network of other containers, sharing network resources such as IP and PORT
none The container uses its own network (similar to --net=bridge), but does not configure it
--privileged=false, specifies whether the container is a privileged container, and the privileged container has all capabilities
--restart="no", specifies the restart strategy after the container is stopped:
no: do not restart when the container exits
on-failure: the container fails to exit ( always restart when the return value is non-zero)
: always restart when the container exits
--rm=false, specifies that the container will be automatically deleted after it stops (containers started with docker run -d are not supported)
--sig-proxy=true, set by The proxy accepts and processes signals, but SIGCHLD, SIGSTOP and SIGKILL cannot be proxied

  • View container

Syntax docker ps [OPTIONS] , view the container list

#查看运行中的容器
docker ps
#查看所有容器
docker ps -a

Parameter Description 

  • CONTAINER ID: Container ID.
  • IMAGE: The image used.
  • COMMAND: The command to run when starting the container.
  • CREATED: The creation time of the container.
  • STATUS: Container status.
    • There are 7 states:
      created (created)
      restarting (restarting)
      running (running)
      removing (migrating)
      paused (paused)
      exited (stopped)
      dead (dead)
  • PORTS: The port information of the container and the connection type used (tcp\udp).
  • NAMES: Automatically assigned container names

  • View container network port mappings

Syntax docker port CONTAINER [PRIVATE_PORT[/PROTO]]

#根据容器名称查询映射端口
docker port nervous_feynman
#根据容器id查询映射端口
docker port 48de190154d9

  • Container start/stop/restart 

语法docker start/stop/restart [OPTIONS] CONTAINER [CONTAINER...]

#根据容器id启停容器
docker stop 48de190154d9
docker start 48de190154d9
docker restart 48de190154d9

#根据容器NAMES启停容器
docker stop nervous_feynman
docker start nervous_feynman
docker restart nervous_feynman

  • Enter the container

Syntax docker exec [OPTIONS] CONTAINER COMMAND [ARG...] , you can only enter the running container

docker exec -it 48de190154d9 sh
#or
docker exec -it 48de190154d9 /bin/bash

Each container can be considered as a virtual machine, exit exits the container

  • View container logs

Syntax docker logs [OPTIONS] CONTAINER

#根据容器id查询日志
docker logs 48de190154d9
#根据容器names查看日志
docker logs nervous_feynman
  • Delete container

Syntax docker rm [OPTIONS] CONTAINER [CONTAINER...] , delete one or more containers, the parameter can be container ID or NAMES, multiple containers are separated by spaces

docker rm dfcf491e8547

Guess you like

Origin blog.csdn.net/DreamEhome/article/details/131962440