Docker for Beginners Part 3 - Docker Image and Docker Container
Part 3 - Docker Image and Docker Container
This is the Part 3 from the Docker series - Docker for Beginners in 8 Parts. In this post we're going to explore Docker Images and Docker Containers!
- Part 1 - Differences between Containers and Virtual Machines
- Part 2 - Overview of Docker Installation for Mac and Ubuntu
- Part 3 - Docker Images and Containers
- Part 4 - Exploring Docker Images in Details
- Part 5 - Exploring Docker Containers in Details
- Part 6 - Building Custom Docker Images with Dockerfile
- Part 7 - Pushing our Great Docker Image to Docker Hub
- Part 8 - Keeping MongoDB Data with Docker Volumes
A simple comparison between Docker Images and Docker Containers could be:
A Docker Image is the prototype, the skeleton to create your container.
Docker Image is a recipe for a cake!
A Docker Image describes what your container will have like applications, exposed ports, mounted volumes, script to be executed in the start up, etc.
A Container is the environment itself up and running, ready for your application
Container is the real cake from the recipe!
So, we must use Images to start and run Containers, right?
And you can ask:
Where does that Docker Image come from?
Docker Images come from Docker Repository! Yeh, this is a new term and we will see more about that in details.
But for now, we just need to know that Docker has a repository to keep Docker images.
Those images can be images to run containers with Linux, Tomcat, Nginx or Java for example, together or not.
Ok, let's hacking some Docker code!
First Docker Image
We will get our first Docker Image from the Docker Repository!
This image will be the Docker Image for Tomcat applications. Nice, isn't?
To get that image, we must type the docker pull command in the terminal followed by the image name, as below:
$ docker pull tomcat
Easy! Notice that an image has been downloaded from the Docker Repository. You will see an output showing something like this:
Perfect! Now we have the Docker Tomcat image locally. But remember, an Image is just a prototype, a skeleton, a cake recipe! We can't eat recipes 🙁
So, let's make the cake!
First Docker Container
Now it's time to eat! Let's make our cake from the Docker Tomcat Image.
To run our first container based on that image, let's use the docker run command:
$ docker run tomcat
Just that! You can go to your
chrome preferred browser and type the address:
And you will see the Tomcat default page up and running \o/
Notice that we're using the default port 8080. Maybe you already have another application running in this port, since it is really famous port 🙂
Let's change this behavior by using the flag -p while starting the docker container to indicate which port should be used. Type Ctrl+C on the Terminal to stop the current Tomcat Container and type the command:
$ docker run -p 8083:8080 tomcat
The pattern used here was:
$ docker run -p your_desired_port:tomcat_port_on_container tomcat
Now you can access the Tomcat default page from the address:
After this first emotion, let's take a step back to the
boring important theory. Don't run away!
Just to learn and overview all of the most important names in the Docker World, we will see each of them in this part of the series:
Just to remember, a Docker Image is a recipe that contains the instructions to start and run containers. Again, this is the cake's recipe, not the cake itself.
Now it is the cake! Containers will run your application using the environment that will be built based on its Docker image.
For example, you could run a Java Application in a Linux Container, that was built from a Docker Ubuntu Image
Docker Registry is the place where we can push or pull Docker Images. This place can be private or public and it stores and shares lots of images.
You can, for example, push your particular images to a Docker Registry to keep that images public and developers could pull that image and build its containers and your image could have success in the world!
Docker Repository is a namespace that is used for storing a Docker Image. I know, the first impression is that Docker Repository and Registry is the same thing.
The difference is that the Repository is just the namespace that indicate which address should be used to get your images on Docker Registry
Docker Hub is the Official Docker Registry to push and pull images. You can create yourself your own Docker Registry if you want (we're going to see it with Docker and Kubernetes in another post!).
To keep practical, let's see my Registry on the Docker Hub, that has a namespace as Docker Repository address.
This is one of my public repositories: https://hub.docker.com/r/alexandregama/spring-boot/
Notice that this is a namespace to find my public Docker Image.
The DockerHub is the Docker Registry
This means that images can be stored in the DockerHub
Let's explore Docker images in more details in the next post of the Docker Series: Part 4 - Docker Images in Details
Follow us to keep up to date!
Hacking Code FounderHacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
Guides and Tutorials
Go Language Tutorial
Guides and Tutorials