Skip to content

Instantly share code, notes, and snippets.

@snnsnn
Last active August 3, 2019 13:17
Show Gist options
  • Save snnsnn/8e64dd6358b59ec5861451095a60ef18 to your computer and use it in GitHub Desktop.
Save snnsnn/8e64dd6358b59ec5861451095a60ef18 to your computer and use it in GitHub Desktop.

Docker Quick Reference

Concepts

Dockerfile: blueprint for an image that defines what goes on in the environment inside a container.

Image: an executable package that includes everything needed to run an application --the code, a runtime, libraries, environment variables, and configuration files.

Container: a runtime instance of an image --what the image becomes in memory when executed (that is, an image with state, or a user process).

Service: Services are just “containers in production.” A service only runs one image, but it codifies the way that image runs—what ports it should use.

Swarm: A group of machines that are running Docker and joined into a cluster.

Nodes: Machines in a swarm can be physical or virtual. After joining a swarm, they are referred to as nodes. Nodes in swarms can be managers or workers.

swarm manager: Machines in a swarm that can execute commands, or authorize other machines to join the swarm as workers.

workers: Workers are just there to provide capacity and do not have the authority to tell any other machine what it can and cannot do.

Stack: A group of interrelated services that share dependencies, and can be orchestrated and scaled together.

docker-compose.yml: a YAML file that defines how Docker containers should behave in production

Registry: a collection of repositories, i.e: hub.docker.com

Repository: a collection of images

Task: A single container running in a service. Tasks are given unique IDs that numerically incremented up to the number of replicas defined in docker-compose.yml

webnet: load-balanced overlay network

Building image

# Use an official nodejs runtime as a parent image
FROM node

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in package.json
RUN npm install && npm cache clean --force
RUN npm run build

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NODE_ENV=production

# Run app when the container launches
CMD ["npm", "start"]
# Build an image
$ docker build -t «username»/«image-name»:«tag-name» «docker-file-path»
# Build an image tagged myApp using the Dockerfile in the same folder where the command was executed
$ docker build -t myApp .

# Tag an «image» for upload to registry
$ docker tag «image-name» «username»/«repository»:«tag»

# Create a tagged image with version
$ docker tag «image-name» «tag»:«version»

# Create a new image with the latest tag
$ docker tag «image-name» «new-image-name»

# Create a new image specifying the “new tag” from an existing image and tag
$ docker tag «username»/«repository»:«tag» «new-image-name».[:new-tag]

# Publish an image
docker push «username»/«repository»:«tag»

# Export the image to an external file
$ docker save -o «file-name».tar

# Import an image from an external file
$ docker load -i «file-name».tar

Images related commands

# List local images
$ docker images

# Remove one or multiple images by name or id
$ docker rmi «image-name | image-id» «image-name | image-id»

# Remove multiple images using $ to list image id
# This one removes dangling images
$ docker rmi $(docker images -f "dangling=true")

# Checking the history of an image
$ docker history «image-name»
$ docker history «username»/«repository»:«tag»

Running container

# Run official image tagged latest from docker registery
# Same as running  docker/hello-world:latest
$ docker run hello-world

# Stop container
$ docker container stop «container-name or container-id»

# Run an image from a registry
$ docker run -p 4000:80 «username»/«repository»:«tag»

# Assigning custom names
$ docker run --name «container-name» -p 4000:80 «username»/«repository»:«tag»

# Mapping port:
# -p is for port remapping of «host-port»:«container-port»
$ docker run -p 4000:80 «tag-name»

# Run a container in detached mode:
$ docker run -d -p 4000:80 «tag-name»

# Run a detached container in a previously created container network:
$ docker network create «networ- name»
$ docker run --name «container-name» -d --net «network-name» -p 8080:8080 «image-name»

# Run a detached container mounting a local folder inside the container:
$ docker run --name «container-name» -d -v «host-directory»/:«in-container-directory» \
  -p 8080:8080 «image-name»

# List running containers
$ docker container ls

# List all containers
$ docker container ls --all
$ docker container ls --a

# List all containers in quite mode
docker container ls -aq

# Gracefully stop container
$ docker container stop «hash»

# Force shutdown of the specified container
$ docker container kill «hash»

# Remove specified container from the machine
$ docker container rm «hash» 

# Remove all containers
docker container rm $(docker container ls -a -q)

# Run a bash shell inside an image
$ docker run -it «image name» bash

# Print the logs of a specific container
$ docker logs -f «container-name»
$ docker logs -f «container-name | container-id»

Services

A docker-compose.yml file is a YAML file that defines how Docker containers should behave in production.

# docker-compose.yml
version: "3"
services:
  web:
    image: «username»/«repository»:«tag»
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "4000:80"
    networks:
      - webnet
networks:
  webnet:
# Switch into swarm mode, otherwise
# you get an error that “this node is not a swarm manager.”
$ docker swarm init

# Deploy the app
$ docker stack deploy -c «compose-file» «app-name»

# Redeploy the app after change
$ docker stack deploy -c docker-compose.yml «app-name»

# Take the app down
$ docker stack rm «app-name»

 # Take down a single node swarm from the manager
$ docker swarm leave --force

# List stacks or apps
$ docker stack ls

# List the tasks for a service
# List running services associated with an app
$ docker service ls

# List tasks associated with an app
docker service ps «service»

# List container IDs
$ docker container ls -q

# Inspect task or container
$ docker inspect <task or container>

Swarms

# Enable swarm mode and make current machine a swarm manager
$ docker swarm init

# Have other machines join the swarm as workers
$ docker swarm join

# Run on the manager to view the nodes in the swarm
$ docker node ls

# Run from each node to leave the swarm
$ docker swarm leave

Commands

# List Docker CLI commands
$ docker
$ docker container --help

# Show version
$ docker --version 

# Show detailed information
$ docker version
$ docker info

# List images
$ docker images
$ docker image ls

# Build image
$ docker build -t friendlyhello .

# Manage Docker without sudo
# Create the docker group.
$ sudo groupadd docker
# Add your user to the docker group.
$ sudo usermod -aG docker $USER
# Log out and log back in so that your group membership is re-evaluated or run code below
$ exec su -l $USER

# Configure Docker to start on boot
$ sudo systemctl enable docker
# Disablo start on boot
$ sudo systemctl disable docker

# Docker is automatically configured to start on boot using upstart. To disable this behavior
$ echo manual | sudo tee /etc/init/docker.override

# chkconfig
$ sudo chkconfig docker on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment