What, when, where, why, and how?
I recently started using Docker, and with that produced an absolute pile of questions. Scraping through Google and StackOverflow for answers, here is a compilation of what I can remember learning on the first day.
Amazingly this isn’t a simple thing.
Docker Hub really just hosts the images, not the actual Dockerfile used to make them (assuming they were made from a Dockerfile). You can get lucky by heading to the page for the desired image on Docker Hub, and often you will find a link to a GitHub hosted Dockerfile.
You can also get some idea about the image if you head to Tags
and click on the tag you want, and look at the image history.
On your machine, run
docker info
and look for Docker Root Dir
, like mine:
Docker Root Dir: /var/lib/docker
Liar! I went to that directory and it doesn’t exist! —————————————————-
Probably you are on a Mac like me. In that case, a virtual image is located at:
~/Library/Containers/com.docker.docker/Data/vms/0
This image is run behind the scenes with HyperKit
to run Docker images. You can enter that image with:
screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
Then try the directory again
ls /var/lib/docker/
should do it! Press control k
to exit.
You are similarly out of luck! There’s some tricks floating around on how to search the logs for how the image was built to find the Dockerfile, but in general this is not shipped with the image when you do a
docker pull <IMAGE\_NAME>
To see the containers, run:
docker container ls
Why can’t I still see the container I wanted? ———————————————
To see the all containers, run:
docker container ls -a
Note the **-a**
flag— often, otherwise, containers you want to find will not be shown.
If the image has nothing running, the container will exit by default and show the STATUS
as exited
in docker container ls
.
Run it with the --detach
or -d
flag:
docker run -d <image\_name>
Make sure it will show with STATUS
as Created
in docker container ls
.
You can manually remove it with:
docker container stop <container\_id>
docker container rm <container\_id>
where container_id
can be seen in docker container ls
.
You will need to run the image with the -t
flag, which from the manual:
-t, --tty Allocate a pseudo-TTY
so run:
docker run -t -d <image\_name>
then you can enter the container with:
docker exec -it <container_id> bash
where container_id
can be seen in docker container ls
.
You need to run it with the controversial--privileged=true
flag, like this:
docker run -t -d --privileged=true <image\_name>
How can I copy a file to a Docker container? ——————————————–
You can copy a file <file>
to the container with:
docker cp <file> <container\_id>:/root/<file>
where container_id
can be seen in docker container ls
. This will copy the file to the /root/
directory; you could also place it elsewhere.
Try the inspect
command:
docker inspect <container\_id>
Find the container container_id
in docker container ls
. Then:
docker logs <CONTAINER\_ID>
shows the logs.
You need to redirect the output, then pipe it to grep:
docker logs <CONTAINER\_ID> 2>&1 | grep "<SEARCH>"
How do I search ALL the logs across ALL containers? —————————————————
You need to list all the containers, extract the container ID, and filter the logs with grep:
docker ps -q | xargs -L 1 docker logs 2>&1 | grep "<SEARCH>"
That’s the end of what I can remember from day one! I hope to update this list periodically with more insights, or leave a comment with your favorite ones!
Oliver K. Ernst
August 29, 2020