Docker
Quick docker cheat sheet that I put together as I was learning a bit about it.
Table of contents
- Introduction
- Build an image
- List images
- Start container
- Start remote container
- List running containers
- Stop a container
- Upload image to Registry
- Quick Cheat Sheet
- Create image using this directory’s Dockerfile
- Run “friendlyname” mapping port 4000 to 80
- Run “friendlyname” in detached mode
- See a list of all running containers
- Gracefully stop the specified container
- See a list of all containers, even the ones not running
- Force shutdown of the specified container
- Remove the specified container from this machine
- Remove all containers from this machine
- Show all images on this machine
- Remove the specified image from this machine
- Remove all images from this machine
- Log in this CLI session using your Docker credentials
- Tag
<image>
for upload to registry - Upload tagged image to registry
- Run image from a registry
Introduction
In order to use the CLI, the current user needs either be root or be part of the docker
group.
Build an image
Create a docker file, then run:
docker build -t <imagename> .
List images
docker images
Start container
List the images and see which one you want to instantiate (means, create a container from).
Then run:
docker run -d -p 4000:80 imagename
The above command creates a container from imagename
. Supposing that the imagename
container exposes port 80, that port will be mapped to the host’s port 4000. The -d
option tells that we want to start this container in the background.
Start remote container
docker login
docker run -p 4000:80 <username>/<repository>:<tag>
In my case, I created the friendlyhello
repository with the v1
tag in my docker cloud, so I can run:
docker run -p 4000:80 typoon/friendlyhello:v1
List running containers
docker ps
docker container ls
Stop a container
docker stop <id>
Where <id>
is taken from the list of running containers.
Upload image to Registry
First you need an account with Docker Cloud (or you need to setup your own registry). Go to https://cloud.docker.com and create an account.
Then run:
docker login
docker tag <imagename> <user>/<imagename>:<tag>
docker push <user>/<imagename>:<tag>
For example, I have created the friendlyhello
image and want to give it a tag called v1
, so I use:
docker tag friendlyhello typoon/friendlyhello:v1
docker push typoon/friendlyhello:v1
If I check the docker cloud website now, my image will be there.
Quick Cheat Sheet
Create image using this directory’s Dockerfile
docker build -t friendlyname .
Run “friendlyname” mapping port 4000 to 80
docker run -p 4000:80 friendlyname
Run “friendlyname” in detached mode
Does the same port mapping as described before but runs in detached mode.
docker run -d -p 4000:80 friendlyname
See a list of all running containers
docker ps
Gracefully stop the specified container
docker stop <hash>
See a list of all containers, even the ones not running
docker ps -a
Force shutdown of the specified container
docker kill <hash>
Remove the specified container from this machine
docker rm <hash>
Remove all containers from this machine
docker rm $(docker ps -a -q)
Show all images on this machine
docker images -a
Remove the specified image from this machine
docker rmi <imagename>
Remove all images from this machine
docker rmi $(docker images -q)
Log in this CLI session using your Docker credentials
docker login
Tag <image>
for upload to registry
docker tag <image> username/repository:tag
Upload tagged image to registry
docker push username/repository:tag
Run image from a registry
docker run username/repository:tag