Skip to content

Instantly share code, notes, and snippets.

@psgganesh
Last active August 16, 2020 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psgganesh/36ab4288493c27e8b4f20676e139c781 to your computer and use it in GitHub Desktop.
Save psgganesh/36ab4288493c27e8b4f20676e139c781 to your computer and use it in GitHub Desktop.
Docker all-in-one

Docker

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.

Commands

Docker comes with 3 main components, docker, docker-machine, docker-compose. At the time of this writing, I had the following versions

$ docker --version
Docker version 19.03.5, build 633a0ea

$ docker-machine --version
docker-machine version 0.16.2, build bd45ab13

$ docker-compose --version
docker-compose version 1.24.1, build 4667896b

Using docker

To run docker commands with a container running from the host machine

Running an nginx instance from localhost for example

Steps
$ docker run --rm -p 8080:80 nginx

This will run reflect page from nginx port 80 onto the host port 8080. We should be getting an nginx page upon running. http://localhost:8080

Enter into the virtual env created

$ docker-machine env virtual-machine-01
$ eval $(docker-machine env virtual-machine-01)
$ docker run --rm -p 80:80 nginx

To get back to original env

$ eval $(docker-machine env -u)

To check the images which are currently present (ENV specific)

$ docker images

To check the containers which are running currently (ENV specific)

$ docker ps

Create / pull new containers with container tags using : symbol & running a command following it

$ docker run php:7.2-apache-stretch php --version

List running containers

$ docker ps

List all containers / running and not running

$ docker ps -a

Build a docker image with tag specify the working directory / context

$ docker build -t phpinfo:latest . # docker build -t <IMAGE_NICK_NAME> : <TAG_NAME>  <WORKING_DIR>

RUN a docker image

$ docker run phpinfo # docker run <IMAGE_NICK_NAME>

Run an image and name it as container. Run a command on that container

$ docker run --name=phptest php:7.2-apache-stretch php --version
Ctrl + C kill / abort from it

RUN a docker image and get inside it's shell

$ docker run -i -t --rm phpinfo bash

Run as a deamon

$ docker run -i -t -p 80:80 -d phpinfo 

Using docker-machine

To run docker commands from within a virtual machine, we need a virtual machine runner like virtualbox / vmware desktop to achieve the same.

Running an nginx instance from a virtualbox for example

Steps
  • Download virtualbox from the oracle virtualbox site
  • Create a virtual env
  • Run containers from within that env

After installing virtualbox

Create a virtual env

$ docker-machine create virtual-machine-01

Show original env

$ docker-machine env -u

Stop virtual machine

$ docker-machine stop virtual-machine-01

Remove virtual machine

$ docker-machine rm virtual-machine-01

Listing virtual machine

$ docker-machine ls

Using Dockerfile

Instructions

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