DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How To Use the Node Docker Official Image
  • Using Environment Variable With Angular
  • Docker Commands Beginners Should Know
  • How to Integrate a Distributed Database With Event Streaming

Trending

  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • Building Enterprise-Ready Landing Zones: Beyond the Initial Setup
  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Docker Commands for Development to Deployment

Docker Commands for Development to Deployment

This article lists and explains all the commands needed for a Docker container, from developing the image to deployment.

By 
Saravanan Subramanian user avatar
Saravanan Subramanian
·
Updated Aug. 10, 17 · Tutorial
Likes (31)
Comment
Save
Tweet
Share
22.6K Views

Join the DZone community and get the full member experience.

Join For Free

The objective of this article to understand the end to end flow of container development to deployment in the target environment and list the Docker commands needed for every action.

1. Introduction

The overall process consists of developing a container image with your code, dependent software, and configurations, running and testing the container in a development environment, publishing the container image into Docker Hub, and finally, deploying the Docker image and running the container in the target environment. This article assumes that you have installed Docker engine in the development and target environment. Please refer to 6.3 for installation instructions.

2. Develop Container Image

To build the container image, we have to create a dockerfile which will contain all the necessary information. Please refer here to develop the dockerfile.

2.1 Build Docker Container

$docker build -t containername .

This command will take the Dockerfile present in your current directory. If you have the dockerfile in a different name and different location, we can use -f flag to specify the docker file name. The “docker build” command will build the container image in the name specified with “-t” flag.

$docker build -t myapp .

01-DockerBuild

2.2 Docker Image Naming Convention

You can provide any name to a Docker container, when you run locally.  It could be as simple as “myApp” as shown above. But if you want to publish the image into Docker Hub, there is a specific naming convention to be followed. This convention helps the Docker tools to publish the container image into right namespace and repository.

The format:

NameSpace/Repository:Version

So, I build the docker image using the above convention:

$docker build -t saravasu/techietweak:001 .

We can also use the “docker tag” command to create an image from an existing image. The “docker tag” command is explained below.

2.3 List All Images in the Docker

$docker images

02-DockerImagesList

3. Run the Container

3.1 Start the Docker Container

Use the “docker run” command to start the Docker container.

$docker run -d -p 8080:8080 saravasu/techietweak:001

03-DockerRun

The “-d” option runs the container in the detached mode, so that the container continues to run, even if the terminal is closed.

The “-p” command used to map the ports. In this example, “-p 8080:8080” the first port number is the port used the Docker host. The second port number is used by the Docker container. As per this command, all the traffic comes to the Docker host port, will be forwarded to the Docker container port. 

3.2 Check Current Running Containers

$docker ps

07-DockerPS

From the above output, we can see that a Docker container is running in the name of “trusting_snyder.”

To list all the containers irrespective of the states, use the “-a” switch.

$docker ps -a

3.3 Show the Console Logs of the Running Container

$docker logs <containerName>

08-dockerlogs

ContainerName can be found with the “docker ps” command.

3.4 Log into the Container

$docker exec -it containerId /bin/bash

The above command will prompt you with “bash” shell of the container.

09-dockerlogin

3.5  Stop the Running Container

$docker stop <containername>

11-DockerStop

3.6  Remove the Container Image From Docker

$docker rm imageId

Find the imageId of the container using the command “docker images” or “docker images -a.”

$docker rmi -f <List Of Image Ids>

11-DockerRMIImages

The above command will forcefully delete the given image.

3.7 Clean Up Your Docker/Delete All Container Images in the Local Docker

$docker rmi -f $(docker images | tr -s ' ' ' ' | cut -d' ' -f3)

4. Publish the Container Image

The Docker container images can be published to your local dockyard or the public Docker hub. The process and commands are the same for both. To publish your Docker image in the Docker Hub, first create your namespace and repository at http://hub.docker.com.

I have used my namespace “saravasu” and the repository “techietweak” for this exercise.

00-dockerhubRepo

4.1 Log Into Docker Hub

$docker login

If you want to log into your local repository, please provide the URL. If a URL is not specified, then this command will log into Docker Hub.

$docker login http://localhost:8080

04-DockerLogin

4.2 Tag the Container Image

To push the Docker container image into Docker Hub, it must be tagged in a specific format:  <Namespace>/<Repository>:<Version>. If you don’t specify the version, it will be taken as “default.” In the below command, I am tagging the image:

$docker tag myapp:latest saravasu/techietweak:001

4.3 Push the Docker Image Into Docker Hub

$docker push saravasu/techietweak:001

10-DockerPushToDockerHub

4.4 Check the Container Images in Docker Hub

Now log into your Docker Hub account and check for the images in the respective repository.

05-dockerhubimages

5 Deploy the Container

5.1 Pull the Docker Container Image

Login into Docker Hub from the host machine in the target environment and pull the container image from Docker Hub. If you want to pull it from your private dockyard, use the command “$docker login <hostname>” to specify the hostname of the private dockyard.

$docker login

The above command will log into https://hub.docker.com, since the host name is not specified.

$docker pull saravasu/techietweak:001

12-dockerPull

5.2 Check the Image

The docker pull command downloaded the container image from the Docker Hub. We can validate the same by using the “docker images” command.

 $docker images

06-DockerPull-Images

5.3 Run the Container

Now we can run the Docker container in the same way we ran it in the development environment and test it in the same way we have done before.

$docker run -d -p 8080:8080 saravasu/techietweak:001

13-dockerTargetRun

The docker run command starts the container.  To validate it, we can use the “docker ps” command. Docker has created a new container and it is running in the name of “naughty_lewin.”

As we see above, the Docker engine provides a random name to the running container, but this could be a problem in automation, so it is always good to specify a name we want to refer. This can be achieved by using the “–name” parameter.

$docker run -d -p 8080:8080 --name "myNodeJsWebContainer" saravasu/techietweak:001

14-dockerRunNamedContainer

6. Summary

This article captures the flow and necessary commands to develop a container image, run it in the local environment, publish the image to Docker Hub, and run the container in the target environment. Further study and detailed documentation are available on the Docker website [refer to 6.1].

Docker (software) Command (computing)

Published at DZone with permission of Saravanan Subramanian, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Use the Node Docker Official Image
  • Using Environment Variable With Angular
  • Docker Commands Beginners Should Know
  • How to Integrate a Distributed Database With Event Streaming

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!