Resources
What | Type | Link |
---|---|---|
Base Commands - Quick Ref | Reference | docs.docker |
Compose - getting started | Tutorial | docs.docker |
Compose - getting started | Tutorial | fedoraproject |
Compose Guide (Heroku) | Guide | Heroku |
Example of a YAML config for a complex backend/frontend docker setup | Tutorial | freecodecamp |
Devhint Cheatsheet | Reference | devhints.io |
Fireship: "Docker in 100 Seconds" | YT Video | YouTube |
Basic Commands
What | Command | Helpful Flags |
---|---|---|
List active containers | docker ps |
--all |
Restart a container | docker restart {containerName} |
|
Stop a container | docker kill {containerName} |
|
Inspect something | docker inspect {thingIdOrName} |
|
View logs | docker logs {?:containerName} |
-f follow-tail --details show extra detail |
Execute a command inside a container | docker exec {containerName} |
-i interactive-t allocate TTY-d detach |
Create volume | docker volume create {options} {volumeName} |
--driver (see notes on volume types)--label --name --opt |
List volumes | docker volume ls |
|
Drop volume | docker volume rm {volumeName} |
-f or --force |
Print version | docker version |
|
List images | docker image ls |
|
Pull an image | docker pull {imageName} |
|
Run an image | docker run {imageName} Example: docker run hello-world |
--name (give container a short name) |
List downloaded images | docker images OR docker image ls |
--filter |
Note: For most of the commands that take an image name, you can optionally request a specific tag or digest version of an image by using a special suffix. For example,
{imageName}:{tag}
or{imageName}@sha256:{digestSha}
docker-compose
is listed below, in its own section
Docker Compose (aka docker-compose) commands
CLI Overview: https://docs.docker.com/compose/reference/overview/, or use docker-compose --help
Composer YML file overview: https://docs.docker.com/compose/compose-file/
There are common flags that can be applied with any subcommand. For example:
-f
or--file
lets you specify a docker.yml
file other than the default ofdocker-compose.yml
.
What | Command | Helpful Flags |
---|---|---|
Test out a config | docker-compose config |
|
Stop containers, without removing | docker-compose stop |
|
Stop and remove containers | docker-compose down |
-v : remove named volumes |
Build and launch container | docker-compose up |
-d (starts in detached mode)--force-recreate --build |
Start a specific service | docker-compose run {serviceName} |
|
Removed stopped containers | docker-compose rm |
If you don't pass a name, it removes all (like rm * )-v removes the volumes as well. Useful if something is persisting when you don't want it to and need a hard reset.-f : Force |
Validate a config without running it | docker-compose config |
-q - Quiet mode, only validates true/false without verbose output |
Show logs | docker-compose logs |
-f follow--tail={#} Tail linesYou can also pass the container name - docker-compose logs {containerName} |
Docker-Compose syntax and options
-
You can use environment values within your docker-compose file (variable substitution)
-
You can pass environment values to the container itself
-
- You can use the
env_file
option to pass an entire.env
file contents to the container - Put under
environment:
but leave off value - Pass via
docker-compose run -e {VAR}={VAL} {imageName}
- You can use the
-
Volume types options / drivers
See: https://docs.docker.com/storage/storagedriver/select-storage-driver/
Type | Description |
---|---|
tmpfs |
Temp data, stored in memory, not persisted between sessions |
btrfs |
Advanced, persisted storage. Requires a bunch of pre-reqs, including Linux Kernel support. |
nfs |
Shared, persisted storage |
How do i...
How do I...
-
List active containers
docker ps
-
Restart a container with changes after a .yml / yaml config chance?
-
Bring up, and force image rebuild:
docker-compose up -d --force-recreate --build
-
If you run into issues (changes don't seem to take affect, passwords not working, etc)...
- Sometimes the volume data will persist, even if you ran
docker-compose down -v
first. - Try finding the volume data directory and running
rm -rf
on it, then rebuild and start back up
- Sometimes the volume data will persist, even if you ran
-
-
Get a container IP address
docker inspect {containerName}
-
Just IP address:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
- Use double quotes if you have issues.
- Credit: S/O
-
Debug a container not starting correctly (
die
event), or view logs-
Multiple options for log viewing:
-
See this S/O
-
Summary:
- Start event logger with
docker events&
- this kind of doestail
- Do "thing" - e.g.
up
, that triggers error - You should see error appear in event log
- Copy error instance ID from event log and use with
docker logs {eventId}
- Start event logger with
-
-
docker-compose -f {composeFile} logs
- Use
-f
to follow - Use
--tail={#}
to tail # of lines
- Use
docker logs {?:containerName}
-
-
-
Access BASH inside container
-
docker exec -ti {containerName} bash
- Use
exit
to stop
- Use
- You can also try
docker attach {containerName}
to link console STDIN/STDOUT, but this seems less optimal thanexec
-
-
Remove a stubborn network ("error while removing network ... has active endpoints")
-
Find out what containers are using the network
docker inspect {networkName}
-
Either bring down those containers, or break the connection
- To break connection(s), use
docker network disconnect {networkName} {containerName}
- To break connection(s), use
-
Once you have broken linkage, you should be able to bring down
- Either re-run
down
, or manually remove network withdocker network rm {networkName}
- Either re-run
-
-
Kill *all containers
docker ps -q | xargs docker kill
Issues
-
PostgreSQL issues with Windows - usually due to permissions issue
-
This is a known issue, and requires using named volumes. Links:
-
-
Can't remove volume (even with
-f
or--force
): "Error response from daemon: remove {volume}: volume is in use"-
Try this first:
docker-compose down -v
(ordocker-compose rm -f -v
)
-
If the volume is being locked by a container, you can find what is using it:
docker ps --filter volume={volume}
- Try
docker system prune
, and then remove volumes again -
As a last ditch effort, you can try restarting the entire Docker service (either via GUI, or CLI), then try removing container
- Windows: restart via CLI
- Lots of tips in the responses to this S/O question
-