Docker
Docker Commands
Verify commands are current/up to date and execute (&& is used to run multiple commands in sequence, if the first command fails, the second command will not run):
Start the Docker service: Enable Docker to start on boot: Verify the Docker installation: Add standard user to Docker group:
sudo apt-get update &&
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common &&
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - &&
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" &&
sudo apt-get update &&
sudo apt-get install -y docker-ce
Pull a Docker image from a registry: Pull a specific version. Without specifying tag, latest is pulled: Pull an image from a private registry: Common Images:
Run a Docker container in detached mode: Run a Docker container with a specific name: Run a Docker container with a specific port: Run a Docker container with a specific volume: Run a Docker container with environment variables: Test a Docker container --rm will remove the container after it stops:
Inspect a Docker container: Provides detailed information about the container, including its configuration, network settings, and more. Inspect a container's IP address: Inspect an image: Inspect a network:
View logs, add -f to follow the logs: View logs for a specific number of lines: View logs with timestamps, the --details flag shows extra details: Add --since and --until to view logs within a specific time range: Add regex to filter logs, 2>&1 redirects stderr to stdout: Add -E to search for multiple patterns, -i to ignore case:
Execute Commands against a Container
Copy files from a container to the host: Copy files from the host to a container: Copy files between containers: Enter a shell of a running container: Stop a container: Start a container: Restart a container: Pause a container: Unpause a container:
View all containers: View all images: Remove a container, -f to force removal: Remove all containers: Remove an image, -f to force removal: Remove all images: Docker prune removes all stopped containers, dangling images, and unused networks:
Create a network: List all networks: Inspect a network: Connect a container to a network: Disconnect a container from a network:
Docker Builds
Create a simple Dockerfile in your current directory - e.g. vim Dockerfile: Build an image, -t to tag the image, . to build from the current directory, from file named Dockerfile: Build an image from a specific Dockerfile: Build an image with a specific build argument: For example, this would be used to specify the version of the application, which can be accessed in the Dockerfile: To see this, define APP_VERSION in the Dockerfile. ARG is the same as ENV, but only available during the build process:
Build File Example
Build with ENVs, Entry Point, Copy files, Switch User and CMD