logo

Docker: my questions from the first day

What, when, where, why, and how?

Image by David Baxendale — source and license (CC BY-ND 2.0).

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.


Images and Docker Hub

How do I see the actual docker file on Docker Hub?

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.

Where is the actual image on my machine?

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.

Where on my machine can I see the actual Docker file that was used to create the image?

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>

Containers

How do I see the containers?

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.

Why is there STILL no Docker container?

If the image has nothing running, the container will exit by default and show the STATUS as exited in docker container ls.

How do I run the Docker container in the background?

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 .

How do I stop/remove a Docker detached container?

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 .

How can I enter a Docker container?

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.

How to I add sudo to a Docker container?

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.

How do I get information about my Docker container?

Try the inspect command:

docker inspect <container\_id>

Logs

How do I see the logs for my container?

Find the container container_id in docker container ls. Then:

docker logs <CONTAINER\_ID>

shows the logs.

How do I search 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>"

Final thoughts

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!

Contents

Oliver K. Ernst
August 29, 2020

Read this on Medium