From Bonus Bits
Jump to: navigation, search


This article gives common docker command syntax examples on a Mac with Docker Toolbox (VirtualBox) installed.



List Images

docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
<none>                        <none>              88c75ebcd6e9        9 hours ago         291.6 MB
<none>                        <none>              44e83afd49db        11 hours ago        291.6 MB
centos                        centos7             40dae1fe8777        9 days ago          196.7 MB
kitematic/hello-world-nginx   latest              04b455fad7b9        13 months ago       7.913 MB

Create Image from Dockerfile

  1. Write a Dockerfile
    FROM amazonlinux:latest
    MAINTAINER Levon Becker ""
    # Install Basics
    RUN yum clean all
    RUN yum update -y
    RUN yum install -y sudo openssh-server openssh-clients which curl vim-enhanced openssl ca-certificates net-tools htop git
  2. Build Image.
    docker build -f Dockerfile -t <name to give container> .
    Example With multiple tags
    docker build -t bonusbits/amazonlinux_base:2.2.3 -t bonusbits/amazonlinux_base:latest .

Tag Existing Image

docker tag my_image $DOCKER_ID_USER/my_image

Upload Image to Docker Hub from CLI

  1. Set username environment variable
    export DOCKER_ID_USER="username"
  2. Login
    docker login
  3. Upload Image
    docker image push <image name>
    1. Example
      docker image push bonusbits/amazonlinux_base

Import Tarball to Create Image

docker import </path/to/tar | URL> <name>

Export Image to Tarball

docker export -o </path/to/file.tar> <container name>

Remove Image

docker rmi <image name>


List Containers

Only Running

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
5c1f53e43598        88c75ebcd6e9        "/usr/sbin/sshd -D -o"   4 minutes ago       Up 4 minutes>22/tcp   kitchen-base-centos72


List running and stopped containers

docker ps -a

Lastest Running

List latest created container

docker ps -l

Create Container

Run Container from Image

Pull Image if not local, Create Container from Image, Random Container Name, Run interactive Bash Shell.

docker run -it <imagename>:<tag> /bin/bash

Pull Image if not local, Create Container from Image, Assign Container Name, Run interactive Bash Shell.

docker run --name <name to give container> -it <imagename>


docker run --name amzntest -it amazonlinux:latest

Create a New Container from Image

Create container from local or remote image, but don't start or interact.

docker container create  --name <name to give container> <imagename>:<tag>

Run Command on a Container

docker exec [OPTIONS] <container name or id> <command> [ARGS]

Create Container and Run Interactive Shell

docker run [OPTIONS] <container name or id> -it <image>:<tag> <command> [ARGS]


docker run --name centostest -it centos:7.4.1708 /bin/bash]
docker run --name amzntest -it amazonlinux:latest /bin/bash

Login to a Container

docker exec -it <container name or id> /bin/bash

Copy Files to a Container

Copy files from local system to a container.

docker cp [OPTIONS] <container name>:<source path> <destination path>

Fetch Logs of a Container

docker logs <container name>

Start, Stop, Restart and Kill a Container

docker start <name or id>
docker stop <name or id>
docker restart <name or id>
docker kill <name or id>

Remove a Container

docker rm <name or id>

Related Articles