Basic usage of docker

Keywords: Linux Operation & Maintenance Docker Container

1, What is docker

Containers in docker:

  • lxc --> libcontainer --> runC

2, OCI & OCF

OCI

Open Container-initiative

  • Led by the Linux foundation, it was founded in June 2015
  • It aims to develop an open industrial standard around container format and runtime
  • contains two specifications
    • the Runtime Specification(runtime-spec)
    • the Image Specification(image-spec)

OCF

Open Container Format

runC is a CLI tool for spawning and running containers according to the OCI specification

  • Containers are started as a child process of runC and can be embedded into various other systems without having to run a daemon
  • runC is built on libcontainer, the same container technology powering millions of Docker Engine installations

docker provides a site dedicated to container images: https://hub.docker.com

3, docker architecture

4, docker image and image warehouse

Why is the name of the image repository Registry instead of repository? In docker, the name of the warehouse is named after the name of the application.

The image is static, while the container is dynamic. The container has its life cycle. The relationship between the image and the container is similar to that between the program and the process. The image is similar to the program file in the file system, while the container is similar to the state in which a program runs, that is, the process. Therefore, containers can be deleted. After a container is deleted, its image will not be deleted.

5, docker object

When you use docker, you are creating and using images, containers, networks, volumes, pluginns, and other objects.

  • IMAGES
    • An image is a read-only template with instructions for creating a docker container.
    • Often, an image is based on another image, with some additional customization.
    • You might create your own images or you might only use those created by others and published in a registry.
  • CONTAINERS
    • A conntainer is a runnable instance of an image.
    • You can create, run, stop, move, or delete a container using the docker API or CLI.
    • You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.

6, Installing and using docker

docker installation

[root@localhost ~]# cd /etc/yum.repos.d/

