How to clear Docker task history
You can adjust the history limit in swarm by running:
docker swarm update --task-history-limit=1
Which will only keep one previous task instead of the default 5. See the cli docs for more details: https://docs.docker.com/engine/reference/commandline/swarm_update/
how to remove all the build history of a docker image?
You can use a multistage build. This is an example for a tomcat image:
docker pull tomcat:7-jre8
docker history tomcat:7-jre8
This shows you the full history of the image.
I now create a Dockerfile like this:
FROM tomcat:7-jre8 as orig
FROM alpine:latest
COPY --from=orig / /
I build it:
docker build -t mytomcat:1.0 .
If I check the history this is what I see now:
docker history mytomcat:1.0
IMAGE CREATED CREATED BY SIZE COMMENT
c3cde992658a 6 minutes ago /bin/sh -c #(nop) COPY dir:f31f2e5f414562467… 454MB
5cb3aa00f899 6 weeks ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 6 weeks ago /bin/sh -c #(nop) ADD file:88875982b0512a9d0… 5.53MB
Test the new image:
docker run -ti --rm mytomcat:1.0 bash
root@62d8c9934bd4:/# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
root@62d8c9934bd4:/# curl http://localhost:8080
...
Hope this is what you are looking for. If not let me know.
How can I remove shutdown Docker Service tasks after a rolling update?
The containers for those services are removed after a rolling update; you are simply left with a log of those that were shutdown.
You can limit the number you see using
docker swarm update --task-history-limit 5
Docker: How to clear the logs properly for a Docker container?
First the bad answer. From this question there's a one-liner that you can run:
echo "" > $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)
instead of echo, there's the simpler:
: > $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)
or there's the truncate command:
truncate -s 0 $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)
I'm not a big fan of either of those since they modify Docker's files directly. The external log deletion could happen while docker is writing json formatted data to the file, resulting in a partial line, and breaking the ability to read any logs from the docker logs
cli. For an example of that happening, see this comment on duketwo's answer:
after emptying the logfile, I get this error:
error from daemon in stream: Error grabbing logs: invalid character '\x00' looking for beginning of value
Instead, you can have Docker automatically rotate the logs for you. This is done with additional flags to dockerd if you are using the default JSON logging driver:
dockerd ... --log-opt max-size=10m --log-opt max-file=3
You can also set this as part of your daemon.json file instead of modifying your startup scripts:
{
"log-driver": "json-file",
"log-opts": {"max-size": "10m", "max-file": "3"}
}
These options need to be configured with root access. Make sure to run a systemctl reload docker
after changing this file to have the settings applied. This setting will then be the default for any newly created containers. Note, existing containers need to be deleted and recreated to receive the new log limits.
Similar log options can be passed to individual containers to override these defaults, allowing you to save more or fewer logs on individual containers. From docker run
this looks like:
docker run --log-driver json-file --log-opt max-size=10m --log-opt max-file=3 ...
or in a compose file:
version: '3.7'
services:
app:
image: ...
logging:
options:
max-size: "10m"
max-file: "3"
For additional space savings, you can switch from the json log driver to the "local" log driver. It takes the same max-size and max-file options, but instead of storing in json it uses a binary syntax that is faster and smaller. This allows you to store more logs in the same sized file. The daemon.json entry for that looks like:
{
"log-driver": "local",
"log-opts": {"max-size": "10m", "max-file": "3"}
}
The downside of the local driver is external log parsers/forwarders that depended on direct access to the json logs will no longer work. So if you use a tool like filebeat to send to Elastic, or Splunk's universal forwarder, I'd avoid the "local" driver.
I've got a bit more on this in my Tips and Tricks presentation.
How to remove old Docker containers
Since Docker 1.13.x you can use Docker container prune:
docker container prune
This will remove all stopped containers and should work on all platforms the same way.
There is also a Docker system prune:
docker system prune
which will clean up all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes, in one command.
For older Docker versions, you can string Docker commands together with other Unix commands to get what you need. Here is an example on how to clean up old containers that are weeks old:
$ docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm
To give credit, where it is due, this example is from https://twitter.com/jpetazzo/status/347431091415703552.
DC/OS: remove history for a specific service ID
As far as I know this is not possible. The setting is the same for all services.
Docker Swarm: How to remove stopped containers
There's a option --task-history-limit
.
So you can do something like this:
docker swarm update --task-history-limit=1
Take a look at the cli docs: https://docs.docker.com/engine/reference/commandline/swarm_update/
How do I remove old service images after an update?
These are containers, not images. In docker, there's a rather significant difference between the two (images are the definition used to create a container). Inside of a swarm service, they are referred to as tasks. To adjust how many docker keeps by default, you can change the global threshold with:
docker swarm update --task-history-limit 1
The default value for this is 5.
To remove individual containers, you can remove the container from the host where it's running with:
docker container ls -a | grep picday
docker container rm <container id>
Related Topics
How to Implement Getthreadcontext in Linux/Unix
In Linux, How to Do System Calls Through Gnu Arm Assembly
How to Change Qmake Prefix Location
Will Java Compiled in Windows Work in Linux
Where Is Linux Cfs Scheduler Code
Getting Uptime or Downtime of Systemd Process Using Systemctl
How to Test If a Directory Is Empty with Find
Better Way to Check Linux Version
Why Disable One Local Interrupt or Preemption Can Cause The Whole System with 4 Cpus Unresponsive
How to Measure Net Used Disk Space Change Due to Activity by a Given Process in Linux
Mongodb (Result= Signal, Code = Killed, Signal = Ill
Nvcc Cuda Cross Compiling Cannot Find "-Lcudart"
Echo - Syntax Error: Bad Substitution
How to Install and Run Tacotron2 on Ubuntu Wsl
Linux - Change The Hostname in The Cli
How to Compile Redis So That I Can Upload and Run It on Shared Hosting