If Docker is "ephemeral", how is stopping a container different than removing a container? When I stop and I start, that doesn't mean I can save information in that container does it? I thought that was what you didn't want to do (save data in the Container.)
Docker – Is stopping a Docker Container the same as removing a Docker Container
containersdocker
Related Solutions
Docker events
command may help and Docker logs command can fetch logs even after the image failed to start.
First start docker events
in the background to see whats going on.
docker events&
Then run your failing docker run ...
command.
Then you should see something like the following on screen:
2015-12-22T15:13:05.503402713+02:00 xxxxxxxacd8ca86df9eac5fd5466884c0b42a06293ccff0b5101b5987f5da07d: (from xxx/xxx:latest) die
Then you can get the startup hex id from previous message or the output of the run command. Then you can use it with the logs command:
docker logs <copy the instance id from docker events messages on screen>
You should now see some output from the failed image startup.
As @alexkb suggested in a comment: docker events&
can be troublesome if your container is being constantly restarted from something like AWS ECS service. In this scenario it may be easier to get the container hex id out of the logs in /var/log/ecs/ecs-agent.log.<DATE>
. Then use docker logs <hex id>
.
You got an assumption wrong about how volumes work in docker. I'll try to explain how volumes relates to docker containers and docker images and hopefully differences between data volumes and data volume containers will become clear.
First let's recall a few definitions
Docker images
Docker images are essentially a union filesystem + metadata. You can inspect the content of docker image union filesystem with the docker export
command, and you can inspect a docker image metadata with the docker inspect
command.
Data volumes
from the Docker user guide:
A data volume is a specially-designated directory within one or more containers that bypasses the Union File System to provide several useful features for persistent or shared data.
It is important to note here that a given volume (as the directory or file that contains data) is reusable only if it exists at least one docker container using it. Docker images don't have volumes, they only have metadata which eventually tells where volumes would be mounted on the union filesystem. Data volumes aren't either part of docker containers union filesystem, so where are they? under /var/lib/docker/volumes
on the docker host (while containers are stored under /var/lib/docker/containers
).
Data volume containers
That special type of container has nothing special. They are just stopped containers using a data volume with the sole and unique goal of having at least one container using that data volume. Remember, as soon as the last container (running or stopped) using a given data volume is deleted, that volume will become unreachable through the docker run --volumes-from
option.
Working with data volume containers
How to create a data volume container
The image used to create a data volume container has no importance as such a container can remain stopped and still fill its purpose. So to create a data container named datatest_data
for a volume in /datafolder
you only need to run:
docker run --name datatest_data --volume /datafolder busybox true
Here base
is the image name (a conveniently small one) and true
is a command we provide just to avoid seeing the docker daemon complain about a missing command. Anyway after you have a stopped container named datatest_data
with the sole purpose of allowing you to reach that volume with the --volumes-from
option of the docker run
command.
How to read from a data volume container
I know two ways of reading a data volume: the first one is through a container. If you cannot have a shell into an existing container to access that data volume, you can run a new container with the --volumes-from
option for the sole purpose of reading that data.
For instance:
docker run --rm --volumes-from datatest_data busybox cat /datafolder/data.txt
The other way is to copy the volume from the /var/lib/docker/volumes
folder. You can discover the name of the volume in that folder by inspecting the metadata of one of the container using the volume. See this answer for details.
Working with volumes (since Docker 1.9.0)
How to create a volume (since Docker 1.9.0)
Docker 1.9.0 introduced a new command docker volume
which allows to create volumes :
docker volume create --name hello
How to read from a volume (since Docker 1.9.0)
Let say you created a volume named hello
with docker volume create --name hello
, you can mount it in a container with the -v
option :
docker run -v hello:/data busybox ls /data
About committing & pushing containers
It should now be clear that since data volumes aren't part of a container (the union filesystem), committing a container to produce a new docker image won't persist any data that would be in a data volume.
Making backups of data volumes
The docker user guide has a nice article about making backups of data volumes.
Good article reagarding volumes: http://container42.com/2014/11/03/docker-indepth-volumes/
Best Answer
Stopped containers are visible via
docker ps -a
. If containers are started with the--rm
option, they'll remove themselves after stopping.You're absolutely right. State should never be saved inside a container.