Docker basic commands

1. View Docker information (version, info)

$docker version $docker version $Docker infoCopy the code

2. Operations on images (search, pull, images, RMI, history)

Image $docker pull image $docker pull image $docker pull 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_nameCopy the code

3. Start the container (run)

A Docker container can be understood as a process running in a sandbox. This sandbox contains the resources necessary for the process to run, including the file system, the system class library, the shell environment, and so on. But this 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 this process ends, the container stops completely.

Run the "echo" command in the container, $docker run image_name echo "hello word" $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_nameCopy the code

4. View the container (ps)

# List all currently running Container $docker ps # List all currently running Container $docker ps -a # List the last launched Container $docker ps -lCopy the code

5. Save the changes to the container (commit)

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

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

Note: Image is a class and container is an instance. However, you can dynamically install new software on the instance and then fix the container into an image using the commit command.

6. Operations on containers (rm, stop, start, kill, logs, diff, top, cp, restart, attach)

$docker rm 'docker ps-a -q' -f, --force=false; -l, --link=false Remove the specified link and not the underlying container; -v, $docker stop Name/ID $docker stop Name/ID $docker start Name/ID $docker kill Name/ID $docker logs Name/ID # --since: specifies the start date of output logs, i.e. -t: -- View the date when a log is generated. --tail=10: view the last 10 logs. You can set this parameter by yourself. List all the files or directories in A container that have been changed. $docker diff Name:/container_path $docker diff Name:/container_path $docker diff Name:/container_path To_path $docker cp ID:/container_path to_path # # -t, --time=10 Number of seconds to try to stop for before killing the container, Default=10 $docker restart Name/ID # # --no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process $docker attach IDCopy the code

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 get out of 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 and load the image.

# Save the image to a tar file; -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 # machine a $docker save image_name > /home/save.tar # $docker load < /home/save. Tar = $docker load < /home/save. Tar = $docker load < /home/saveCopy the code

Example:

docker save 00ead811e8ae -o ./portainer.tar

docker load -i fastdfs.tar

8. Login to the registry server (login)

# Registry server; -e, --email="" Email; -p, --password="" Password; -u, --username="" Username $docker loginCopy the code

9. Publish image (push)

$docker push new_image_nameCopy the code

10. Build a container based on 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_pathCopy the code

11. Change the mirror name

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

Copy the code

Common Commands

Get metadata for the container/image.

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 by using the exit command or Ctrl+ D, and the created container terminates immediately.

Fetching logs from a container;

$docker logs Name/ID

–since: specifies the start date of output logs. That is, only logs after the specified date are output

-f, — View real-time logs. -t, — View the date when logs are generated –tail=10, view the last 10 logs. You can set this parameter by yourself.

Package the image according to the container

To package an image, run the following command:

Docker commit -m “Remarks” -a “Author name” Container ID Container name

Example:

Docker commit -m "111" -a "li jie" 141f15df05ff testCopy the code