Basic Docker Commands
Image Related Commandsβ
# Download image
docker pull xxx
# Search image
docker search xxx
# View downloaded images
docker images
# View ids of downloaded images
docker images -q
# Delete image
docker rmi containerId/SOURCE_IMAGE[:TAG]
# Delete all images
docker rmi $(docker images -q)
docker images -q | xargs docker rmi
# Delete images with tag <none>
docker rmi | xargs docker images | awk '{if ($2 == "<none>" )print $3}' | xargs docker rmi -f
Container Related Commandsβ
# Load a local image
docker load -i tomcat-8.0-jre.tar
# Start a container
docker run -dp 8080:8080 tomcat-8.0-jre --name tomcat
docker run -p 8887:8887 -it --name myideac jetbrains/projector-idea-c
docker start myideac
# Enter interactive mode
docker run -it debian /bin/bash
# View running containers
docker ps
# View ids of all containers
docker ps -q
# View all containers
docker ps -a
# run / restart / stop / kill container
docker start/restart/stop/kill containerId/name
# Delete created container
docker rm containerId/name:tag
docker rm $(docker ps -q)
# Stop container
docker stop $(docker ps -a | grep "Exited" | awk '{print $1 }')
# Delete container
docker rm $(docker ps -a | grep "Exited" | awk '{print $1 }')
# Delete image
docker rmi $(docker images | grep "none" | awk '{print $3}')
# View logs
docker logs containerId/name
# Real-time log display
docker logs -tf containerId/name
# Log with time at head of line
docker logs -tail 5 containerId/name
# View container resources
docker top containerId
# Enter container interactive mode
docker exec -it containerId bash
# Copy files, directories from container to host
docker cp containerId:/root/test.txt /root/
# Copy files, directories from host to container
docker cp a.txt containerId:/root/
# Commit an image
docker commit -m "description xx" -a "author" containerId SOURCE_IMAGE[:TAG]
docker save redis:latest -o redis-cluster.tar
# Upload to docker hub repository
docker tag redis:latest docker.io/whalefall541/redis:0.01
docker push docker.io/whalefall541/redis:0.01
# View docker network configuration
docker network ls
# Create custom bridge
docker mynetwork create mynetwork
# Assign container to created bridge
docker run -dp 80:80 --network mynetwork redis
# Delete bridge
docker newnetwork rm mynetwork
# View bridge
docker inpsect mynetwork
# Volume assignment
# docker run -dp 80:80 tomcat -v /root/webapps:/root/webapps
docker run -dp 80:80 tomcat -v aa:/root/webapps
# Volume creation
docker volume create volume_1
# Remove all unused volumes
docker volume prune
docker rm volume_1
# View volume
docker inspect volume_1
# Restart docker
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl status docker
Dockerfileβ
- ADD
- COPY
- ENV
- EXPOSE
- FROM
- LABEL
- STOPSIGNAL
- USER
- VOLUME
- WORKDIR
- ONBUILD (when combined with one of the supported instructions above)
For detailed tutorial please see official website Dockerfile
how-cmd-and-entrypoint-interact
Understand how CMD and ENTRYPOINT interactβ
Both CMD and ENTRYPOINT instructions define what command gets executed when running a container.
There are few rules that describe their co-operation.
-
Dockerfile should specify at least one of
CMDorENTRYPOINTcommands. -
ENTRYPOINTshould be defined when using the container as an executable. -
CMDshould be used as a way of defining default arguments for anENTRYPOINTcommand or for executing an ad-hoc command in a container. -
CMDwill be overridden when running the container with alternative arguments.
The table below shows what command is executed for different ENTRYPOINT / CMD combinations:
| No ENTRYPOINT | ENTRYPOINT exec_entry p1_entry | ENTRYPOINT ["exec_entry", "p1_entry"] | |
|---|---|---|---|
| No CMD | error, not allowed | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry |
| CMD ["exec_cmd", "p1_cmd"] | exec_cmd p1_cmd | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry exec_cmd p1_cmd |
| CMD ["p1_cmd", "p2_cmd"] | p1_cmd p2_cmd | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry p1_cmd p2_cmd |
| CMD exec_cmd p1_cmd | /bin/sh -c exec_cmd p1_cmd | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd |
Note
If
CMDis defined from the base image, settingENTRYPOINTwill resetCMDto an empty value. In this scenario,CMDmust be defined in the current image to have a value.You can override the ENTRYPOINT setting using --entrypoint, but this can only set the binary to exec (no sh -c will be used).
A Simple Caseβ
Copy springboot file to container and specify spring configuration to run
FROM java
ENV JARNAME="demo-0.0.1-SNAPSHOT.jar"
WORKDIR /root/app
RUN /bin/bash -c "echo 'aaaa' >> 1.txt"
EXPOSE 80
VOLUME ["/root/app"]
ENTRYPOINT ["java","-jar","demo-0.0.1-SNAPSHOT.jar", "-Dspring.config.location"]
CMD ["application.yml"]
#ENTRYPOINT ["echo","/root"]
#CMD ["/root/app"]
docker build -t springboot:0.01 .
docker run -v /dockerlearn/dockerfilelearn:/root/app -p 80:80 --network rediscluster_default --rm springboot:0.01
docker run -p 8080:800 --rm learncase:latest
Note
- If
ENTRYPOINT ["exec_entry", "p1_entry"]is followed byCMD ["p1_cmd", "p2_cmd"]then parameters in CMD will all be appended to ENTRYPOINT Whendocker runis followed by parameters, it will overwrite parameters in CMD- CMD ENTRYPOINT array format are both exec mode, do not support parsing local variables If want to parse use this command
ENTRYPOINT ["sh","-c","java -jar $JARNAME"]- If used
ENTRYPOINT ["sh","-c",..]or directly usedENTRYPOINT "echo Hello world"Then simply cannot overwrite parameters
References
- Attribution: Retain the original author's signature and code source information in the original and derivative code.
- Preserve License: Retain the Apache 2.0 license file in the original and derivative code.
- Attribution: Give appropriate credit, provide a link to the license, and indicate if changes were made.
- NonCommercial: You may not use the material for commercial purposes. For commercial use, please contact the author.
- ShareAlike: If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.