Docker Commands

Useful docker commands

Run interactive shell on an image

docker run \
    -it -e VAR_A=1 -e VAR_B=2 \
    -v $PWD:/mapped_volume \
    -w /workdir \
    ubuntu:18.04 /bin/bash

Dockerfile example

# Dockerfile
FROM ubuntu:18.04
ENV TZ=Europe/Zurich
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN dpkg --add-architecture i386 \
    && apt-get update \
    && apt-get install -y \
         build-essential \
         wget \
         curl \
         expect \
         sudo \
    && rm -rf /var/lib/apt/lists/*
RUN useradd -ms /bin/bash user

Build Dockerfile and tag it with the name “image”

docker build -t image -f Dockerfile .

Clear all stopped containers

docker rm $(docker ps -q -f 'status=exited')