Skip to content

Docker basic commands

command explanation example
docker ps show running containers
docker ps -a show all containers, running and stopped
docker images show local images
docker images \ show details for a specific local image
docker stop|start stop or start a container
docker pull \:\ download docker image
docker run \:\ download (if needed) and run container based on image
docker exec \ \ execute a command in the container
docker exec -it \ \ execute a command in the container docker exec -it mycontainer /bin/bash
docker run -d \:\ similar to above but in detached mode or non-interactive
docker run -p \:\ run docker container and expose the internal application port
docker rm \ remove a docker container
docker rmi \ remove a docker image
docker build -t \:\ \ builds a docker image based on Dockerfile specification
docker tag \:\ \:\ change the tag of a local container image. used when uploading to public or private registry. container image is duplicated
docker push \/\:\ upload local container image to private or public registry
docker login authenticate to private or public registry
docker volumes ls show all local volumes
docker volumes create create a new volume

Docker Volumes

Used for data persistency. Docker container is volatile and data is destroyed when container is destroyed. For certain use cases such as databases, this is not practical.

Host Volumes: - usually done within docker run command by sepcifying -v argument

`docker run -v <localDirectory>:<containerDirectory>`

`docker run -v /home/data:/var/lib/mysql/data`

Anonymous Volumes: - similar to above but without specifying the local path. docker creates the local volume automatically with a random hash as the name usually in /var/lib/docker/volumes/_data

`docker run -v /var/lib/mysql/data`

Named Volumes: <-- This should be used in production - similar to host volumes with the exeption that instead of path, local location is referenced by name

`docker run -v <volName>:/var/lib/mysql/data`

Docker Compose basic commands

command explanation
docker-compose -f \ up create containers from input file and start them interactively
docker-compose -f \ up -d as above except containers are started detached or non-interactive
docker-compose -f \ down stop and destroy the containers - interactive

Docker Compose File

It's a YAML file that describes all the command line arguments passed to start your application containers. Docker Compose also creates a docker network by default and that is no longer required to be done via docker network create <myNetwork> command.

version: '1'
services:
  mongodb: # equivalent to specifying --name argument on the docker command
    image: mongo # docker image to use
    ports: # equivalent to specifying -p argument on the docker command
      - 27017:27017
    environment: # equivalent to sepcifying -e argument on the docker command
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    volumes: # equivalent to specifying --volumes or -v on the docker command
      - mongo-data:/data/db
  mongo-express:
    image: mongo-express
    ports:
      - 8080:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb
volumes: # equivalent to running docker volumes create
  mongo-data:
    driver: local

Docker File

It a simple file that defines the construct of your container image. It is usually based on an existing image, i.e ubuntu, node, etc. Docker file MUST always be name Dockerfile with a capital D.

every time Dockerfile is changed, a new image MUST be created

# FROM specifies any base/existing image you need to work from
# for example, node image is itself based on alpine image
FROM node

# ENV defines any environment vars, typically default vars if needed
ENV ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
    ME_CONFIG_MONGODB_ADMINPASSWORD=password \
    ME_CONFIG_MONGODB_SERVER=mongodb

# RUN executes any Linux command inside the container when it's run
RUN mkdir -p /home/app

# COPY command executes on the host, different from RUN command above
COPY . /home/app

# CMD executes any docker entry point linux command, i.e. jekyll serve, node server.js
CMD ["node","/home/app/server.js"]

Docker Image Name Convention (Docker Registries)

\/\:\

exmaple: mydockerimage.mydomain.com/myapp:20