Link Menu Search Expand Document

Docker

Quick docker cheat sheet that I put together as I was learning a bit about it.

Table of contents

  1. Introduction
  2. Build an image
  3. List images
  4. Start container
  5. Start remote container
  6. List running containers
  7. Stop a container
  8. Upload image to Registry
  9. Quick Cheat Sheet
    1. Create image using this directory’s Dockerfile
    2. Run “friendlyname” mapping port 4000 to 80
    3. Run “friendlyname” in detached mode
    4. See a list of all running containers
    5. Gracefully stop the specified container
    6. See a list of all containers, even the ones not running
    7. Force shutdown of the specified container
    8. Remove the specified container from this machine
    9. Remove all containers from this machine
    10. Show all images on this machine
    11. Remove the specified image from this machine
    12. Remove all images from this machine
    13. Log in this CLI session using your Docker credentials
    14. Tag <image> for upload to registry
    15. Upload tagged image to registry
    16. 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                   

Gilgalab Knowledge Base