Docker copy

Updated: 14 June 2023

Copy file from container to host

docker cp <:containerId>:/file/path/within/container /host/path/target

Copy file from host to container

docker cp /host/path/target <:containerId>:/file/path/within/container

Docker init

Updated: 08 May 2023

Run an init inside a container which forwards signals and reaps processes. Configure in a Dockerfile

FROM mcr.microsoft.com/dotnet/sdk
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        tini \
    && rm -rf /var/lib/apt/lists/*
ENTRYPOINT ["/usr/bin/tini", "--"]

or docker-compose.yml

services:
  web:
    image: alpine:latest
    init: true

https://github.com/krallin/tini

Tini is the simplest init you could think of. All Tini does is spawn a single child (Tini is meant to be run in a container), and wait for it to exit all the while reaping zombies and performing signal forwarding.

Dockerfile Linter – Haskell

Updated: 18 August 2023

A smarter Dockerfile linter that helps you build best practice Docker images. The linter parses the Dockerfile into an AST and performs rules on top of the AST. It stands on the shoulders of ShellCheck to lint the Bash code inside RUN instructions.

Use online at https://hadolint.github.io/hadolint

Run in a Docker container

docker run --rm -i hadolint/hadolint < Dockerfile

Docker ip addresses

Updated: 27 March 2024

On Linux, IP address of the Docker host

172.17.0.1

Get the IP address of a running container

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' id-or-name

Docker logs

Updated: 16 November 2023

View and follow container logs

sudo docker logs -f my-symf

View container logs via docker compose

docker compose logs certbot
docker compose logs --follow --timestamps --tail="all" <service>

Docker volumes

Updated: 03 May 2024

List all docker volumes

docker volume ls

Inspect a volume

docker volume inspect my-vol

Delete a volume

docker volume rm my-vol

Delete all volumes

docker volume rm $(docker volume ls -q)

List volumes for a container

docker inspect -f '{{ .Mounts }}' containerid

Docker

Updated: 06 May 2024

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)

List images

docker images

See which containers are running

docker ps

See all containers, including non-running

docker ps -a

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"

Remove all containers

docker rm $(docker ps -aq)

Delete all images

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

Remove stopped containers and images, including unused and dangling images

docker system prune -a

phpMyAdmin in Docker

Updated: 16 November 2023

phpMyAdmin is available as a Docker image. The MySQL server connection parameters are provided when the container is instantiated.

For a remote MySQL server

docker run --name myadmin \
-e PMA_HOST=https://example.com \
-e PMA_PORT=3306 \
-e PMA_USER=user \
-e PMA_PASSWORD=password \
-p 8080:80 \
phpmyadmin/phpmyadmin

Browse to phpMyAdmin at http://localhost:8080

MySQL server running in a local Docker network some-network

docker run --name myadmin \
--network=some-network \
-e PMA_HOST=db \
-e PMA_PORT=3306 \
-p 8080:80 \
phpmyadmin/phpmyadmin

--network The docker network the phpMyAdmin container will join.