Docker Crash Course
Docker is one of those things that seems more complicated than it is. Atleast when it comes to using the tool. I have had the pleasure of using docker professionally and personally and it has always a pleasure. To start off learning Docker I'd always check out the documentation. Docker benefits from actually having good readible documentation that we will be referencing heavily in this article. To get started let's go ahead and get docker installed.
Docker Installation
Install Instructions based on Operating System
Docker Overview
Docker allows us to package our applications with all their necessary dependancies in virtual enviornments called containers. The typical work flow is as follows:
[Build Applications] ---> [Define virtual enviornment with docker file] --(Docker build command)--> [Docker Image] --(Docker run command)--> [Running Docker Container]
Keep in mind when you get to the stage of having and image you can upload it to a registry or have end users build from a repository containing your code and the docker file.
Typical Commands
Common Commands:
run Create and run a new container from an image
exec Execute a command in a running container
ps List containers
build Build an image from a Dockerfile
bake Build from a file
pull Download an image from a registry
push Upload an image to a registry
images List images
login Authenticate to a registry
logout Log out from a registry
search Search Docker Hub for images
version Show the Docker version information
info Display system-wide information
Management Commands:
builder Manage builds
buildx* Docker Buildx
checkpoint Manage checkpoints
compose* Docker Compose
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
plugin Manage plugins
system Manage Docker
volume Manage volumes
Swarm Commands:
config Manage Swarm configs
node Manage Swarm nodes
secret Manage Swarm secrets
service Manage Swarm services
stack Manage Swarm stacks
swarm Manage Swarm
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
export Export a container's filesystem as a tar archive
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Global Options:
--config string Location of client config files (default "/home/joshwatts/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host string Daemon socket to connect to
-l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/home/joshwatts/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/home/joshwatts/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/joshwatts/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Run 'docker COMMAND --help' for more information on a command.
For more help on how to use Docker, head to https://docs.docker.com/go/guides/
Hands on Labs
Create your first Docker Container
To get started follow the following steps to package a simple index.html file into a container running nginx. This will give you a chance to see the workflow in action.
Step 1) Create a new directory to house these files. Step 2) Create an index.html file with the following:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
<title>Test Docker Container</title>
</head>
<body>
<h1>Test Docker Container</h1>
<h2>This is a test docker Container</h2>
</body>
</html>
Step 3) Create Dockerfile and insert the following:
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/
EXPOSE 80
Step 4) Build Image
docker build -t test-container .
Step 5) Run Image
docker run -d -p 8080:80 --name test-container test-container
Step 6) Now you can visit the sample site at localhost:8080 and whenever you want to stop and remove it use the following:
docker rm --force test-container
Run multiple images at once with docker compose
Step 1)
git clone https://github.com/docker/getting-started-todo-app
cd getting-started-todo-app
Step 2)
docker compose up --build --detach
Step 3) Open http://localhost, then add an item to the to-do list.
Step 4) See the containers running
docker compose ps
docker ps
Step 5) Spin down containers
docker compose down