Skip to content

A guide for Docker along with examples of Dockerfiles and Docker-compose files.

Notifications You must be signed in to change notification settings

arthuranteater/dockerize-it

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Dockerize It!

A guide for Docker along with examples of Dockerfiles and Docker-compose files.

Docker Commands

View Containers

docker container ls
docker ps

Flags:

-a = show all containers

-q = show all active container ids

-ql = show last active container id

-qa = all container ids

docker start $(docker ps -ql)

docker ps -qa | xargs docker stop

Inspect Container

docker inspect container_id || container_name 

Start Container

docker start container_id || container_name 

Stop Container

docker stop container_id || container_name

Delete Container

docker rm container_id || container_name

Delete all:

docker ps -qa | xargs docker rm -f

Create Container

docker run base_image_name:tag_name + args_for_ENTRYPOINT --name name_container
  • If no local image found, runs Docker pull to check Docker Hub

-it = shell into the running container

-e = environment variable -e PORT...

--rm removes container after shutdown

Rename Container

docker rename container_id || old_container_name new_container_name

Remove Container

docker rm container_id || container_name 

Get Image

docker pull username/image_name:tag_name
  • Default tag name is latest

View Images

docker images

View Image Layers

docker name_of_image:tag_name history

Create Image

Standard

1. Create Dockerfile

Dockerfile:

FROM base_image_name:tag_name
RUN install x && install y
RUN install y
COPY file_name + destination
WORKDIR starting_directory_for_CMD
CMD npm start
ENTRYPOINT ["command", "command", "command"]
MAINTAINER author_name + author_email
EXPOSE port_number
ENV environment_var
USER command_line_user
  • RUN installs, commands end after execution always put before COPY src

  • CMD only executed once per Dockerfile as default command

  • ENTRYPOINT allows for args to be passed in via terminal with docker run to be executed by ENTRYPOINT overriding a potentially a default CMD

  • COPY copies directories recursively. All files in the root directory can be selected with . The root directory of the build starts with / for the destination. best practice is . /src/

  • VOLUME directory to hold persistent data

  • WORKDIR sets directory for run commands, instead of cd directory_name

  • ENV can be injected on docker run with -e

  • USER default is root user

2. Build Image

docker build -t name_of_image:tag_name + Dockerfile_location

Flags:

-q with build = limits output

--no-cache = build from scratch

Share Image

docker push username/image_name:tag_name

Delete Image

docker rmi container_id || container_name

Connect to Container

-In order to connect to a container you must publish the exposed ports

  • You can do this with the -P flag during container creation then run docker ps to find the port mapping

  • Or you can manually map the port to connect with it using -p exposed_port:container_port

  • bridge network (same computer) is the default

  • --net none is null network driver

  • overlay network (multiple computers)

  • can use --net to connect new container to existing network

  • can use same container_name if different networks using --net-alias

View Networks

docker network ls

Mount Volume

docker run -v host_location:container_location

Connect Container to Container

A. Default Network

  • docker-compose finds all child Dockerfiles to run multiple containers in default bridge network

docker-compose.yaml:

service_name: 
  build: src_directory
  image:
  ports:
  networks:
  volumes:
  environment:
  command:

-build can be written as
      build:
        context: ../../jenkins/docker

B. Define Your Own

version: "3"
services:
  postgres:
    container_name: class_postgres_4
    image: postgres:11.1
    restart: unless-stopped
    networks:
      - my-net
    ports:
      - "15432:5432"
    environment:
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: "${MY_PG_PASS}"
      POSTGRES_DB: "gogs"
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres", "-d", "gogs"]
      interval: 10s
      timeout: 5s
      retries: 5

Defining the network via docker-compose:

networks:
  my-net:
    driver: bridge

C. CLI Option

docker network ls
docker network create network_name 
docker run --net network_name 

Docker Compose CLI

docker-compose ps
docker-compose up -d
docker-compose down
docker-compose kill
docker-compose rm
docker-compose build

View Volumes

docker volume ls

Orchestration Tools

  • New Relic’s Centurion
  • Spotify’s Helios
  • Ansible’s Docker

Distributed Schedulers

  • Google’s Kubernetes
  • Apache Mesos

Cloud Providers

  • Amazon EC2 Container Service
  • Google Container Engine
  • Red Hat OpenShift 3
  • Joyent Triton
  • Microsoft Azure

About

A guide for Docker along with examples of Dockerfiles and Docker-compose files.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published