Useful Docker commands

  Docker

Basic Docker commands

List all local images

docker images

The above is the same as:

docker image ls
The parameter -q will ensure only Ids are listed.
docker image ls -q

List all containers

docker ps
The parameter -a will include stopped containers.
docker ps -a

List all and don’t truncate columns (for example, to see entrypoints):

docker ps --no-trunc

Remove a single image

docker image rm [image_id]

Remove all image

This command will result in an error if an image currently used in a container.
docker image rm $(docker image ls -q)

Remove all containers

docker container rm -f $(docker container ls -a -q)

Create/run container

docker run --name [name_of_container] [image_name_to_use]

Go into container

Execute bash inside container with an abbreviated or full id (e.g. 5a432b61b585):

The parameter -it stands for interactive mode.
docker exec -it [container_id] bash

Basic Docker Compose commands

Using the parameter -d starts the containers in the background and leaves them running. Without it, you’ll see the logs but need to open another shell/terminal.
docker compose up -d
docker compose down

Docker & Postgres

Create Postgres database container

Create and run a container with…

  • name=db
  • mapping port 5432 to the same local port
  • setting password=password and username=username
  • creates a db with the name postgres
  • and uses image postgres to create the container:
docker run -d --name db -p 5432:5432 -e POSTGRES_PASSWORD=password -e POSTGRES_USER=postgres -e POSTGRES_DB=postgres_db postgres