Saturday 15 August 2015

Basic Docker Commands

Start Docker Server in Mac

Although the Docker client can run on Windows and Mac OS X to control a Docker Server, Docker containers can only be built and launched on Linux. The Docker server is integrated into the same binary that you use as the client. Therefore, non-Linux systems, like Mac, will require a virtual machine(e.g. VirtualBox) or remote server to host the Linux-based Docker server.
VirtualBox and Boot2Docker,  allow you to boot an Ubuntu-based Linux virtual machine on your local system.

1. Boot2Docker

To initialize Boot2Docker and download the required boot image:
$ boot2docker init

Start up a virtual machine with a running Docker daemon.
$ boot2docker up

$export DOCKER_HOST=tcp://192.168.xx.xx:2375

Test the connection:
$ docker info

Make sure client and server in the same version.

2. Docker-machine
In early 2015, Docker announced the beta release of Docker Machine, a tool that makes it much easier to set up Docker hosts on bare-metal, cloud, and virtual machine platforms.

Create a named Docker machine.
$ docker-machine create --driver virtualbox local

Run below to configure your shell
$ eval "$(docker-machine env local)"

To confirm:
$ docker-machine ls

Check the Docker host’s IP address
$ docker-machine ip

Stop and start a Docker machine

$ docker-machine stop local
$ docker-machine start local

The Docker Engine consists of two parts: a daemon, a server process that manages all the containers, and a client, which acts as a remote control for the daemon.

1. Check docker version
$ docker version

Container images are available on the Docker Hub Registry, a cloud-based collection of applications.
2. Search for images
$ docker search tutorial

3. Downloading container images
$ docker pull learn/tutorial

You can think of containers as a process in a box. The box contains everything the process might need, so it has the filesystem, system libraries, shell and such, but by default none of these are running. You 'start' a container by running a process in it.


4. Run a docker image
$ docker run learn/tutorial echo “hello world"

5. Install software in the container
$docker run learn/tutorial apt-get install -y ping

6. Save your changes
Find the ID of the container you are going to save.
$ docker ps -l
$ docker commit first-3-digits-ID learn/ping.

7. Check your container info.
$ docker inspect ID

8. Push your image to the Docker Hub Registry
By pushing (uploading) images that you build, you can easily retrieve them to use on other hosts as well as share them with other users.

$ docker push learn/ping

A docker ngnix example:

1. Check active VM
$ docker-machine ls

2. Start an NGINX container 
$ docker run -d -p 8080:8080 --restart=on-failure:3 --name web nginx

The docker run commands starts a container, runs it, and then exits. The -d flag keeps the container running in the background after the docker run command completes. The -p flag map port 8080 in the container to port 8080 on the Docker host.
By passing the --restart argument to the docker run command. It takes three values: no, always, or on-failure:#. If restart is set to no, the container will never restart if it exits. If it is set to always, then the container will restart whenever the container exits with no regard to the exit code.


3. When you start a container it automatically shares your /Users/username directory with the VM. You can use this share point to mount directories onto your container.

$ docker run -d -P -v $HOME/site:/usr/share/nginx/html  --name mysite nginx

Start a new nginx container and replace the html folder with yoursite directory.

4. Display all your containers
docker ps -a

5. View the container’s ports.
$ docker port mysite

6. To stop and then remove your running nginx container
$ docker stop mysite 
$ docker rm mysite

7. List all images in the system
$ docker images

8. delete an image and all associated filesystem layers
$ docker rmi image-id


9. Image updates
$ docker pull ubuntu:latest

pull command will automatically pick up any updates that have been published since you last ran it. That’s because latest is a tag that, by convention, is always moved to the most recent version of the image that has been published to the image registry.

10. View stats of running container
$ docker stats container-id

11. Build an image
$ docker build -t docker-hello:latest .

12. Create a new Bash session in the container
$ docker exec -it <containerIdOrName> bash

13. Remove all exited containers

$ docker rm $(docker ps -a)
Removing Volumes
$ docker volume rm $(docker volume ls)

Reference:
https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes 

No comments:

Post a Comment