Docker Container

List all containers

docker ps
  -a      # include stopped
  -q      # only returns the container IDs

Run a shell inside an existing container

docker exec
  -it         # connect the container to terminal
  <command>   # bin/sh, bin/bash

Starting a stopped container

docker start <container>

Print a container’s logs

docker logs
  -f               # tail
  -t               # timestamps

Restarting a stopped container

docker restart <container>

Getting additional information about a container

docker inspect
  -f '{{ .NetworkSettings.IPAddress }}'   # selectively query.

Stop the running Docker container

docker stop <container>

Remove exited containers

docker rm -f $(docker container ls -f "status=exited" -q)

Remove all containers

docker rm -f $(docker ps -aq)

Show the history of an image

docker history <container-id>

Stop a running container through SIGKILL

docker kill web

Docker Image

Build a image

docker build
  -t <image-name>:<tag> # name docker image
  --no-cache            # do not use cache

Run an image

docker run
  -v <source>:<destination> # create a volume in our container from a directory on the host
  --net=<NETWORK>           # Creating a container inside a Docker network
  --volumes-from            #
  -it                       # connect the container to terminal
  --name <name>             # name the containe
  -e NAME=Sam               # environment variable
  -p 5000:80                # expose port 5000 externally and map to port 80
  -d                        # run in the background
  --rm                      # remove container automatically after it exits
  --restart=always          #
  --link <container-name>   #

Debug an image

docker run -it <image> /bin/bash

List images

docker images ls

Remove an image

docker rmi <image-name>

Remove all images

sudo docker rmi `docker images -a -q`

Pulling a image

docker pull <dockerhub-id>/<image-name>:<version>

Pushing a image to DockerHub

docker push <dockerhub-id>/<image-name>:<version>

Docker Network

List the networks

docker network ls

Creating a Docker network

docker network create
  -d <driver>         #

Inspect the network

docker network inspect <newwork>

Adding a new container to the network

docker network connect <newwork> <container>

Disconnecting a host from a network

docker network disconnect <newwork> <container>

Docker Volume

List volumes

docker volume ls

Remove volume

docker volume rm <volume>

Volumes Detail

docker volume inspect <volume>

Remove unused volumes using docker volume prune

docker volume prune


# Sets the Base Image for subsequent instructions.

# Apply key/value metadata to your images, containers, or daemons.
LABEL key="value"

# Sets environment variable.

# Cxecute any commands in a new layer on top of the current image and commit the results.

# The WORKDIR instruction provides a way to set the working directory for the container and
# the ENTRYPOINT and/or CMD to be executed when a container is launched from the image.

# Use COPY unless you sure you need ADD
# The key difference is that the COPY instruction is purely focused on copying local files from the build context and does not have any extraction or decompression capabilities.


# Creates a mount point for externally mounted volumes or other containers.
VOLUME ["/opt/project"]

# The ARG instruction defines variables that can be passed at build-time via the docker build command.
ARG webapp_user=user

# Configures a container that will run as an executable.

# Any arguments we specify on the docker run command line will
# be passed as arguments to the command specified in the ENTRYPOINT.
ENTRYPOINT ["/usr/sbin/nginx"]

# The CMD instruction specifies the command to run when a container is launched.
# We can override the CMD instruction on the docker run command line.
CMD ["-h"]