Docker · General · MAC · Windows

Docker PlaySchool

Statement : Docker is a tool which enables us to create, build, deploy and publish the containers in the Micro-Service architecture world.

While working on the projects, most of us would have come across this term called Docker. In this play-school, I am gonna cover commonly used docker commands to fulfil our day to day needs irrespective of any project language preference like Java, JS, Pyhon etc

To start with, runt the below command to get the feel of what docker does. 
$ docker run hello-world
In case of any help required regarding any docker command.
$ docker action_name --help
To list all the running container.
$ docker ps
To list all the running and stopped container.
$ docker ps -a
To Get the logs of any docker container.
$ docker logs container_id
To Get the logs of any docker container in particular time-span.
$ docker logs --since 5s container_id
TO list all the available images on your machine.
$ docker image ls
To Get the detailed info about any docker container.
$ docker inspect container_id
To stop any running docker container before deletion.
$ docker stop container_id
To delete any docker container.
$ docker rm container_id
To run any docker container with the specifies.
$ docker run container_name command

To run the container in background with out interruption.
$ docker run -d container_name command

Note* To your knowledge, it does all the magic. If container_name doesn't exist on your machine then it pulls the image(container_name) from registry.
To expose the port while running any server inside docker container.
$ docker run -p Your_Machine_Port:Container_Port container_name
To expose the volume to persist the data of any docker container.
$ docker run -v /your_machine/folder:/docker/container/folder -d container_name

Dockerfile : A Dockerfile is a text document which contains all the instructions a user could call on the command line to create an image. Below is the sample Dockerfile which will have all commonly used instructions –

# FROM Instruction is the first step used to pull the base image
FROM alpine:latest

# Use RUN Instruction to execute any commands like getting all the dependencies etc
RUN apt-get update

# Use ENV/ARG Instruction to set any variable inside docker container
ENV/ARG ENV_NAME = ENV_VALUE

# Use CMD Instruction to run the command inside container
CMD ["echo", "Use this Docker PlaySchool link to build your docker competency"]

# Use WORKDIR Instruction equivalent to cd /dir 
WORKDIR /dir

# Use COPY/ADD Instruction equivalent to cp -rf machine/dir /container/dir
COPY/ADD machine/dir /container/dir

# Use EXPOSE Instruction to run the service on any port.
EXPOSE 8123

# Use ENTRYPOINT Instruction to run the service. java -jar /container/dir/app.jar
ENTRYPOINT ["java","-jar","app.jar"]
To Build the above docker image.
$ docker build -t container_name .
To run the above image.
$ docker run --rm container_name
To tag the image with some unique name before pushing into registry.
$ docker tag imae_name tag_name
To login into the docker.
$ docker login dokcer_server (write the name of the server from where you are going to push/pull the image)
To push the image into the registry.
$ docker push tag_name
To keep the container running always even if we stop the container
$ docker run --rm -it -d -p 8085:8080 --restart always container_name

To keep the container running always even unless stopped explicitly.
$ docker --rm -it -d -p 8085:8080 --restart unless-stopped  container_name
To get the resource usages of all the containers
$ docker stats
To delete all the container, volumes and images.
$ docker container prune -f
$ docker volume prune -f
$ docker image prune -f
$ docker image prune --all

I Hope I have covered almost everything required to build the docker containers and in turn it will help anyone to dockerize the application easily. Cheers 🙂

Leave a comment