Docker basic command

1. View Docker information (version, info)

$docker version $docker version $docker version $docker version $docker version

2. Operation of image (search, pull, images, RMI, history)

# image $docker pull image_name # image $docker pull image_name # # -a, --all=false Show all images; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs $docker images # delete one or more images; # -f, --force=false Force; --no-prune=false Do not delete untagged parents $docker rmi image_name # # --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs $docker history image_name

3. Start the container (run)

A Docker container can be thought of as a process running in a sandbox. This sandbox contains the resources necessary for the process to run, including the file system, system class libraries, shell environment, and so on. But the sandbox doesn’t run any programs by default. You need to run a process in the sandbox to start a container. This process is the only process in the container, so when the process ends, the container stops completely.

# run echo in the container $docker run-i-t image_name /bin/bash $docker run-i-t image_name /bin/bash $docker run-i-t image_name /bin/bash $docker run-i-t image_name /bin/bash $docker run image_name apt-get install -y app_name

4. View the container (ps)

Container $docker ps-a container $docker ps-a container $docker ps-a container $docker ps-l

5. Save the Container Commit

When you make a change to a container (by running a command in the container), you can save the change to the container so that it can be run from the saved state the next time.

# Save changes to the container; -a, --author="" Author; -m, --message="" Commit message $docker commit ID new_image_name

Note: An image is a class, and a container is an instance. You can dynamically install new software on an instance, and then solidify the container into an image with the commit command.

6. Container operations (rm, stop, start, kill, logs, diff, top, cp, restart, attach)

$docker rm 'docker ps-a-q' $docker rm 'docker ps-a-q' $docker rm 'docker ps-a-q' $docker rm 'docker ps-a-q' -f, --force=false; -l, --link=false Remove the specified link and not the underlying container; -v, -- Volumes =false Remove the volume associated to the container $docker rm Name/ID # stop stop Name/ID $docker $docker start Name/ID $docker kill Name/ID $docker start Name/ID $docker kill Name/ID $docker logs Name/ID # --since = $docker logs Name/ID # --since = $docker logs Name/ID -t: -- check the log generated date --tail=10: check the last 10 logs, you can set it yourself. # List the contents of A container that have been changed. List the contents of A container that have been changed. $docker :/container_path $docker :/container_path $docker :/container_path $docker :/container_path $docker :/container_path To_path $docker cp ID:/container_path to_path # Restart a running container; # -t, --time=10 Number of seconds to try to stop for before killing the container, Default=10 $docker restart Name/ID # Attach to a running container; # --no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process $docker attach ID

Note: The attach command allows you to view or influence a running container. You can attach the same container at the same time. You can also detach from a container, from Ctrl-C.

7. Save and load images

When you need to migrate an image from one machine to another, you need to save the image and load the image.

Save the image to a tar package; -o, --output=" Write to a file $docker save image_name -o file_path -i, --input=" Read from a tar archive file $docker load-i file_path # a $docker save image_name > /home/save.tar # Use SCP to copy save.tar to machine B, then: $docker load < /home/save.tar

Example:

docker save 00ead811e8ae -o ./portainer.tar

docker load -i fastdfs.tar

8. Login Registry Server (Login)

# Log into Registry Server; -e, --email="" Email; -p, --password="" Password; -u, --username="" Username $docker login

9. Publish image (push)

$docker push new_image_name $docker push new_image_name

10. Build a container from Dockerfile

# build # --no-cache=false Do not use cache when building the image # -q, --quiet=false Suppress the verbose output generated by the containers # --rm=true Remove intermediate containers after a  successful build # -t, --tag="" Repository name (and optionally a tag) to be applied to the resulting image in case of success $docker build -t  image_name Dockerfile_path

11. Change the image name

docker tag server:latest myname/server:latest 
or
docker tag d583c3ac45fd myname/server:latest

Commonly used commands

Gets the container/image metadata.

Docker inspect ID

Go inside the Docker container

docker exec -it ID sh


When you want to exit the container, you can exit the terminal with the exit command or Ctrl+ D, and the container you created terminates immediately.

Fetching logs from a container

$docker logs Name/ID

–since specifies the start date of the output log, that is, only logs after the specified date are printed


-f, — view the live log;


-t, — Check the date the log was generated


–tail=10 –tail=10 –tail=10 –tail=10

Package the image according to the container

The format of the command to package the image:

Docker commit -m “remarks” -a “author name” container ID container name

Example:

Docker commit -m "111" -a "Li Jie" 141f15df05ff test