Docker common operation commands_docker commands Daquan

Hello everyone, meet again, I am your friend Quanzhanjun.

Docker common commands use

1 Container Lifecycle Management

1.1 docker run

  • The run command is used to run a new container
# docker run syntax
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
copy

OPTIONS

Description

-d

Run the container in the background and return the container ID

–name

Specify a name for the container --name container-name or --name=container-name (two - )

-p

Specify the port mapping, the format is: host (host) port: container port or externally exposed port: container (project) port

-m

Set the maximum memory used by the container

-i

Run the container in interactive mode, usually with -t

-t

reassigns a pseudo input terminal for the container, usually used with -i

1.2 docker start/stop/restart

  • Start, stop, restart containers
# grammar:
# start the container
docker start [OPTIONS] CONTAINER [CONTAINER...]
# stop container
docker stop [OPTIONS] CONTAINER [CONTAINER...]
# restart the container
docker restart [OPTIONS] CONTAINER [CONTAINER...]
copy

1.3 docker kill

  • Force kill the container
# grammar
docker kill [OPTIONS] CONTAINER [CONTAINER...]
# -s : send a signal to the container
copy

Kill the running container tomcat (specify the container name) docker kill -s KILL tomcat Kill the running container (specify the container id) docker kill -s kill 3a5b

1.4 docker pause/unpause

  • Pause and resume all container processes
# grammar
# 1. Pause all processes in the container
docker pause CONTAINER [CONTAINER...]

# 1. Resume all processes in the container
docker unpause CONTAINER [CONTAINER...]
copy

Pause database container mysql; provide service docker pause mysql Restoring the database container mysql provides services docker unpause mysql

1.5 docker exec

  • Execute commands in the running container (usually bash into the container)
# grammar
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

-d :Separate mode: run in the background

-i :keep even if not attached STDIN Open

-t :Allocate a pseudo terminal
copy

Enter mysql in docker (pseudo terminal, exit exit, does not affect normal operation)   docker exec -it mysql bash   mysql -uroot -p    Enter the password of mysql

1.6 docker rm

  • Delete one or more containers. By default, only non-running containers can be deleted.
    • The   -f parameter can be forcibly deleted (not mandatory, you need to docker stop first, then docker rm)
# grammar 
docker rm [OPTIONS] CONTAINER [CONTAINER...]

-f :pass SIGKILL The signal forces the removal of a running container.

-l :Remove network connections between containers, not the containers themselves.

-v :Delete the volume associated with the container.
copy

delete multiple containers     docker rm CONTAINER_id1 CONTAINER_id2 delete all containers     docker rm -f ($docker ps -aq)

2 Container Operations

2.1 docker ps

  • List container related information

OPTIONS

Description

-a

The parameter can view the container in all states

-s

Show total file size

-f

Filter displayed content based on criteria

–format

template file specifying return value

-l

Show recently created containers

-n

List the n most recently created containers

–no-trunc

Do not truncate output

–q

silent mode, only the container number is displayed

docker ps # default is to view, running docker container

2.2 docker inspect

  • Used to view the configuration information of the container, including container name, environment variables, running commands, host configuration, network configuration and data volume configuration, etc.
# grammar 
docker inspect [OPTIONS] CONTAINER|IMAGE|TASK

-f :template file specifying return value

-s :Show total file size

--type :return for the specified type JSON

# Get information about the container mysql
docker inspect mysql
copy

2.3 docker logs - view docker container logs

  • Used to view the log of the container, it will output the data output to standard output as a log to the terminal of the docker logs command. Commonly used for backend containers
$ docker logs [OPTIONS] CONTAINER
  Options:
        --details        show more information
    -f, --follow         Track real-time logs
        --since string   displayed from a timestamp After the log, or relative time like 42 m(i.e. 42 minutes)
        --tail string    How many log lines to display from the end of the log, default is all
    -t, --timestamps     show timestamp
        --until string   displayed from a timestamp previous log, or relative time such as 42 m(i.e. 42 minutes)
copy

Example:

# CONTAINER_ID: can be the container full id or container name
# View logs after a specified time, only show the last 100 lines
docker logs -f -t --since="2022-02-08" --tail=100 CONTAINER_ID

# View logs for the last 30 minutes:
docker logs --since 30m CONTAINER_ID

# View logs after a certain time:
docker logs -t --since="2022-02-08T13:23:37" CONTAINER_ID

# View logs for a certain period of time:
docker logs -t --since="2022-02-08T13:23:37" --until "2022-02-09T12:23:37" CONTAINER_ID
copy

2.4 docker export

  • Package the container and export it to tar file format
# grammar
docker export [OPTIONS] CONTAINER

Example: will id for 746300 e14aa4 Containers are saved by date as tar document
docker export -o mysql-`date +%Y%m%d`.tar 746300e14aa4

ls mysql-`date +%Y%m%d`.tar      >>     mysql-20211228.tar
copy

2.5 docker port

  • Output information about the port mapping between the container and the host
grammar: docker port CONTAINER [PRIVATE_PORT[/PROTO]]
Example: View Containers mysql port mapping.

docker port mysql
>>>		 3306/tcp -> 0.0.0.0:3001
copy

3 Local image management

3.1 docker images

  • List all local mirrors
grammar: docker images [OPTIONS] [REPOSITORY[:TAG]]

-a :List all local images (including intermediate image layers, by default, the intermediate image layers are filtered out);

--digests :Display the summary information of the mirror;

-f :Display images that meet the conditions;

--format :A template file specifying the return value;

--no-trunc :Display complete image information;

-q :show only mirror ID

# Displays all containers by default, (generally) equivalent to docker images -a
docker images
copy

3.2 rmi

  • delete one or more mirrors
# grammar
docker rmi [OPTIONS] [IMAGE...]

-f :forced deletion;

--no-prune :The process image of the image is not removed, it is removed by default;

docker rmi -f ($docker images -aq)delete all images
docker rmi -f mysql   delete mysql mirror

docker image rm mysql    Only one image can be deleted at a time
copy

3.3 docker build

  • Explanation of common parameters: docker build [OPTIONS] PATH | URL | –

OPTIONS

Description

-f

The name of the Dockerfile (default "PATH/Dockerfile")

-t,–tag

Specify the built image name and tag

3.4 docker save

  • Save the specified image as a tar archive
# grammar 
docker save [OPTIONS] IMAGE [IMAGE...]

Example: mirroring mysql generate mysql.tar Documentation
docker save -o mysql.tar mysql
ll mysql.tar
copy

3.5 docker load

  • Import the packaged image
# grammar
docker load [OPTIONS]

--input , -i : Specify the imported file, instead of STDIN. 

--quiet , -q : Condensed output information.


# Example
# Import a packaged msyql image
docker load < mysql.tar
docker images
copy

article Reference documentation

Copyright statement: The content of this article is contributed by Internet users, and the opinions of this article only represent the author himself. This site only provides information storage space services, does not have ownership rights, and does not assume relevant legal responsibilities. If you find any content suspected of infringing/violating laws and regulations on this site, please send an email to report. Once verified, this site will be deleted immediately.

Publisher: Full-stack programmer, please indicate the source: https://javaforall.cn/192352.html Original link: https://javaforall.cn

Tags: Database SQL Container

Posted by smitthhyy on Sat, 01 Oct 2022 17:12:03 +0930