Docker is a container platform provider to address every application across the hybrid cloud. Docker enables true independence between applications and infrastructure and developers and IT ops to unlock their potential and creates a model for better collaboration and innovation.
Once Docker is installed , we’ll start by creating the Dockerfile. Then we’ll create the Docker image from the Dockerfile. We’ll run it as a containerized service. Then finally, we’ll learn how to use the Docker hub to share your Docker images.
Step 1: Install Docker CE
Step 2: Open ‘Docker QuickStart Terminal (In MAC Spotlight, Search for ‘Docker QuickStart Terminal’ )
Scenario: Say for example, we are creating a new application and need a Redis service. In a distributed environment, we want everyone to use the same set of configured services to prevent surprises during deployment. A simple solution is to create a Redis Docker image and distribute it across the teams.
Step 3: Create a Docker File
Create a new folder called “docker_image”. After creating the folder, create the empty Dockerfile:
$ mkdir docker_image
$ cd docker_image
$ sudo touch Dockerfile
Now that the Dockerfile is created, open it in a text editor and set the base image and maintainer information. Next, update application to have the latest version of Redis and Install the Redis Server. Finally, expose the default port for Redis using the
EXPOSE instruction and then set the entrypoint for the image.
You can now save the file. Here’s how it should look:
# Set the base image
# Dockerfile author / maintainer
MAINTAINER Name <email.id@here>
# Update application repository list and install the Redis server.
RUN apt-get update && apt-get install -y redis-server
# Expose default port
# Set the default command
Step 4: Build the Docker Image
Dockerfile is ready, let’s create the image. Run this command:
$ docker build -t redis-server .
Path is a mandatory argument for the build command. We use . as the path because we’re currently in the same directory. -t flag is used to tag the image.
Step 5: Run a Redis-Server Instance
With the image we just created, we can now create a container running a Redis server instance inside. Run this command:
$ docker run –name redis_instance -t redis-server
This creates a container with the name redis_instance. It is a good practice to assign the name for the container or else you will need to deal with complex alphanumeric container IDs.
Useful Docker Commands:
Command to list docker image
$ docker image ls
Command to list docker containers
$ docker ps
Command to kill Conatiner
$ docker kill redis_instance
Command to save docker image
$ docker save redis-server > redis-server-image.tar
Step 6: Upload to Docker Hub
Now that we’ve successfully built an image and created a container, let’s share the images using Docker Hub.
Create login credentials in Docker Hub
$ docker login
$ docker tag b538350ff2a3 curtistechnologies/redis-server:version1.0
$ docker image push curtistechnologies/redis-server:version1.0
$ docker image pull curtistechnologies/redis-server:version1.0