Docker commands

All about Docker container and Images

Ref documentation https://docs.docker.com/v17.12/get-started/part2/#dockerfile

Dockerfile

#Refer to base image to start from
FROM ubuntu:latest

# Set environment
ENV HUGO_VERSION 0.50

# Run command line command
RUN apt-get update && apt-get install mytool

# Download online resource to image location
ADD https://github.com/spf13/hugo/releases/download/v${HUGO_VERSION}/${HUGO_BINARY}.tar.gz /usr/local/hugo/

# Open port
EXPOSE 1313

# set working dir
WORKDIR /app

# Execute command line command
CMD hugo version

Docker Images


#Build image from Dockerfile
docker build -t my-tag dirname-with-dockerfile

#Remove existing docker image
docker rmi my-docker-image

#Export/Import image to/from tar
docker export -o my-export.tar my-docker-image
docker import my-export.tar my-docker-image

#Add new tag to docker image
docker tag my-docker-image new-tag

Docker Container

#Run container
docker run --name=my-container my-docker-image

# Run container with port forwarding
docker run -p 80:80 -p 8443:8443 -p 8080:8080 --name=my-container my-docker-image

# Run with folder mapping
docker run -v shared:/in/container

# Run interactive session
docker run -it my-container my-docker-image

# The same in three steps
docker run --name=my-container my-docker-image
docker start my-container
docker exec -it my-container /bin/bash

# silently execute command in running container
cmd='ls -al > /resource/ls.out'
docker exec my-container $cmd

# list containers
docker container ls

# list all processes
docker ps -a

Other docker related commands

# login to Docker Hub for pushing repo
docker login

# push image
docker push <username>/twentyfortyeight:<tag>


# get image from Docker hub
docker pull singularityware/docker2singularity

Run docker without root priviledges

#add user to docker group
curl -fsSL https://get.docker.com/rootless | sh
export DOCKER_HOST=unix:///run/user/$(id -u)/docker.sock
sudo usermod -a -G docker $USER
systemctl --user start docker