Skip to main content

Docker Commands

Command line #

Run an 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

Build Dockerfile and tag it with the name “image” #

docker build -t image -f Dockerfile .

List running containers #

docker ps

Attach to a running container #

docker exec -it <container_name> <command>
docker exec -it <container_name> /bin/bash

Clear all stopped containers #

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

Prune docker images #

docker image prune -a

Dockerfile example #

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