Docker

Updated: 14 June 2023

Build and tag an image from a Dockerfile in the same directory

docker build -t node-demo .

Start a container

docker run -i -t ubuntu /bin/bash
docker run -it node:19 /bin/bash
docker run -it --rm -d -p 8080:80 --name web nginx

Docker compose

docker-compose up --build
docker-compose up --remove-orphans -d

Stop all containers

docker stop $(docker ps -aq)

Remove all containers

docker rm $(docker ps -aq)

List images

docker images

See which containers are running

docker ps

See all containers, including non-running

docker ps -a

Remove stopped containers and images, including unused and dangling images

docker system prune -a

Delete all images

sudo docker rmi $(sudo docker images -q) --force

Run a php script inside a container

docker run --rm -v $(pwd):/app -w /app php:cli php hello.php

Get ip address of container

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container name>
sudo docker inspect <container name> | grep IPAddress

Command prompt into a container

sudo docker exec -it  bash
sudo docker exec -it wp-local-docker_phpfpm_1 /bin/sh -c "[ -e /bin/bash ] && /bin/bash || /bin/sh"
docker-compose exec --user www-data phpfpm bash

Keep a container running. From docker-compose.yml

services:
  myservice:
    command: bash -c "while true; do echo 'sleeping...' && sleep 10; done"

Leave a comment