Docker handy commands and information

Here I am listing docker handy commands and information which will be helpful in using Docker. I will keep on updating this file, hence making this file as Github gist.

Docker?

It lets you package your application in isolated environment (including os). Docker runs your application in light weight component called container.

To run the test container to see if your docker installation works.

docker container run hello-world

To list all the running containers

docker container ls

To list all the containers including exited ones

docker container ls --all

To stop the container

docker container stop <container-id>

To stop all the containers

docker container stop $(docker container ls -q)

To run container in detached mode with publishing ports, ports published as :

docker container run --detach --publish 80:80 nginx:alpine

To see the docker machine’s(daemon/server) IP, on windows this is the host where all the containers are running.

docker-machine ip

To run container in interactive mode, you will be in container to run the commands

docker container run --interactive --tty ubuntu:18.04 or
docker container run -it ubuntu:18.04

Docker file?

Docker file is a text document which contains steps to build a docker image which will be used to create dokcer container. Default name of the file is Dockerfile. This file can contain following
FROM ubuntu
RUN apt-get update && apt-get install nano
FROM Specifies the base image that your image will start from. It is only the required instruction to build image.
RUN Specifies the commands which will get executed one by one as part of image build process. e.g. RUN apt-get install -y git
ENV Sets environment variable e.g. ENV gituser=”sachin”
COPY Copy files from build context into image. e.g. COPY . /app
EXPOSE expose the ports from container e.g EXPOSE 8000
VOLUME Creates directory inside image which can be mapped to external storage. Value can be JSON array. e.g. VOLUME [“/var/log”]
CMD Specifis command to run when container starts e.g CMD [“python”,”manage.py”,”runserver”, ‘0:8000″]. Format is CMD [“executable”,”param1″,”param2″]. All instructions gets executed in order from docker file to build image except one CMD instruction, which gets executed when container is created.

Build docker image

docker image build –tag : .
e.g.
docker image build –tag docker-notes:v1.0 .
Here . is the build context, docker client will send(copy) contents of the build context to server and server stores it in working directory and uses it to build the image.
If you dont specify tag, docker takes ‘latest’ as default tag.

List all the images stored locally

docker image ls

Notes
Docker adds entry in the hosts file (/etc/hosts) with ip address of the container and id of the container. So the references by container id on the same machine goes to container only.

view raw

docker_notes.md

hosted with ❤ by GitHub