Starting with docker can be a bit tricky, Here we cover all the information required to start with your first docker container
If you are updated with today’s programming world, you must have heard about docker here and there.
It is fairly new concept but is getting popular day-by-day. So it’s time we startworking with docker. Implementing docker in your project is also a good practise and can be useful while testing. So lets start with it.
WHAT IS DOCKER ?
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.
— Official Docker Guide1
Well in simple terms its a virtualization of a base linux in your local system where you can test and run your codes. It comes very handy when you are aware of linux and how you can use it and configure it for your own tasks. Also the best feature is that it isolates the running software from your local system. This allows you to safely test your application without risking your system.
Next thing you must have heard people say is docker file. Well Docker file simply is a configuration which gives sequence of commands to run when the system is ready.
Example Docker file for a go application can look like
FROM golang:1.11-alpine AS build # Install tools required for project # Run `docker build --no-cache .` to update dependencies RUN apk add --no-cache git RUN go get github.com/golang/dep/cmd/dep # List project dependencies with Gopkg.toml and Gopkg.lock # These layers are only re-built when Gopkg files are updated COPY Gopkg.lock Gopkg.toml /go/src/project/ WORKDIR /go/src/project/ # Install library dependencies RUN dep ensure -vendor-only # Copy the entire project and build it # This layer is rebuilt when a file changes in the project directory COPY . /go/src/project/ RUN go build -o /bin/project # This results in a single layer image FROM scratch COPY --from=build /bin/project /bin/project ENTRYPOINT ["/bin/project"] CMD ["--help"]
Lets understand this .
- The first line
FROM XXimplies which linux base to be used for container
RUN xgives instruction to which file should be run.
COPY X Ygives instruction to COPY X from your system to Y of docker container
- Last is
ENTRYPOINTwhich specifies once the docker container is set up which will act as the initial starting point
CMDis the command which will be run once it starts.
Containers is the isolated environment where your system will run. Now the question is there are many Virtualization software like VirtualBox etc. So what’s the difference between them.
So the main difference is >Containers take a different approach: by leveraging the low-level mechanics of the host operating system, containers provide most of the isolation of virtual machines at a fraction of the computing power.> — Official Docker Guide1
In simple terms, the container uses you host system terminal hence instead of running whole system, the docker container is super light weight and hence require very less computation power.
GETTING STARTED WITH IT
This is your local system which connects to your docker host and handles the build etc.
- Docker build is used to build your dockerfile to create virtualization.
- Docker pull is used to get new docker images form docker hub
- Docker run is used to run the docker images etc.
This is the main server or Daemon which handles all the docker images and run the images on containers.
Images are like the docker files you create or using any official images from Docker Hub.
Docker Hub have all the images like official Ubuntu, Redis etc.
RUNNING A UBUNTU DOCKER CONTAINER
Step 1. Installing Docker
- Check whether docker is installed in your system, use
docker --version =>
Docker version 19.03.7-ce, build 7141c199a2 This means you have Docker installed, else refer Official Docker Installation.
Step 2. Pull Docker Image from Docker Hub We will be using Ubuntu Image from Docker hub
sudo docker pull ubuntu:latest Output
latest: Pulling from library/ubuntu Digest: sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7 Status: Image is up to date for ubuntu:latest docker.io/library/ubuntu:latest
Now you have docker image downloaded.
For checking the image use
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 1d622ef86b13 4 weeks ago 73.9MB
Done you have sucessfully downloaded Docker image.
Step 3. Running Docker Image
Running an image means starting a container and connecting to it. Use
Sudo docker run -it ubuntu A new container will be started and passing the flags
-it directly connects your terminal to the docker container.
You must be seeing something like
root@a071161fc9ed:/# This is your new docker container. You can try all the commands of ubuntu here and can be executed.
Note:- As soon as you exit the docker container the progress of the container will be stored temporarily in the stopped containers.
That’s all, You have sucessfully started your first docker container.
INTRODUCTION TO DOCKER HUB
As of now you can pull a docker image and run it and do whatever you want to do with it.
Lets see how you can use Docker Hub for your project.
- Go to Docker HUB
- Go to search window and searhc what ever image you want Eg: Alpine You will see something like this
Over here you will find the command to pull this image to your local machine.
Well Now you know everything which one needs to start working with Docker
We will discus advanced concepts like Docker Compose, Docker Networking etc In next post.
If You find it interesting!! we would really like to hear from you.
Ping us at Instagramfirstname.lastname@example.org
IF YOU WANT ARTICLES ON ANY TOPICS DM US ON INSTA.
Thanks for reading!!