> Running Containers > Image Management
Docker for Data Science # Run a container with docker run {image}
docker run hello-world # Runs a test container to check your installation works
# List all local images with docker images
Cheat Sheet
docker images
# Run a container then use it to run a command with docker run {image} {command}
# Show Docker disk usage with docker system df
docker run python python -c "print('Python in Docker')" # Run Python & print text
docker system df
docker run rocker/r-base r -e "print(lm(dist~speed, cars))" # Run R & print a model
# Show image creation steps from intermediate layers with docker history {image}
# Run a container interactively with docker run --interactive --tty
Learn online at www.DataCamp.com docker run --interactive --tty rocker/r-base # Run R interactively
docker history alpine
# Save an image to a file with docker save --output {filename}
# Run a container, and remove it once you've finished with docker run --rm
# Usually combined with a compression tool like gzip
docker run --rm mysql # Run MySQL, then clean up
docker save julia | gzip > julia.tar.gz
# Run an image in the background with docker run --detach
# Load an image from a file with docker load --input {filename}
docker run --detach postgres
docker load --input julia.tar.gz
> Advantages and Disadvantages of Containers # Run an image, assigning a name, with docker --name {name} run
docker run --name red1 redis # Run redis, naming the container as red1
# Delete an image with docker rmi {image}
docker rmi rocker/r-base
Role Native software Virtual machine Docker or other container # Run an image as a user with docker run --user {username}
docker run --user doctordocker mongo
Complexity Low High Medium
Portability Low Medium High
> Working with Registries
>
Reproducibility Low High High
Startup time Low High Medium Inspecting Containers
# Log in to Docker with docker login --username {username}
Scalability Low High High
docker login --username doctordocker
# List all running containers with docker ps
Resource efficiency High Low High docker ps
# Pull an image from a registry with docker pull {image}
Isolation (Security) Low High Medium docker pull julia
# List all containers with docker ps --all
Managing dependencies Low Medium High docker ps --all
# Pull a version of an image from a registry with docker pull {image}:{tag}
docker pull julia:1.8.5-bullseye
# List all containers matching a conditions with docker ps --filter '{key}={value}'
docker ps --filter 'name=red1'
# Tag an image to a repo with docker tag {image} {user}/{repo}
>
docker tag python doctordocker/myrepo
Definitions # Show container log output with docker logs --follow {container}
docker run --name bb busybox sh -c "$(echo date)" # Print current datetime
# Push an image to a registry with docker push {repo_tag}
docker logs --follow bb # Print what bb container printed
docker push doctordocker/myrepo
Docker image: A read-only template containing all the necessary files, libraries, and # Search for an image with docker search "{image-search-text}
dependencies required to run an application in a Docker container "docker search "py"
Docker container: A running instance of an image. That is, an executable package including
> Managing Containers
>
the code, libraries, and dependencies needed to run the application # docker run is equivalent to docker create + docker start
# Create a container from an image with docker create {image}
# Start a container with docker start {container}
Creating Dockerfiles
Dockerfile: A script containing instructions to create the image docker create --name py --interactive --tty python
docker start --interactive --attach py
# Derive image from another image with FROM{image}
# Same as docker run --name py --interactive --tty python
Docker registry: A repository for storing, sharing, and managing Docker images. These FROM ubuntu:jammy-20230301
include Docker Hub, Amazon Elastic Container Registry, Microsoft Azure Container Registry, # Create a new image from a container with docker container commit {container}
# Set a build and runtime environment variable with ENV {name}={value}
docker container commit
and Google Cloud Container Registry ENV TZ="America/New_York"
# Stop a container with docker stop {container}
# Set a build-time variable with ARG {name}={default_value}
# Container has option to save state or ignore request
Docker Engine: An application for managing Docker containers. It includes a server (the ARG VERBOSE=1
docker stop py
"Docker daemon"), a command line tool (the Docker client), and an API for other software to # Set the working directory with WORKDIR {path}
# Kill a container with docker kill {container}# Container process finished immediately
interact with the Docker Daemon WORKDIR /home
docker kill py
# Switch to the user with USER {username}
# Kill and remove a container with docker rm --force {container}
Docker client: A command-line tool to interact with Docker Engine to manage Docker images USER doctordocker
docker rm --force py
and containers # Copy a local file into the image with COPY {existing_path} {image_path}
# Stop then start a container with docker restart {container}
COPY ./settings/config.yml ./settings/config.yml
docker restart py
Docker daemon (a.ka. Docker server): A background process that manages Docker images # Run a shell command during the build step with RUN {command}
# Delete stopped containers with docker container prune
and containers according to the commands sent from the Docker client. # \ lets commands continue across multiple lines
docker container prune
# && means run this command only if the preceding command succeeded
RUN apt-get update \
# Create an image from a container with docker container commit {container_id} {image}
&& install -y libxml2-dev
# Find the container ID with docker ps --all
docker container commit 123456789abc newimage
>
# Run a shell command on launch with CMD ["{executable}", "{param1}"]
Getting Help # Each Dockerfile should only have 1 CMD statement
CMD ["python", "-i"] # Start Python interactively
# Display Docker version with docker --version
docker --version
> Building Images
# Display Docker system info with docker info
# Build an image with docker build {path}
docker info
docker build .
Learn Docker Online at
# Get help on Docker with docker --help
# Build a tagged image with docker build --tag {name:tag} {path}
www.DataCamp.com
docker --help
docker build --tag myimage:2023-edition .
# Get help on Docker command usage with docker {command} --help
# Build an image without using the cache docker build -no-cache {path}
docker run --help docker build --no-cache .