[root@localhost yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo

[root@localhost yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo

[root@localhost yum.repos.d]# yum -y install docker-ce

[root@localhost yum.repos.d]# yum clean all
Failed to set locale, defaulting to C.UTF-8
28 files removed

// Start service
[root@localhost ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@localhost ~]# ls /etc/docker/
key.json

docker acceleration

The configuration file of docker CE is / etc/docker/daemon.json, which does not exist by default. We need to create and configure it manually, and the acceleration of docker is realized by configuring this file.

docker can be accelerated in many ways:

  • docker cn
  • Accelerator of China University of science and technology
  • Alicloud accelerator (you need to register an account through alicloud Developer Platform and use your own accelerator for free)
[root@Docker ~]# systemctl enable --now docker / / start the docker service
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

// Add accelerator
cat > /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}
EOF

// restart
[root@local ~]# systemctl dameon-reload docker
[root@local ~]# systemctl restart docker

docker common operations

commandfunction
docker searchSearch the Docker Hub for images
docker pullPull an image or a repository from a registry
docker imagesList images
docker createCreate a new conntainer
docker startStart one or more stopped containers
docker runRun a command in a new container
docker attachAttach to a runninng container
docker psList containers
docker logsFetch the logs of a container
docker restartRestart a container
docker stopStop one or more running containers
docker killKill one or more running containers
docker rmRemove onne or more containers
docker execRun a command in a running container
docker infoDisplay system-wide information
docker inspectReturn low-level information on Docker objects

docker version: View version

[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.11
 API version:       1.41
 Go version:        go1.16.9
 Git commit:        dea9396
 Built:             Thu Nov 18 00:36:58 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.11
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.9
  Git commit:       847da18
  Built:            Thu Nov 18 00:35:20 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.12
  GitCommit:        7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

docker info: view system information

[root@localhost ~]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)
  scan: Docker Scan (Docker Inc., v0.9.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 1
 Server Version: 20.10.11
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc version: v1.0.2-0-g52b36a2
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.18.0-257.el8.x86_64
 Operating System: CentOS Stream 8
 OSType: linux
 Architecture: x86_64
 CPUs: 8
 Total Memory: 3.622GiB
 Name: localhost.localdomain
 ID: FT3B:RKJB:SBUC:OAL7:NW2C:QBTY:6VIW:PWHU:CWBD:PQPU:D424:BZJU
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

docker pull: pull the image

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
eff15d958d66: Pull complete 
1e5351450a59: Pull complete 
2df63e6ce2be: Pull complete 
9171c7ae368c: Pull complete 
020f975acd28: Pull complete 
266f639b35ad: Pull complete 
Digest: sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

docker images: lists all current images of the system

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    ea335eea17ab   13 days ago   141MB

docker search: find the image in the docker hub

[root@localhost ~]# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        15893     [OK]       
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con...   2098                 [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of...   819                  [OK]
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho...   285                  
linuxserver/nginx                 An Nginx container, brought to you by LinuxS...   160                  
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp...   146                  [OK]
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        144                  [OK]
alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou...   110                  [OK]
jasonrivers/nginx-rtmp            Docker images to host RTMP streams using NGI...   95                   [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co...   79                   [OK]
privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al...   60                   [OK]
nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo...   57                   
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  54                   
nginxproxy/nginx-proxy            Automated Nginx reverse proxy for docker con...   28                   
staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo...   25                   [OK]
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN...   22                   
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra...   19                   [OK]
centos/nginx-112-centos7          Platform for running nginx 1.12 or building ...   16                   
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n...   13                   
bitwarden/nginx                   The Bitwarden nginx web server acting as a r...   11                   
mailu/nginx                       Mailu nginx frontend                            9                    [OK]
sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a...   7                    [OK]
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          3                    [OK]
wodby/nginx                       Generic nginx                                   1                    [OK]
arnau/nginx-gate                  Docker image with Nginx with Lua enabled on ...   1                    [OK]

docker image history: view the history of the specified image

[root@localhost ~]# docker image history nginx
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
ea335eea17ab   13 days ago   /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon...   0B        
<missing>      13 days ago   /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B        
<missing>      13 days ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
<missing>      13 days ago   /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr...   0B        
<missing>      13 days ago   /bin/sh -c #(nop) COPY file:09a214a3e07c919a...   4.61kB    
<missing>      13 days ago   /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7...   1.04kB    
<missing>      13 days ago   /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0...   1.96kB    
<missing>      13 days ago   /bin/sh -c #(nop) COPY file:65504f71f5855ca0...   1.2kB     
<missing>      13 days ago   /bin/sh -c set -x     && addgroup --system -...   61.1MB    
<missing>      13 days ago   /bin/sh -c #(nop)  ENV PKG_RELEASE=1~bullseye   0B        
<missing>      13 days ago   /bin/sh -c #(nop)  ENV NJS_VERSION=0.7.0        0B        
<missing>      13 days ago   /bin/sh -c #(nop)  ENV NGINX_VERSION=1.21.4     0B        
<missing>      13 days ago   /bin/sh -c #(nop)  LABEL maintainer=NGINX Do...   0B        
<missing>      2 weeks ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      2 weeks ago   /bin/sh -c #(nop) ADD file:a2405ebb9892d98be...   80.4MB    

docker image inspect: view the details of the specified image

[root@localhost ~]# docker inspect nginx
[
    {
        "Id": "sha256:ea335eea17ab984571cd4a3bcf90a0413773b559c75ef4cda07d0ce952b00291",
        "RepoTags": [
            "nginx:latest"
        ],
        "RepoDigests": [
            "nginx@sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-11-17T10:38:14.652464384Z",
        "Container": "8a038ff17987cf87d4b7d7e2c80cb83bd2474d66e2dd0719e2b4f7de2ad6d853",
        "ContainerConfig": {
            "Hostname": "8a038ff17987",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.21.4",
                "NJS_VERSION=0.7.0",
                "PKG_RELEASE=1~bullseye"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"nginx\" \"-g\" \"daemon off;\"]"
            ],
            "Image": "sha256:2fb4060b053a39040c51ff7eadd30325de2c76650fc50aa42839070e16e8bdcb",
.....Omitted here

docker create: creates a container (but the container will not start)

[root@localhost ~]# docker create httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
eff15d958d66: Already exists 
ba1caf8ba86c: Pull complete 
ab86dc02235d: Pull complete 
0d58b11d2867: Pull complete 
e88da7cb925c: Pull complete 
Digest: sha256:1d71eef54c08435c0be99877c408637f03112dc9f929fba3cccdd15896099b02
Status: Downloaded newer image for httpd:latest
8877c16c9c19353c6eb74a93827ed9914fbeb503067d6cc7f1f1e68e4f3ce203

docker rm: delete image

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
httpd        latest    ad17c88403e2   12 days ago   143MB
nginx        latest    ea335eea17ab   13 days ago   141MB

[root@localhost ~]# docker rmi ad17c88403e2
Untagged: httpd:latest
Untagged: httpd@sha256:1d71eef54c08435c0be99877c408637f03112dc9f929fba3cccdd15896099b02
Deleted: sha256:ad17c88403e2cedd27963b98be7f04bd3f903dfa7490586de397d0404424936d
Deleted: sha256:a59e7dfeeb485a8a45b1fcce812b10fbd955d304fa2e9ca43b10b16a8ee1afb8
Deleted: sha256:9592080464aa1890ed187c42a13ecc9f175e975a96a3fad28df0559ad0c08b9d
Deleted: sha256:42d2debfa0c419f7f89affa3e9b62d1b7e54dc6654dbd186d4654ee3661c44c8
Deleted: sha256:136822c50a75392f4ce06461fa4894aa7d1e060ec0dd4782e13e2d9829df50a3
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    ea335eea17ab   13 days ago   141MB

docker ps: List containers

[root@localhost ~]# docker ps 	//  View all running containers
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@localhost ~]# docker ps -a 	//  View all containers (running and non running)
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS    PORTS     NAMES
8877c16c9c19   httpd     "httpd-foreground"   21 seconds ago   Created             infallible_nobel

docker start: starts the container

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS    PORTS     NAMES
8877c16c9c19   httpd     "httpd-foreground"   About a minute ago   Created             infallible_nobel

[root@localhost ~]# docker start 8877c16c9c19
8877c16c9c19

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS         PORTS     NAMES
8877c16c9c19   httpd     "httpd-foreground"   About a minute ago   Up 4 seconds   80/tcp    infallible_nobel

docker attach: enter container

In the current shell, the attach connection specifies to run the image. If you enter the container in this way, the container will always occupy the foreground. If you exit the container, the container stops running

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS          PORTS     NAMES
8877c16c9c19   httpd     "httpd-foreground"   2 minutes ago   Up 56 seconds   80/tcp    infallible_nobel

[root@localhost ~]# docker attach 8877c16c9c19

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

docker exec: enter container

This command runs in the background of the container. Even if you exit the container, the container will not stop running

// Start container
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS                          PORTS     NAMES
8877c16c9c19   httpd     "httpd-foreground"   4 minutes ago   Exited (0) About a minute ago             infallible_nobel

[root@localhost ~]# docker start 8877c16c9c19
8877c16c9c19

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS     NAMES
8877c16c9c19   httpd     "httpd-foreground"   4 minutes ago   Up 3 seconds   80/tcp    infallible_nobel

// Enter container
[root@localhost ~]# docker exec -it 8877c16c9c19 /bin/bash
root@8877c16c9c19:/usr/local/apache2# ls
bin    cgi-bin  error   icons    logs
build  conf     htdocs  include  modules
root@8877c16c9c19:/usr/local/apache2# exit
exit

// View container startup
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS          PORTS     NAMES
8877c16c9c19   httpd     "httpd-foreground"   5 minutes ago   Up 46 seconds   80/tcp    infallible_nobel

docker inspect: View container details

[root@localhost ~]# docker inspect 8877c16c9c19
[
    {
        "Id": "8877c16c9c19353c6eb74a93827ed9914fbeb503067d6cc7f1f1e68e4f3ce203",
        "Created": "2021-12-01T10:06:47.836823243Z",
        "Path": "httpd-foreground",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 50995,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-12-01T10:11:23.288419586Z",
            "FinishedAt": "2021-12-01T10:10:05.442221306Z"
        },
        "Image": 
......Omitted later

docker logs: View container log information

// Create container
[root@localhost ~]# docker create nginx
baac0f280b0a2581ad5c3be233841f0d985d37ca3ecb7c3be5008bca941c9979

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
baac0f280b0a   nginx     "/docker-entrypoint...."   5 seconds ago   Created                  sleepy_mccarthy
8877c16c9c19   httpd     "httpd-foreground"       9 minutes ago   Up 4 minutes   80/tcp    infallible_nobel

// The container is not running. Unable to view log information
[root@localhost ~]# docker logs baac0f280b0a 

// Start container
[root@localhost ~]# docker start baac0f280b0a 
baac0f280b0a

// View log information
[root@localhost ~]# docker logs baac0f280b0a 
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/12/01 10:16:51 [notice] 1#1: using the "epoll" event method
2021/12/01 10:16:51 [notice] 1#1: nginx/1.21.4
2021/12/01 10:16:51 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2021/12/01 10:16:51 [notice] 1#1: OS: Linux 4.18.0-257.el8.x86_64
2021/12/01 10:16:51 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/01 10:16:51 [notice] 1#1: start worker processes
2021/12/01 10:16:51 [notice] 1#1: start worker process 31
2021/12/01 10:16:51 [notice] 1#1: start worker process 32
2021/12/01 10:16:51 [notice] 1#1: start worker process 33
2021/12/01 10:16:51 [notice] 1#1: start worker process 34
2021/12/01 10:16:51 [notice] 1#1: start worker process 35
2021/12/01 10:16:51 [notice] 1#1: start worker process 36
2021/12/01 10:16:51 [notice] 1#1: start worker process 37
2021/12/01 10:16:51 [notice] 1#1: start worker process 38

docker stop: stops the container

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS     NAMES
baac0f280b0a   nginx     "/docker-entrypoint...."   About a minute ago   Up About a minute   80/tcp    sleepy_mccarthy
8877c16c9c19   httpd     "httpd-foreground"       11 minutes ago       Up 6 minutes        80/tcp    infallible_nobel

[root@localhost ~]# docker stop baac0f280b0a
baac0f280b0a

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS         PORTS     NAMES
8877c16c9c19   httpd     "httpd-foreground"   11 minutes ago   Up 6 minutes   80/tcp    infallible_nobel

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                     PORTS     NAMES
baac0f280b0a   nginx     "/docker-entrypoint...."   2 minutes ago    Exited (0) 5 seconds ago             sleepy_mccarthy
8877c16c9c19   httpd     "httpd-foreground"       11 minutes ago   Up 7 minutes               80/tcp    infallible_nobel

docker restart: restart the container

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                          PORTS     NAMES
baac0f280b0a   nginx     "/docker-entrypoint...."   3 minutes ago    Exited (0) About a minute ago             sleepy_mccarthy
8877c16c9c19   httpd     "httpd-foreground"       12 minutes ago   Up 8 minutes                    80/tcp    infallible_nobel

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS         PORTS     NAMES
8877c16c9c19   httpd     "httpd-foreground"   12 minutes ago   Up 8 minutes   80/tcp    infallible_nobel

[root@localhost ~]# docker restart baac0f280b0a
baac0f280b0a
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS     NAMES
baac0f280b0a   nginx     "/docker-entrypoint...."   3 minutes ago    Up 2 seconds   80/tcp    sleepy_mccarthy
8877c16c9c19   httpd     "httpd-foreground"       12 minutes ago   Up 8 minutes   80/tcp    infallible_nobel

docker kill: kill the running container

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
baac0f280b0a   nginx     "/docker-entrypoint...."   4 minutes ago    Up 40 seconds   80/tcp    sleepy_mccarthy
8877c16c9c19   httpd     "httpd-foreground"       13 minutes ago   Up 8 minutes    80/tcp    infallible_nobel

[root@localhost ~]# docker kill 8877c16c9c19
8877c16c9c19

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS          PORTS     NAMES
baac0f280b0a   nginx     "/docker-entrypoint...."   4 minutes ago   Up 55 seconds   80/tcp    sleepy_mccarthy

docker run: create a container and start the container directly

The run command will first check whether the local image corresponds to the image. If not, pull it, then create a container, and then run the container

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
e2ae1f194bd8   httpd     "/bin/bash"   37 minutes ago   Exited (0) 36 minutes ago             hungry_faraday

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
httpd        latest    ad17c88403e2   12 days ago   143MB

[root@localhost ~]# docker run -it nginx /bin/bash
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
eff15d958d66: Already exists 
1e5351450a59: Pull complete 
2df63e6ce2be: Pull complete 
9171c7ae368c: Pull complete 
020f975acd28: Pull complete 
266f639b35ad: Pull complete 
Digest: sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e
Status: Downloaded newer image for nginx:latest
root@421bdeaf3878:/#

docker rm: delete container

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
baac0f280b0a   nginx     "/docker-entrypoint...."   7 minutes ago   Up 4 minutes   80/tcp    sleepy_mccarthy


// Cannot delete a running container
[root@localhost ~]# docker rm baac0f280b0a
Error response from daemon: You cannot remove a running container baac0f280b0a2581ad5c3be233841f0d985d37ca3ecb7c3be5008bca941c9979. Stop the container before attempting removal or force remove

// Add - f to force the deletion of running containers
[root@localhost ~]# docker rm -f baac0f280b0a
baac0f280b0a

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

docker event state

Posted by alsouno on Wed, 01 Dec 2021 07:27:54 -0800