Sitemap
Kishor Aswar

Kishor Aswar

Docker Tutorial For Beginner

6 min readFeb 22, 2022

What is docker?

Docker is a software platform that allows you to build, test, and deploy applications quickly.

Docker uses OS level virtualization.

Docker Installation on Amazon Linux EC2 Instance:

  1. Launch an instance with the Amazon Linux 2 or Amazon Linux AMI.
  2. Connect to your instance: $ ssh -i <.pem file> ec2-user@18.220.238.236
  3. Update the installed packages and package cache on your instance. Command: sudo yum update -y
  4. Install docker engine on Amazon Linux host.

Command: sudo yum install docker

5.Start the docker service and check status using below command.

sudo service docker start or systemctl start docker // systemctl status docker

Press enter or click to view image in full size
Docker Daemon Status

6. Check Docker Version using below command.

Docker Version Command

############################################

How to pull image and create container:

Pull image using command #docker pull <image-name>:<tag>

Note: By default tag is latest..

Press enter or click to view image in full size
#docker pull nginx
Press enter or click to view image in full size
docker run -itd nginx

Using command #docker run -itd <image-name>we have created nginx container .

To access nginx application we have first find IP address of container using command #docker inspect <container-id>

Once we get container id we can access application locally using curl command.

Press enter or click to view image in full size
Access Nginx Application Locally on Linux Host

To access application outside use -p <host-port>:<container:port> option while creating container .

Press enter or click to view image in full size

Now we can access nginx on browser.

http://<public-ip>:80

Press enter or click to view image in full size

Docker Basic Commands:

We can find below command using #docker — help command.

1.attach -Attach local standard input, output, and error streams to a running container

2. build - Build an image from a Dockerfile

3.commit - Create a new image from a container’s changes

Press enter or click to view image in full size

4. cp -Copy files/folders between a container and the local filesystem

Press enter or click to view image in full size

5. exec -Run a command in a running container

6. export - Export a container’s filesystem as a tar archive

7. import -Import the contents from a tarball to create a filesystem image

Press enter or click to view image in full size

8. history — Show the history of an image

Press enter or click to view image in full size

9. images — List images

Press enter or click to view image in full size

10. inspect - Return low-level information on Docker objects

#docker inspect <container-id>

11. kill — Kill one or more running containers

#docker kill <container-id>

12. login - Log in to a Docker registry

#docker login

13. logout - Log out from a Docker registry

#docker logout

14. logs - Fetch the logs of a container

#docker logs <container-id>

15. pause - Pause all processes within one or more containers

#docker pause <container-id>

16. port -List port mappings or a specific mapping for the container

#docker port <container-id>

17. ps — List containers

#docker ps

18. pull -Pull an image or a repository from a registry

#docker pull <image-name>

19. push - Push an image or a repository to a registry

#docker image push kishoraswar/mynewimage:v1.0

20. restart - Restart one or more containers

#docker restart <container-id>

21. rm -Remove one or more containers

#docker rm <container-id>

22. rmi -Remove one or more images

#docker rmi <image-name>

Get Kishor Aswar’s stories in your inbox

Join Medium for free to get updates from this writer.

23. run -Run a command in a new container

#docker run -itd <image-name>

24. start -Start one or more stopped containers

#docker start <container-id>

25. stats -Display a live stream of container(s) resource usage statistics

#docker stats <container-id>

26. stop -Stop one or more running containers

#docker stop <container-id>

27. top -Display the running processes of a container

#docker top <container-id>

28. unpause - Unpause all processes within one or more containers

#docker unpause <contianer-id>

What is image in docker?

Docker image is a blueprint of container or set of instruction to build container.

Its read only template that contain set of instructions for creating a container that can run on the Docker platform

In above diagram we can see base image is CentOS using base image we have created final image.

While creating image temporary container is created in background. Inside that container given instruction got executed i.e #yum update and #yum install vim commands.

Dockerfile :

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image

Docker can build images automatically by reading the instructions from a Dockerfile.

Dockerfile contain Instruction part and command part as below

INSTRUCTION →Command/Arguments

— — — — — — — — — — — — — — —

FROM <image-name>

MAINTAINER <author>

RUN <OS-CMD>

CMD <Default First command > [executable, arg1, arg2 ]

ENTRYPOINT <Default First command > [executable, arg1, arg2 /bin/bash]

ENV <variable name><value>

ARG <variable name> <value>

USER <user-name>

WORKDIR <path>

COPY <hostsrc> <container dest>

ADD <hostsrc/URL><container dest>

EXPOSE <port>

Dockerfile Command Detail With Example

FROM → It use for base image.

ex: FROM ubuntu:latest

MAINTAINER →Author of docker file for maintenance

ex: MAINTAINER Kishor Aswar

ARG →built-time arguments, not available in containers

ex: ARG var_name=def_value # default ARG

ENV → environment variables , available in containers

ex: ENV JAVA_HOME /opt/java Here we setup JAVA_HOME environment variable and value is /opt/java

ARG Vs ENV: The difference between ARG and ENV is that after you set an environment variable using ARG, you will not be able to access that later on when you try to run the Docker Container.

RUN → It will help us to execute OS level command. Its Create new layer in image.

ex: RUN yum update -y

CMD → Specifies the instruction that is to be executed when a Docker container starts.

If we have multiple CMD in docker file it will override.

ex: CMD [“/bin/bash”]

While creating container if we pass any input it will be override in CMD.

ENTRYPOINT →ENTRYPOINT instruction is used to set executables that will always run when the container is initiated. ENTRYPOINT commands cannot be ignored or overridden.If we passing any command after entrypoint it will append on entrypoint command

ex: ENTRYPOINT [“echo”, “hi kishor” ]

COPY → It will copy file from local system to container.

ex: COPY myfile /tmp/

ADD → Its similar to COPY but it help us copy multiple files at a time. If we want to copy .tar file from source or remote location we are using ADD instruction in dockerfile.

ex: ADD myfile.tar /opt/middleware/webapp/

USER → It help us switch user inside container.

ex: USER root

WORKDIR → It will default working directory inside container.

ex: WORKDIR /opt/middleware/webapp

EXPOSE → Exposing ports is a way of documenting which ports are used, but does not actually map or open any ports. Exposing ports is optional.

ex: EXPOSE 80

Sample Dockerfile with all Instruction:

============================

FROM ubuntu

MAINTAINER Kishor Aswar

USER root

WORKDIR /opt/myapp

RUN apt-get update -y

ENV JAVA_HOME /opt/java

CMD [“echo”, “Hi Kishor”]

ENTRYPOINT [“echo”, “hi kishor” /bin/bash]

EXPOSE 80

--

--