Logs, Exec and Copying Files In and Out
Read the log of a running or failed container, run a command inside it and move files across with docker cp. After this, you can find out what went wrong inside a container without guessing.
Course content
Sign in to save your progress.
-
Before You Run Anything
-
What Docker Is and Why Containers Exist
-
Install Docker Desktop on Windows With WSL2
-
-
-
Your First Containers
-
Run, Stop and Remove a Container
-
Logs, Exec and Copying Files In and Out
-
-
-
Images
-
Pull an Image and See What Layers Are
-
Build a Small Image With a Dockerfile
-
-
-
Data and Networks
-
Volumes: Keep Data When a Container Dies
-
Networks: Let Two Containers Talk by Name
-
-
-
More Than One Service
-
Docker Compose: Two Services With One File
-
Clean Up: Images, Containers and Disk Space
-
When Something Won't Start
-
-
- Final Check: Docker for Beginners
Skip ahead: If you already read container logs and use docker exec and docker cp, skip to "Copy files in and out", then go on to the next lesson.
A container is a closed box. When something goes wrong inside it, you need three ways to look: read what it printed (logs), run a command inside it (exec), and move files across the wall (cp). This lesson uses one web server and one deliberately failing container to practise all three.
Start the server first, the same way as in the last lesson.
docker run --name hello-web -d -p 8080:80 nginx:1.30-alpine
Status: Downloaded newer image for nginx:1.30-alpine
Read the logs
Whatever a program writes to its standard output or standard error ends up in the container's log. That is why well-behaved containers print to the console instead of to a file. Make one request, then read the last line of the log.
curl.exe -s -o NUL http://localhost:8080
docker logs --tail 1 hello-web
172.17.0.1 - - [15/Oct/2026:07:39:08 +0000] "GET / HTTP/1.1" 200 896 "-" "curl/8.21.0" "-"
That is nginx's access log: who asked (the address, date and time), what they asked for, the status code 200, and the size of the answer. The date and the curl version will be yours, not mine. --tail 1 limits the output to the last line. Without it you get everything since the container started, and a busy container prints a lot. Two more options you will use: --since 5m shows the last five minutes, and -f follows the log live until you press Ctrl+C.
Logs are most useful for a container that stopped. Run one that fails on purpose.
docker run --name boom alpine:3.24 sh -c "echo starting; echo something went wrong; exit 3"
starting
something went wrong
It was attached to your terminal, so you saw both lines at once. Now look at it as if it had run in the background.
docker ps -a --filter name=boom --format "table {{.Names}}\t{{.Status}}"
docker logs boom
NAMES STATUS
boom Exited (3) Less than a second ago
starting
something went wrong
"Exited (3)" is the exit code the program chose. Zero means success, anything else means it failed. The logs are still there after the container stops, and they stay until you remove it. That is the first place to look when a container exits straight away. If you skip it and go guessing, you will waste an hour on a problem the log states in one line.
Run a command inside a container
docker exec runs a command inside a container that is already running. To get a shell, add -it, which connects your keyboard to it.
docker exec -it hello-web sh
You get a prompt such as / #, and every command you type now runs inside the container. Type exit to leave. The container keeps running. nginx:1.30-alpine has sh but not bash, because Alpine images are small. If bash is not found, try sh.
You can also run a single command and read its answer, which is easier to copy and paste.
docker exec hello-web ls /usr/share/nginx/html
docker exec hello-web whoami
50x.html
index.html
root
The first command lists the folder nginx serves. The second shows that you are root inside the container. That is root inside a container, not on your Windows machine. It still deserves respect, because a bind mount (lesson 7) can give that root access to your files.
⚠ Warning: docker exec changes a live container. Anything you install or edit that way is lost when the container is removed and created again. Use it to look and to test. Do not use it to build your setup.
Copy files in and out
docker cp copies between your computer and a container, in either direction. The container name and path go on the container side, separated by a colon. In a terminal Docker may print a line such as "Successfully copied 2.05kB". It is not shown below, because the verified runs did not print it.
docker cp hello-web:/usr/share/nginx/html/index.html .\welcome.html
Get-Content .\welcome.html | Select-Object -First 4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
Now change the page and copy it back in.
Set-Content -Path .\welcome.html -Value "<h1>Hello from my own file</h1>"
docker cp .\welcome.html hello-web:/usr/share/nginx/html/index.html
curl.exe -s http://localhost:8080
<h1>Hello from my own file</h1>
The page changed, and nginx did not need a restart because it reads the file on every request. Remember where this change lives: in the container. Remove the container and the change is gone with it. For a change that survives, build it into an image (lesson 6) or mount a folder (lesson 7).
💡 Tip: On Linux and macOS, write the local path as ./welcome.html. The Windows .\ form works in PowerShell only.
Clean up before the next lesson.
docker rm -f hello-web boom
If you copy a file into a running container and then run docker rm -f on it, where is your file?
About
Read the log of a running or failed container, run a command inside it and move files across with docker cp. After this, you can find out what went wrong inside a container without guessing.
- Duration
- 5 min
- Last update
- 08/02/2026
Comments (0)
Sign in to commentNo comments yet.