Skip to main content

Basic Docker 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
# 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.

  1. Dockerfile should specify at least one of CMD or ENTRYPOINT commands.

  2. ENTRYPOINT should be defined when using the container as an executable.

  3. CMD should be used as a way of defining default arguments for an ENTRYPOINT command or for executing an ad-hoc command in a container.

  4. CMD will be overridden when running the container with alternative arguments.

The table below shows what command is executed for different ENTRYPOINT / CMD combinations:

No ENTRYPOINTENTRYPOINT exec_entry p1_entryENTRYPOINT ["exec_entry", "p1_entry"]
No CMDerror, not allowed/bin/sh -c exec_entry p1_entryexec_entry p1_entry
CMD ["exec_cmd", "p1_cmd"]exec_cmd p1_cmd/bin/sh -c exec_entry p1_entryexec_entry p1_entry exec_cmd p1_cmd
CMD ["p1_cmd", "p2_cmd"]p1_cmd p2_cmd/bin/sh -c exec_entry p1_entryexec_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_entryexec_entry p1_entry /bin/sh -c exec_cmd p1_cmd

Note

If CMD is defined from the base image, setting ENTRYPOINT will reset CMD to an empty value. In this scenario, CMD must 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

  1. If ENTRYPOINT ["exec_entry", "p1_entry"] is followed by CMD ["p1_cmd", "p2_cmd"] then parameters in CMD will all be appended to ENTRYPOINT When docker run is followed by parameters, it will overwrite parameters in CMD
  2. 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"]
  3. If used ENTRYPOINT ["sh","-c",..] or directly used ENTRYPOINT "echo Hello world" Then simply cannot overwrite parameters

References

  1. Dockerfile
  2. how-cmd-and-entrypoint-interact
Agreement
The code part of this work is licensed under Apache License 2.0 . You may freely modify and redistribute the code, and use it for commercial purposes, provided that you comply with the license. However, you are required to:
  • 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.
The documentation part of this work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License . You may freely share, including copying and distributing this work in any medium or format, and freely adapt, remix, transform, and build upon the material. However, you are required to:
  • 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.