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...]




Run the container in the background and return the container ID


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


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


Set the maximum memory used by the container


Run the container in interactive mode, usually with -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
# stop container
# restart the container

1.3 docker kill

  • Force kill the container
# grammar
# -s : send a signal to the container

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...]

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

-d :Separate mode: run in the background

-i :keep even if not attached STDIN Open

-t :Allocate a pseudo terminal

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 

-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.

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




The parameter can view the container in all states


Show total file size


Filter displayed content based on criteria


template file specifying return value


Show recently created containers


List the n most recently created containers


Do not truncate output


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 

-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

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
        --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)


# 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

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

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 ->

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

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

3.3 docker build

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




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


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

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

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: Original link:

Tags: Database SQL Container

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