Docker is an open platform for developing, delivering, and running applications. Docker enables you to separate applications from infrastructure so that you can deliver software quickly. With Docker, you can manage the infrastructure as if it were an application. By taking advantage of Docker’s rapid approach to delivering, testing, and deploying code, you can significantly reduce the latency between writing code and running it in production. In this article, I will mention the Docker commands we need or most use cases.

Life cycle command

Create a container (without starting it)

docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

#Create a container using the Docker image nginx:latest and name the container mynginx
#docker create --name mynginx nginx:latest    
Copy the code

Create a new container and run a command

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

#Start a container in backend mode using the Docker image nginx: Latest and name the container mynginx
# docker run --name mynginx -d nginx:latest
Copy the code

The OPTIONS:

  • -a stdin: specifies the standard input and output types. The options are stdin, STDOUT, or STDERR.
  • -d: Runs the container in the background and returns the container ID.
  • -i: Runs the container in interactive mode, usually with -t.
  • -p: indicates random port mapping. Ports inside a container are randomly mapped to ports on a host
  • -p: specifies the port mapping. The format is host (host) port: container port
  • -t: reassigns a pseudo-input terminal to the container. It is usually used together with -i.
  • –name=”nginx-lb”: specify a name for the container;
  • — DNS 8.8.8.8: Specifies the DNS server used by the container. The default DNS server is the same as the host server.
  • –dns-search example.com: specify the container DNS search domain name, which is the same as the host by default.
  • -h “Mars “: specifies the hostname of the container.
  • -e username=” Ritchie “: Set the environment variable.
  • –env-file=[]: reads environment variables from specified files;
  • –cpuset=”0-2″ or –cpuset=”0,1,2″: bind the container to the specified CPU;
  • **-m :** Sets the maximum memory used by the container.
  • – net = “bridge” : specify the container’s network connection type, support bridge/host/none/container: four types;
  • –link=[]: Add link to another container;
  • –expose=[]: Open a port or group of ports;
  • –volume, -v: Binds a volume

Rename an existing container

docker rename [CONTAINER_NAME] [NEW_CONTAINER_NAME]
Copy the code

Run the command in the new container

docker run [IMAGE] [COMMAND]
Copy the code

Remove the container after exit

docker run --rm [IMAGE]
Copy the code

Start a container and keep it running

docker run -td [IMAGE]
Copy the code

Start a container and create an interactive Bash shell within the container

docker run -it [IMAGE]
Copy the code

Create, start, and run commands inside the container, and remove the container after executing the command.

docker run -it-rm [IMAGE]
Copy the code

Execute commands in an already running container.

docker exec -it [container]
Copy the code

Delete a container (if it is not running)

docker rm [CONTAINER]
Copy the code

Update the container configuration

docker update [CONTAINER]
Copy the code

Start and stop containers

Start the container

docker start [CONTAINER]
Copy the code

Stop running the container

docker stop [CONTAINER]
Copy the code

Stop running the container and restart it

docker restart [CONTAINER]
Copy the code

Pauses a process in a running container

docker pause [CONTAINER]
Copy the code

Unsuspend a process in a running container

docker unpause [CONTAINER]
Copy the code

Block one container until the others stop

docker wait [CONTAINER]
Copy the code

Kill the container by sending SIGKILL to the running container

docker kill [CONTAINER]
Copy the code

Attach the local standard input, output, and error streams to the running container

docker attach [CONTAINER]
Copy the code

Docker image command

Create an image from a Dockerfile

docker build [URL/FILE]
Copy the code

Create an image from a Dockerfile with a label

docker build -t <tag> [URL/FILE]
Copy the code

Pull images from the registry center

docker pull [IMAGE]
Copy the code

Push the image to the registry

docker push [IMAGE]
Copy the code

Create an image from the tarball

docker import [URL/FILE]
Copy the code

Create an image from the container

docker commit [CONTAINER] [NEW_IMAGE_NAME]
Copy the code

Remove the mirror

docker rmi [IMAGE]
Copy the code

Load the image from the TAR archive or standard input

docker load [TAR_FILE/STDIN_FILE]
Copy the code

Save the image to a tar archive

docker save [IMAGE] > [TAR_FILE]
Copy the code

Docker container and image information

Lists running containers

docker ps
Copy the code

Lists running containers and stopped containers

docker ps -a
Copy the code

Lists logs in running containers

docker logs [CONTAINER]
Copy the code

Lists low-level information about Docker objects

docker inspect [OBJECT_NAME/ID]
Copy the code

Lists live events from the container

docker events [CONTAINER]
Copy the code

Displays port mappings for containers

docker port [CONTAINER]
Copy the code

Displays the running processes in the container

docker top [CONTAINER]
Copy the code

Displays real-time resource usage statistics for containers

docker stats [CONTAINER]
Copy the code

Displays changes to files (or directories) on a file system

docker diff [CONTAINER]
Copy the code

Lists all images stored locally using the Docker engine

docker [image] ls
Copy the code

Displays the history of the mirror

docker history [IMAGE]
Copy the code

The network command

List the network

docker network ls
Copy the code

Delete one or more networks

docker network rm [NETWORK]
Copy the code

Displays information about one or more networks

docker network inspect [NETWORK]
Copy the code

Connect the container to the network

docker network connect [NETWORK] [CONTAINER]
Copy the code

Disconnect the container from the network

docker network disconnect [NETWORK] [CONTAINER]
Copy the code

Container rootfs command

Copy files from the container to the host

#Docker cp container name: the path of the file to be copied in the container must be copied to the corresponding path of the host 
docker cp testtomcat:/usr/local/tomcat/webapps/test/js/test.js /root
Copy the code

Copy files from the host to the container

#Docker cp File path to be copied Container name: the path to be copied to the container
docker cp /root/test.js testtomcat:/usr/local/tomcat/webapps/test/js
Copy the code