Skip to content

Instantly share code, notes, and snippets.

@przbadu
Last active October 31, 2017 04:43
Show Gist options
  • Save przbadu/97a0d4e3e630ebc0f2dfa45f323c336d to your computer and use it in GitHub Desktop.
Save przbadu/97a0d4e3e630ebc0f2dfa45f323c336d to your computer and use it in GitHub Desktop.
Learning Docker

References:

https://github.com/docker/labs/

Basic commands

Check docker info, version, client and server versions, etc

docker info
docker --version
docker version

pull image

docker pull alpine

Docker Container

Run alpine image in a container

docker run alpine ls -la

Run interactive shell commands

docker run -it alpine /bin/sh

Quickly download and run nginx server

This command will docker pull nginx, then run container exposing port 80.

docker run -d -p 80:80 --name webserver nginx
  • -d detached mode (demonized)
  • -p port
  • --name to give container a name, we can quickly start, stop, remove this container with this name from next time.

Running container processes

running processes

docker ps

see all processes (running/stoped)

docker ps -a

Downloaded images

docker images

Check Running port

docker port webserver

Stop container

Stop webserver container that is running in port 80

docker stop webserver

Start container

docker start webserver

Stop and remove container at once

docker rm -f webserver

Experiment with static-site

$ docker run -d -p --name static-site -e AUTHOR="first container" dockersamples/static-site
2d902f63d29ace61c2f47f3c26f9b9647713dd8191a82e5591e3410522955c59

$ docker port static-site
443/tcp -> 0.0.0.0:32768
80/tcp -> 0.0.0.0:32769
# You can run browse this application in http://localhost:32769

$ docker run -d -p 80:80 --name static-site-2 -e AUTHOR="second container" dockersamples/static-site
a14804b930eed01837a4c803f74cd72591a251809819f92baa9877d5c20c2304

Here we are pulling dockersamples/static-site image from cloud. First docker run command will run static site application in random port, to see port number we used docker port static-site. According to our example the port used is 32769 and we can see output in http://localhost:32769.

We run another container for same image but this time we specify port as 80. So this time we can see output in http://localhost:32769.

In the above command:

  • -d will create a container with the process detached from our terminal
  • -P will publish all the exposed container ports to random ports on the Docker host
  • -e is how you pass environment variables to the container
  • --name allows you to specify a container name
  • AUTHOR is the environment variable name and Your Name is the value that you can pass

Let's use shortcut to remove second site

docker rm -f static-site-2

Docker Images

List of images

docker images

Pull specific version of image

By default latest images will be pulled. To pull specific version of image, we need to specify version number.

docker pull ubuntu:12.04

Search for docker images

You can search images in https://store.docker.com/. Or using command line:

docker search nginx

Build image from Dockerfile

Please check [Build Ruby on Rails image](todo add url) if you are interested.

If you have Dockerfile you can use docker build to build your image

docker build -t przbadu/demoApp .

Here,

  • -t specify tag, convention is to use /imagename
  • . specify the path of the Docker file, if Docker file is in current directory, then we can use . otherwise /path/to/Dockerfile.

Now we can run this image using:

$ docker run -p 8888:5000 --name myfirstapp YOUR_USERNAME/myfirstapp
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

Push your image to docker cloud

First you need to login

docker login

And then push,

docker push przbadu/myfirstapp

Delete docker image

docker rm -f myfirstapp
docker rmi przbadu/myfirstapp

docker-compose

docker-compose up -d

In detached mode

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