Getting Started: Docker
Note: These examples will work on Linux out of the box, but might need to enable experimental futures on windows to run (see LCOW under Advance topics). The basics for each command is still same on Windows.
Installation
The best way to install Docker on your workstation is to follow the official guide provided by Docker:
Basic Usage
The best way to test if Docker is running on your workstation is to run the hello-world image, if the image is not already download on the local workstation, Docker with first pull it from the registry and then run it.
We can test if Docker is running properly by entering the following command in the Terminal window (Command Prompt in Windows):
docker info
docker version
docker run hello-world
Pull image
You can pull any image out-of-the-box from the Docker Hub repository by:
docker pull busybox
List pulled images
You can check all the pulled images stored locally:
docker images
Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 59788edf1f3e 6 days ago 3.41MB
hello-world latest 4ab4c602aa5e 4 weeks ago 263kB
Run container
You can run any image in Docker with:
docker run busybox
The Docker Engine will create a new container, and run it. In this case the container will finish execution and exit. This mode is useful when you just want to run a command or script, and turn off the container:
docker run busybox uname -a
This will print the kernel information, as the container sees it.
Interactive
To be able to interact with busybox
, you need to run:
docker run -it busybox
Output:
/ #
Now you have access to busybox. It can be used as if it was installed directly on your local workstation. Do remember, anything you save might be lost as soon as, you logout.
Daemon
Another way to run a container, is as a daemon (background service).
docker run -d --name=busy busybox /bin/sh -c "while true; do echo hello world; sleep 1; done"
You can check if the container is running by:
docker attach busy
hello world
hello world
You can exit the by pressing CTRL+C, the container will continue to run in the background.
Stop container
Stop a running container with:
docker stop [name|container_id]
Example:
docker stop ex1
List running containers
The easiest way to check what containers are running is:
docker ps
List all the containers, including stopped:
docker ps -a
Remove container
A container can be removed with:
docker rm [name|container_id]
Example:
docker rm ex1
Auto remove
Tell Docker to remove the container once finished running:
docker run --rm -ti busybox
Remove image
An Docker Image can be removed with:
docker rmi [image_name]
Example:
docker rmi busybox